感谢大家的指教!我用的数据库是Oracle,那么SqlDataReader dr=cmd.ExecuteReader(); dr.Read();这段代码是不是只能在Sql数据库中使用呢?我在msdn中查到一段相关的代码,但是不太清楚怎么用? private void CreateNewDataRow(){ // Use the MakeTable function below to create a new table. DataTable myTable; myTable = MakeNamesTable(); // Once a table has been created, use the NewRow to create a DataRow. DataRow myRow; myRow = myTable.NewRow(); // Then add the new row to the collection. myRow["fName"] = "John"; myRow["lName"] = "Smith"; myTable.Rows.Add(myRow);
foreach(DataColumn dc in myTable.Columns) Console.WriteLine(dc.ColumnName); dataGrid1.DataSource=myTable; }
//与结果集的“表名.字段名”进行绑定
studentname.DataBindings.Add("Text",dataset,"表名.字段名");但是,如果dataset被本程序的其它代码给更新过了,就要先删绑定,然后在绑定,C#蠢的不知道什么叫自动化,代码如下:
dtudentname.DataBindngs.Clear();
//与结果集的“表名.字段名”进行绑定
studentname.DataBindings.Add("Text",dataset,"表名.字段名");
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sqlStr = "select test from test where ID = " + Request["id"] ;
SqlCommand cmd = new SqlCommand(sqlStr,conn);
SqlDataReader dr=cmd.ExecuteReader();
dr.Read();
test.Text = dr["test"].ToString();
conn.Close();
this.studentname.DataBinding.Add(binding);
private void CreateNewDataRow(){
// Use the MakeTable function below to create a new table.
DataTable myTable;
myTable = MakeNamesTable();
// Once a table has been created, use the NewRow to create a DataRow.
DataRow myRow;
myRow = myTable.NewRow();
// Then add the new row to the collection.
myRow["fName"] = "John";
myRow["lName"] = "Smith";
myTable.Rows.Add(myRow);
foreach(DataColumn dc in myTable.Columns)
Console.WriteLine(dc.ColumnName);
dataGrid1.DataSource=myTable;
}