我在C#中使用SQL Server Compact 3.5数据库文件(.sdf
);我可以从albums_tbl
读取代码,但我想为每个记录表创建一行,而通过单击特定行,我将能够获得其名称和ID。
albums_tbl
表有两列:id, name
SqlCeDataReader rdr = null;
SqlCeCommand cm = new SqlCeCommand("SELECT * FROM albums_tbl ", cn);
rdr = cm.ExecuteReader();
int n = 0;
while (rdr.Read())
{
// rdr.GetString(0) is id
// rdr.GetString(1) is name
label1.Text = rdr.GetString(1) ;
}
当我使用sql conncetion时,我这样做了:
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM album_items WHERE name LIKE N'%" + searchItemName.Text + "%' ", con);
searchItemName.Text = "";
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView2.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = item["id"].ToString();
dataGridView2.Rows[n].Cells[1].Value = item["name"].ToString();
}
然后我可以通过点击此代码的任何行来获取信息:
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (dataGridView1.Rows.Count != 0)
{
albumId.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
albumName.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
}
}
我想现在使用SqlCeCommand