i have one database, and it contains some columns. My requirement is that how to display each of the data that i stored in the databse on a text box? my code is shown below (after the connection string)
我有一个数据库,它包含一些列。我的要求是如何在文本框中显示我存储在数据库中的每个数据?我的代码如下所示(在连接字符串之后)
conn.Open();
mycommnd.ExecuteScalar();
SqlDataAdapter da = new SqlDataAdapter(mycommnd);
DataTable dt = new DataTable();
da.Fill(dt);
What changes that i make after da.Fill(dt) for displaying data on the text box.
在da.Fill(dt)之后我在文本框中显示数据后所做的更改。
4 个解决方案
#1
Something like:
textBox1.Text = dt.Rows[0].ItemArray[0].ToString();
Depends on the name of your textbox and which value you want to put into that text box.
取决于文本框的名称以及要放入该文本框的值。
#2
One form of using the ExecuteScalar:
使用ExecuteScalar的一种形式:
textBox.Text = mycommnd.ExecuteScalar().ToString();
#3
The DataTable consists of rows and columns, that you can reach in some different ways:
DataTable由行和列组成,您可以通过以下不同方式访问:
// get value from first row, first column
myTextBox.Text = dt.Rows[0][0];
// get value from first row, column named "First",
myTextBox.Text = dt.Rows[0]["First"];
#4
You need to loop inside each columns in the DataTable to get the values and then concatenate them into a string and assign it to a textbox.Text property
您需要在DataTable中的每个列内循环以获取值,然后将它们连接成一个字符串并将其分配给textbox.Text属性
DataTable dt = new DataTable();
TextBox ResultTextBox;
StringBuilder result = new StringBuilder();
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dt.Columns)
{
result.Append(dr[dc].ToString());
}
}
ResultTextBox.Text = result.ToString();
#1
Something like:
textBox1.Text = dt.Rows[0].ItemArray[0].ToString();
Depends on the name of your textbox and which value you want to put into that text box.
取决于文本框的名称以及要放入该文本框的值。
#2
One form of using the ExecuteScalar:
使用ExecuteScalar的一种形式:
textBox.Text = mycommnd.ExecuteScalar().ToString();
#3
The DataTable consists of rows and columns, that you can reach in some different ways:
DataTable由行和列组成,您可以通过以下不同方式访问:
// get value from first row, first column
myTextBox.Text = dt.Rows[0][0];
// get value from first row, column named "First",
myTextBox.Text = dt.Rows[0]["First"];
#4
You need to loop inside each columns in the DataTable to get the values and then concatenate them into a string and assign it to a textbox.Text property
您需要在DataTable中的每个列内循环以获取值,然后将它们连接成一个字符串并将其分配给textbox.Text属性
DataTable dt = new DataTable();
TextBox ResultTextBox;
StringBuilder result = new StringBuilder();
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dt.Columns)
{
result.Append(dr[dc].ToString());
}
}
ResultTextBox.Text = result.ToString();