[英]C# SQL query returns nothing
I am trying to get my Winforms app to query a SQL Server CE database and return all rows that correspond to the column name specified by the user through a drop down list. 我试图让我的Winforms应用程序查询SQL Server CE数据库并返回与用户通过下拉列表指定的列名相对应的所有行。 When I run the programs it will return just a blank dataGrid.
当我运行程序时,它将仅返回一个空白的dataGrid。 This is my first time working with SQL Server CE so any help would be appreciated.
这是我第一次使用SQL Server CE,因此将不胜感激。
My code is: 我的代码是:
private void srchBTN_Click(object sender, EventArgs e)
{
string conString = Properties.Settings.Default.CurricularChangeTrackerConnectionString;
using (SqlCeConnection conn = new SqlCeConnection(conString))
{
string queryString = ("SELECT * FROM SecondaryEducation WHERE ProgramCode='" + PrgmCde.SelectedValue + "'");
try
{
conn.Open();
using (SqlCeDataAdapter adapter = new SqlCeDataAdapter(queryString, conn))
{
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
adapter.Dispose();
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
You are declaring the data table inside the using statement, try this 您正在using语句中声明数据表,请尝试以下操作
private void srchBTN_Click(object sender, EventArgs e)
{
string conString = Properties.Settings.Default.CurricularChangeTrackerConnectionString;
DataTable table = new DataTable();
using (SqlCeConnection conn = new SqlCeConnection(conString))
{
string queryString = ("SELECT * FROM SecondaryEducation WHERE ProgramCode='" + PrgmCde.SelectedValue + "'");
try
{
conn.Open();
using (SqlCeDataAdapter adapter = new SqlCeDataAdapter(queryString, conn))
{
adapter.Fill(table);
dataGridView1.DataSource = table;
adapter.Dispose();
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
That way the table wont be disposed of when the scope of the using statement is ended. 这样,在using语句的作用域结束时,将不会处理该表。
If a property is not specified in ValueMember, SelectedValue returns the results of the ToString method of the object. 如果在ValueMember中未指定属性,则SelectedValue返回对象的ToString方法的结果。
My point is you may not be getting what you think from the comboBox. 我的意思是,您可能无法从comboBox中获得您的想法。
Try this: 尝试这个:
string queryString = ("SELECT * FROM SecondaryEducation WHERE ProgramCode='" + PrgmCde.Items[PrgmCde.SelectedIndex].ToString() + "'");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.