如何通过SQL数据适配器类分别检索列值? [英] How to retrieve column values separately through sql data adapter class?

查看:85
本文介绍了如何通过SQL数据适配器类分别检索列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用sql数据适配器...我已对以下代码进行编码,以检查其工作方式...

I am trying to learn how to use sql data adapter ... I have coded the following to check how it works ...

问题是我想要分别检索数据库表 Sana的3列(DeptNo,DeptId,DeptName)的值并将它们显示在三个单独的文本框中...

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");

            }
        }
    }
}

}

推荐答案

    foreach (DataRow myRow in MyDataSet.Tables[0].Rows)
    {
        TextBox1.Text = myRow["DeptNo"].ToString(); 
        TextBox2.Text = myRow["DeptId"].ToString(); 
        ... 
    }

这篇关于如何通过SQL数据适配器类分别检索列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆