How to retrieve column values separately through sql data adapter class?

大兔子大兔子 提交于 2019-12-23 05:26:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!