请高手写一段C#查询的代码,急需(新手求救)

来源:百度知道 编辑:UC知道 时间:2024/06/03 22:59:20
平台是VS2005+SQL2000 显示框体是dataGridView1
如有类似的代码请供参考,多谢大虾..

using System.Data.SqlClient;

string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); //webconfig 配置连接字符串

private void databind()
{
try
{

string sqlstr = "SELECT nid,nbt,nsj FROM news ORDER BY nid desc"; //查询语句
SqlConnection cn = new SqlConnection(strconn);
SqlCommand myCommand = new SqlCommand(sqlstr, cn);
SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
da.Fill(ds, "news");
this.dataGridView1.DataSource = ds.Tables["news"];
this.dataGridView1.DataBind();

}
catch
{
return;
}
}

这是我做的练习,用的平台是VS2005+SQL2005显示框体是dataGridView,代码的核心部分应该差不多,你仅供参考!

string str = @"Data Source = Local