假設我在SQL數據庫中有一個名爲Table_T
的表,它包含6列(column1
,column2
,...,column6
)。如何僅從SQL數據庫中顯示DataGrid中的某些列?
下面的代碼填充數據庫中的所有列到dataGridView
:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(
"Data Source=PCN-TOSH;Initial Catalog=mydb;Integrated Security=True");
cn.Open();
SqlCommand cm = new SqlCommand("SELECT *FROM Table_T");
cm.Connection = cn;
SqlDataAdapter da = new SqlDataAdapter(cm);
DataTable dt = new DataTable();
da.Fill(dt);
cn.Close();
dataGridView1.DataSource = dt;
}
但我怎麼可以只選擇欄3 & column5,並將它們填充到dataGridview1
?
SqlCommand cm = new SqlCommand(「SELECT column3,column5 FROM Table_T」); – c4pricorn