从sql server到textbox的数据简单方法 [英] Data from sql server to textbox easy way

查看:109
本文介绍了从sql server到textbox的数据简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含20个文本框的asp.net页面,应该填充到这些文本框的数据在Sql数据库中可用



您将如何显示数据从数据库到文本框?



我可以设置连接并打开它并使用SELECT * FROM RegDB检索特定行中的所有数据([Id] =某物)



如果在文本框中显示数据,有什么简单的方法吗?





我知道下面我的代码会工作..但我需要简单的方法从sql获取数据并分配到文本框



I have a asp.net page containing 20 textboxes, the data that should be populated to these textboxes are availabe in Sql database

How would you display the data from the DB to the textboxes?

I can setup the connection and open it and retrieve all the data from a specific row using SELECT * FROM RegDB where ([Id] = something)

Any simple way if displaying the data in the textboxes?


I know below my code will work.. but i need simple way to get the data from sql and assign to textbox

using (SqlConnection con1 = new SqlConnection("Data Source=.\SQLExpress;Initial Catalog=tempDB;Integrated Security=True"))
        {
            DataTable dt = new DataTable();
            con1.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("SELECT * FROM RegDB where ([Id] = something)'", con1);

            myReader = myCommand.ExecuteReader();

            while (myReader.Read())
            {
                TextBox1.Text = myReader[0].ToString();
                TextBox2.Text = myReader[1].ToString();
                TextBox3.Text = myReader[2].ToString();
                TextBox4.Text = myReader[3].ToString();
                TextBox5.Text = myReader[4].ToString();
            }
            con1.Close();
        }

推荐答案

int i = 0;
while (myReader.Read())
{
    (Page.FindControl("TextBox" + (i+1).ToString()) as TextBox).Text = myReader[i].ToString();
    i = i + 1;
}



快乐编码!

:)


Happy Coding!
:)


试试这样。

如果这是您的确切需要,那么这将对您有所帮助。



Try like this.
if this is your exact need, then this will help you.

SqlDataAdapter da = new SqlDataAdapter(myCommand);
                da.Fill(dt);
                if (dt.Rows.Count == 1)
                {
                    DataRow row = dt.Rows[0];
                    for (int i = 0; i < 20; i++)
                    {
                        string value = row[i] + "";
                        (Page.FindControl("TextBox" + i) as TextBox).Text = value; ;
                    }
                }


这篇关于从sql server到textbox的数据简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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