【发布时间】:2019-06-27 12:28:33
【问题描述】:
我在使用参数从 sql server 获取特定值时遇到问题,谁能解释我为什么它适用于 winfom 但不适用于 wpf 以及如何修复它 我的代码:
private void UpdateItems()
{
COMBOBOX1.Items.Clear();
SqlConnection conn = new SqlConnection(Properties.Settings.Default.constring.ToString());
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM CLIENT where cod_cli='some_specific_string'", conn);
DataSet ds = new DataSet();
da.Fill(ds, "CLIENT");
COMBOBOX1.ItemsSource = ds.Tables[0].DefaultView;
COMBOBOX1.DisplayMemberPath = ds.Tables[0].Columns["FR"].ToString();
COMBOBOX1.SelectedValuePath = ds.Tables[0].Columns["FC"].ToString();
}
执行此函数时程序崩溃并报错:
System.Data.SqlClient.SqlException: '无效的列名 'some_specific_string'。'
【问题讨论】:
-
SELECT * FROM CLIENT where cod_cli="some_specific_string" in this line "cod_cli" 此列在您的表中未找到。检查查询是否在 sql 中正确执行。
-
查询是正确的,因为在 winform 中它可以正常工作并采用正确的值。
-
我在 sql server manager 上执行,它可以工作,问题出在 wpf 我无法理解为什么它在 Winform 上工作,而不是在 wpf 上,代码是相同和相同的查询
-
它是一个 SqlException 它与 wpf 无关。大多数情况下,您正在尝试针对错误的数据库。确保它在 wpf 和 winforms 中都连接到相同
-
我找到了解决方案
SqlConnection sqlConnection = new SqlConnection(Properties.Settings.Default.constring.ToString())) { SqlCommand sqlCmd = new SqlCommand("SELECT * FROM CLIENT where cod_cli='cod_of_client''", sqlConnection); sqlConnection.Open(); SqlDataReader sqlReader = sqlCmd.ExecuteReader(); while (sqlReader.Read()) { COMBOBOX!.Items.Add(sqlReader["FN"].ToString()); } sqlReader.Close(); }