ComboBox的SqlDataReader [英] SqlDataReader for ComboBox
本文介绍了ComboBox的SqlDataReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友,
我在下面有这个代码,应该从SQL DB列ubytov中选择项目。但它没有选择任何价值。为什么?
Hello friends,
I have got this code below which should select item from SQL DB column ubytov. But it doesn't select any value. Why?
try
{
SqlCommand novyprikaz = new SqlCommand("SELECT * FROM klient WHERE akce=" + vyberradek, spojeni);
spojeni.Open();
SqlDataReader precti = novyprikaz.ExecuteReader();
if (precti.Read())
{ comboBox2.SelectedItem = precti.GetString(precti.GetOrdinal("ubytov"));
}
}
catch (Exception ex)
{
MessageBox.Show("Chybové hlášení2: " + ex.Message.ToString());
}
spojeni.Close();
}
这就是我在form_load上填写它的方式
And this is how I fill it on form_load
private void klient_Load(object sender, EventArgs e)
{
spojeni.Open();
var cb1 = new SqlCommand("SELECT odjezd FROM stanice WHERE akce="
+ vyberradek+ " ORDER BY odjezd ASC", spojeni);
var cb2 = new SqlCommand("SELECT text FROM ubytov WHERE akce="
+ vyberradek + " ORDER BY text ASC", spojeni);
try
{
SqlDataReader dr = cb1.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr["odjezd"]);
}
dr.Close();
dr.Dispose();
SqlDataReader dr2 = cb2.ExecuteReader();
while (dr2.Read())
{
comboBox2.Items.Add(dr2["text"]);
}
dr2.Close();
dr2.Dispose();
spojeni.Close();
}
任何人都知道我该怎么办?
Anyone know what should I do with it ?
推荐答案
建议始终使用参数化查询。显示如何填充组合框的示例(我假设klient是表名)。
Advised to use parameterized query always. Showing example how to fill combo box(I assume klient is table name).
SqlCommand novyprikaz = new SqlCommand("SELECT * FROM klient WHERE akce=@xyz", spojeni);
spojeni.Open();
cmd.Parameters.AddWithValue("xyz",your actual value);//like textbox text
SqlDataAdapter da = new SqlDataAdapter(novyprikaz);
DataSet ds = new DataSet();
da.Fill(ds);
comboBox2.DataSource=ds;
comboBox2.DataBind();
问候..:笑:
Regards.. :laugh:
这篇关于ComboBox的SqlDataReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文