我正在尝试运行此代码,但我收到错误“参数化查询需要参数@faid未提供”。至少根据我的知识,这段代码看起来不错。我在Windows 7上使用VS 2010,SQLEXPRESS作为后端。
提前致谢。
string getDataQuery;
lcFaid = "70464917-967b-4796-9483-3b0b4b004a3e";
SqlConnection sqlConnection1 = new SqlConnection(ccsConnectionString);
DataSet data = new DataSet();
data.Locale = System.Globalization.CultureInfo.InvariantCulture;
getDataQuery =
"SELECT customer,custtrack,ackdate FROM famain WHERE faid = @lcFaid";
SqlDataAdapter masterDataAdapter = new SqlDataAdapter();
masterDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
masterDataAdapter.SelectCommand = new SqlCommand();
masterDataAdapter.SelectCommand.Connection = sqlConnection1;
masterDataAdapter.SelectCommand.Parameters.Add("@lcFaid",
SqlDbType.UniqueIdentifier, 36, "faid").SourceVersion = DataRowVersion.Original;
masterDataAdapter.SelectCommand.CommandText = getDataQuery;
masterDataAdapter.Fill(data, "famain");
答案 0 :(得分:0)
我发现你永远不会使用变量lcFaid
。也许您打算使用它而不是文字字符串"faid"
(根本不是GUID)?
masterDataAdapter.SelectCommand.Parameters.Add("@lcFaid",
SqlDbType.UniqueIdentifier, 36, lcFaid).SourceVersion = DataRowVersion.Original;
答案 1 :(得分:0)
不确定 - 但我会
这样的事情:
string getDataQuery =
"SELECT customer,custtrack,ackdate FROM famain WHERE faid = @lcFaid";
SqlDataAdapter masterDataAdapter = new SqlDataAdapter();
masterDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
masterDataAdapter.SelectCommand = new SqlCommand();
masterDataAdapter.SelectCommand .Connection = sqlConnection1;
// first set the query text
masterDataAdapter.SelectCommand.CommandText = getDataQuery;
// after that, define the parametesr
masterDataAdapter.SelectCommand.Parameters
.Add("@lcFaid", SqlDbType.UniqueIdentifier, 36, "faid").SourceVersion = DataRowVersion.Original;
另外,正如“Gabe”在他的评论中指出的那样 - 错误消息引用参数@faid
,但是您正在定义和设置一个名为@lcFaid
的参数 - 为什么??