菜鸟问一个简单的c#程序!

Yjanuary 2006-11-15 07:22:27
问一个简单的程序
用c#做个窗体
一个文本框,一个按钮,一个dategride
数据库中表table有个字段title
我在文本框中输入字符,点击按钮,下面那个dategride中就列出table中的所有字段
select * from table where title like '%文本框%'

该怎么写啊?
小弟初学.net啊!还什么都不懂啊!
各位高手帮帮忙,指教一下啊!
谢了啊!
...全文
226 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
liujia_0421 2006-11-15
  • 打赏
  • 举报
回复
//如果是Sqlserver,引入命名空间
using System.Data.SqlClient;

//输入字符串后,点击按钮下的代码,模仿一下:
//说明一下:以下student是数据库名,uid用户名,pwd为密码
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
try
{
        //studentInfor是表名,sno是列名
SqlCommand cmd = new SqlCommand("select * from studentInfor where sno like '%"+this.textBox1 .Text +"%'", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds, "student");
        //vs2005下dataGridView,vs2003下用dataGrid
this.dataGridView1.DataSource = ds.Tables["student"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message );
}
liujia_0421 2006-11-15
  • 打赏
  • 举报
回复
测试没有问题....
yu_galaxy 2006-11-15
  • 打赏
  • 举报
回复
datagrid.datasource=ds;
datagrid.Bind();
liujia_0421 2006-11-15
  • 打赏
  • 举报
回复
//如果是Sqlserver,引入命名空间
using System.Data.SqlClient;

//输入字符串后,点击按钮下的代码,模仿一下:
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
try
{
SqlCommand cmd = new SqlCommand("select * from studentInfor where sno like '%"+this.textBox1 .Text +"%'", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds, "student");
this.dataGridView1.DataSource = ds.Tables["student"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message );
}
fish_yht 2006-11-15
  • 打赏
  • 举报
回复
单击按钮时把获取文本字段放到SqlCommand中,( select * from table where title like '%+txt.text+"%'),然后重新读取数据绑定数据
levin9 2006-11-15
  • 打赏
  • 举报
回复
每按一下按钮,重新绑定datagrid
wuluhua2003 2006-11-15
  • 打赏
  • 举报
回复
SqlCommand cmd = new SqlCommand("select * from table where title like '%+txt.text+"%'",连接)
sqldatareader dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
datagrid.datasource=dr;
tcxx2008 2006-11-15
  • 打赏
  • 举报
回复
当面说???
Yjanuary 2006-11-15
  • 打赏
  • 举报
回复
能说详细点吗?
最好能有个例子啊!
shoushii 2006-11-15
  • 打赏
  • 举报
回复
datalist绑title autopostback=true

datagrid 绑%datalist.dataitem%
双黑in上海 2006-11-15
  • 打赏
  • 举报
回复
数据库连接语句就不写了
SqlCommand cmd = new SqlCommand("select * from table where title like '%+txt.text+"%'",连接)
sqldatareader dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
然后datetrid榜定dr就可以了
fancystyle 2006-11-15
  • 打赏
  • 举报
回复
查询后取得数据,再绑定.
lizhizhe2000 2006-11-15
  • 打赏
  • 举报
回复
每按一下按钮,重新绑定一下
shiaihua 2006-11-15
  • 打赏
  • 举报
回复
gre

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧