C# 从 sql 查询中获取数据到文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9761545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get data from sql query into textbox
提问by Joel
Fixed it by closing the connetion at page init and then reopen at Dropdownlist.
通过在页面初始化时关闭连接然后在下拉列表中重新打开来修复它。
I've searched for an answer to this question but without any luck.
我已经搜索了这个问题的答案,但没有任何运气。
I want to get data from a selected dropdownlist item to a textbox. I've been trying to execute this sql query without any success.
我想从选定的下拉列表项中获取数据到文本框。我一直在尝试执行这个 sql 查询,但没有成功。
Here's my code:
这是我的代码:
public partial class EditContact : System.Web.UI.Page
{
SqlConnection connection = new SqlConnection("SqlConnection");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
connection.Open();
SqlCommand SqlCommandDD = new SqlCommand("SELECT FirstName + ' ' + LastName AS 'TextField', Contact_ID, Email, PhoneNumber, CompanyID FROM ContactPerson");
SqlCommandDD.Connection = connection;
DropDownList2.DataSource = SqlCommandDD.ExecuteReader();
DropDownList2.DataValueField = "Contact_ID";
DropDownList2.DataTextField = "TextField";
DropDownList2.DataBind();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string fNameTemp = DropDownList2.SelectedValue;
string sqlquery = ("SELECT FirstName FROM ContactPerson WHERE (Contact_ID = " + fNameTemp + ")");
SqlCommand command = new SqlCommand(sqlquery, connection);
SqlDataReader sdr = command.ExecuteReader();
fNameTextBox.Text = sdr.ToString();
}
}
采纳答案by Mikhail
The “ExecuteReader” returns complex/non-scalar value. Use the “ExecuteScalar” method instead:
“ExecuteReader”返回复数/非标量值。改用“ ExecuteScalar”方法:
SqlCommand command = new SqlCommand(sqlquery, connection);
fNameTextBox.Text = command.ExecuteScalar().ToString();
回答by Elias
Try modifying the code this way:
尝试以这种方式修改代码:
string sqlquery = "SELECT FirstName FROM ContactPerson WHERE Contact_ID = " + fNameTemp;
SqlCommand command = new SqlCommand(sqlquery, connection);
SqlDataReader sdr = command.ExecuteReader();
while ( sdr.Read() )
{
fNameTextBox.Text = sdr[ "FirstName" ].ToString();
}
回答by Agustyan
There is several way to achieve this, close the connection by hardcode was not the best practice instead of using connection.close() you can place the connection inside using tag, this your code with using tag
有几种方法可以实现这一点,通过硬编码关闭连接不是最好的做法,而不是使用 connection.close() 您可以使用标签将连接放在里面,这是使用标签的代码
using (SqlConnection connection = new SqlConnection("SqlConnection"))
{
connection.Open();
SqlCommand SqlCommandDD = new SqlCommand("SELECT FirstName + ' ' + LastName AS 'TextField', Contact_ID, Email, PhoneNumber, CompanyID FROM ContactPerson");
SqlCommandDD.Connection = connection;
DropDownList2.DataSource = SqlCommandDD.ExecuteReader();
DropDownList2.DataValueField = "Contact_ID";
DropDownList2.DataTextField = "TextField";
DropDownList2.DataBind();
}
no need to close the connection manually
无需手动关闭连接
回答by Sabareeshwari Kannan
If we are going to fetch a single value from a table.. Then we can go for execute scalar instead of data adapter or data reader..
如果我们要从表中获取单个值..那么我们可以去执行标量而不是数据适配器或数据读取器..
Method 1:
方法一:
fNameTextBox.Text = command.ExecuteScalar().ToString();
Method 2:(If you use any common function)
方法二:(如果你使用任何常用函数)
object scalarobject;
scalarobject = command.ExecuteScalar();
fNameTextBox.Text = scalarobject.ToString();

