[英]Display database values in textbox
I have a datatable with 2 columns, ID
and Name
, I have populated my combobox with the column ID. 我有一个包含2列
ID
和Name
,我在组合框中填充了ID列。
string Query = "SELECT * FROM [Database]";
OleDbConnection me = new OleDbConnection(connection);
OleDbCommand constr = new OleDbCommand(Query, me);
me.Open();
OleDbDataReader reader = constr.ExecuteReader();
while(reader.Read())
{
textBox15.Text = (reader["Name"].ToString());
}
reader.Close();
When I select an item from the combobox, I want to retrieve values from the Column Name
in the same row. 当我从组合框中选择一个项目时,我想从同一行的“列
Name
中检索值。 For instance I select a value from my combobox which is in datarow 1 and it matches the datarow 1 in the table Name
例如,我从组合框中选择一个在数据行1中的值,该值与表
Name
中的数据行1匹配
Is there anyway to do this? 反正有这样做吗?
I am currently here 我目前在这里
{
string Query = "SELECT * FROM [Database] where Name ='" + comboBox6.Text + "' "; string y = textBox15.Text
OleDbConnection me = new OleDbConnection(connection);
OleDbCommand constr = new OleDbCommand(Query, me);
me.Open();
OleDbDataReader reader = constr.ExecuteReader();
constr.Parameters.Add(new OleDbParameter("@Name", y));
while (reader.Read())
{
textBox15.Text = reader["Name"].ToString();
}
me.Close();
}
}
I am still getting an error "No parameters given for one or more values"
I am sure that the code is right. 我仍然收到错误消息
"No parameters given for one or more values"
我确定代码正确。
You'll need to add a parameter to your SQL query. 您需要在SQL查询中添加参数。 For example:
例如:
string myName = myComboBox.SelectedItem.Text;
string Query = "SELECT * FROM [Database] WHERE Name = ?";
OleDbConnection conn = new OleDbConnection(connection);
OleDbCommand cmd = new OleDbCommand(Query, conn);
cmd.Parameters.Add(new OleDbParameter("@name", myName));
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while(reader.Read())
etc...
I'm not sure of the exact syntax for the OLE DB .NET Provider, but hopefully this helps somewhat. 我不确定OLE DB .NET Provider的确切语法,但是希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.