回覆列表
-
1 # gosko45082
-
2 # 使用者8171356101758
假設,
語句select id,name,code from t_emp,別名"emp"
1.單一變數賦值:
string id=ds.Tables["emp"].rows[0]["id"].value.tostring();
2.集合類的
ArrayList alNames=new ArrayList();
foreach(datarow dr in ds.Tables["emp"].rows)
{
alNames.add(dr["name"]).value.tostring();
}
3.向控制元件繫結:(ListBox)
lbxObj.DataSource = ds.Tables["t"];
lbxObj.DisplayMember = "name";
lbxObj.ValueMember = "id";
4.向控制元件繫結:(datagridview)
objdgv.datasource=ds.Tables["t"];
//建立資料庫連線 SqlConnection cnn = new SqlConnection("資料庫連線字串"); //構造查詢字串 string str = @"Select top * from A"; //新建SqlCommand物件 SqlCommand cmd = new SqlCommand(str, cnn); //開啟資料庫連線 cnn.Open(); //返回dr物件 SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); string x = dr["a"].ToString(); string y = dr["b"].ToString(); string z = dr["c"].ToString();