以前用delphi开发数据库可以使用ADOQuery或者Query,在按钮的click事件下面写如下的代码//事先设置好dbgrid,datesource,query的关系
sql:='select * from tb with(nolock) where a is null';
query1.close;
query1.sql.clear;
query1.sql.add(sql);
query1.open;但是在winforms中不知道要怎么搞,求帮忙,谢谢!说明,之前都是用delphi开发,很熟练,现在想学着用C#来玩玩。谢谢各位了。
sql:='select * from tb with(nolock) where a is null';
query1.close;
query1.sql.clear;
query1.sql.add(sql);
query1.open;但是在winforms中不知道要怎么搞,求帮忙,谢谢!说明,之前都是用delphi开发,很熟练,现在想学着用C#来玩玩。谢谢各位了。
{
string strsql='select * from tb with(nolock) where a is null';
SqlDataAdapter ad=new SqlDataAdapter(strsql,con);
dataset ds=new dataset();
ad.fill(ds)
}
跟你上面的效果是一样的,这个是最简单的转变了。
SqlConnection conn; // 数据连连接
SqlCommand cmd; // SQL指定对象
SqlDataReader reader; // 用来读取数据库返回数据的conn = new SqlConnection("连接字符串");
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "select * from tb with(nolock) where a is null";reader = cmd.ExecuteReader();while (reader.Read()) // 如果Read方法返回false,表示数据已经全部取完。
{
// 假定tb表有两列,第一列是int,第二列是nvarchar。
int id;
string name; id = reader.GetInt32(0); // 读取第一列为Int32,也就是int
name = reader.GetString(1); // 读取第二列为String。
}reader.Close();
conn.Close();
msdn查了一下,例子一大把。