日期:2014-05-18  浏览次数:20994 次

数据库的查询问题
我做了个连接数据库的程序,但是不知道怎么写查询的代码,希望高手写个简单的代码让我做下参考,谢谢。 
  {
  sconn = new SqlConnection(@"Integrated Security=SSPI;server=(local);database=pubs");
  sconn.Open();
  }

  private void button2_Click(object sender, EventArgs e)
  {
  string commstr = @"select * from titles";
  scomm = new SqlCommand(commstr, sconn);
  sdr = scomm.ExecuteReader();
  if (sdr.Read())
  {
  textBox1.Text = sdr.GetValue(1).ToString();
  textBox2.Text = sdr.GetValue(2).ToString();
  }
  }

------解决方案--------------------
楼主最好把问题说清楚一点,好像有点看不懂问题。
------解决方案--------------------
SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;server=local);database=pubs"); 
string mysql="select * from titles"; 
SqlDataAdapter myDapter=new SqlDataAdapter(mysql,conn);
DataSet ds=new DataSet(); 
myDapter.Fill(ds); 
DataTable dt = ds.Tables[0]; 
if(dt.Rows.Count >=1) 
textBox1.Text = dt.Rows[0][0].ToString();
textBox2.Text = dt.Rows[0][1].ToString();


------解决方案--------------------
C# code

SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;server=local);database=pubs");  
string mysql="select * from titles";  
SqlDataAdapter myDapter=new SqlDataAdapter(mysql,conn); 
DataSet ds=new DataSet();  
myDapter.Fill(ds);  
DataTable dt = ds.Tables[0];  
if(dt.Rows.Count  >=1)  
{
textBox1.Text  = dt.Rows[0][0].ToString(); 
textBox2.Text  = dt.Rows[0][1].ToString(); 
}