问题
I am trying to learn how to use sql data adapter ... I have coded the following to check how it works ...
the problem is i want to retrieve values of 3 columns(DeptNo,DeptId,DeptName) of my database table "Sana" separately and display them in three separate text boxes ...
Through the code mentioned below I am able to retrieve the value of entire tuple of data base table together
what should I do to reach above mentioned result???
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("Select DeptNo,DeptId,DeptName from Sana where DeptName='" + TextBox1.Text + "'", connect);
SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
DataSet MyDataSet = new DataSet();
myAdapter.Fill(MyDataSet, "Departments");
object[] rowVals = new object[3];
foreach (DataTable myTable in MyDataSet.Tables)
{
foreach (DataRow myRow in myTable.Rows)
{
foreach (DataColumn myColumn in myTable.Columns)
{
Response.Write(myRow[myColumn] + "\t");
}
}
}
}
}
回答1:
foreach (DataRow myRow in MyDataSet.Tables[0].Rows)
{
TextBox1.Text = myRow["DeptNo"].ToString();
TextBox2.Text = myRow["DeptId"].ToString();
...
}
来源:https://stackoverflow.com/questions/10805578/how-to-retrieve-column-values-separately-through-sql-data-adapter-class