在某些文本框中显示表的内容

时间:2014-01-30 18:04:28

标签: c# sql-server

我编写了这段代码,用c#中的表格显示SQL Server中的表格内容,并在datagridview中显示!如果我想在某个文本框中显示它们,我该怎么办?

 SqlConnection con = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=jadid;Integrated Security=True");
 SqlCommand com_sel = new SqlCommand();

 DataSet ds = new DataSet();
 SqlDataAdapter adap = new SqlDataAdapter(com_sel);
 com_sel.CommandText = "select * from t2 where same'" + textBox1.Text + "' ";
 com_sel.CommandType = CommandType.text;
 com_sel.Connection = con;
 //com_sel.Parameters.Add("@p1", textBox1.Text);
 con.Open();
 ds.Clear();
 adap.Fill(ds);
 com_sel.ExecuteNonQuery();
 dataGridView1.DataSource = ds.Tables[0];

for(i = 0; i< ds.Tables [0] .Rows.Count - 1; i ++){textBox1.Text = ds.Tables [0] .Rows [i] [“Column”]。ToString ();

1 个答案:

答案 0 :(得分:0)

检查此代码:

SqlConnection con = new SqlConnection("Data Source=127.0.0.1;Initial 
Catalog=jadid;Integrated Security=True"); 

SqlCommand com_sel = new SqlCommand(); 

DataSet ds = new DataSet();   
SqlDataAdapter adap = new

SqlDataAdapter(com_sel);  

com_sel.CommandText = "select * from t2 where id=1";  //

com_sel.CommandType = CommandType.StoredProcedure;
com_sel.Connection = con; 
com_sel.Parameters.Add("@p1",textBox1.Text);
con.Open();
ds.Clear();
adap.Fill(ds); 
com_sel.ExecuteNonQuery();  

dataGridView1.DataSource = ds.Tables[0];

int i; //this way you display the data in the textbox 
//TextBox1 is your textbox name 
//lessthen:- less than sign not write

for(i=0;i lessthen ds.Tables[0].Rows.Count-1;i++) {

TextBox1.Text=ds.Tables[0].Rows[i]["Column"].ToString(); 
}