我的代码如下所示:
var settings = ConfigurationManager.ConnectionStrings["InsurableRiskDB"];
string server = ConfigurationManager.AppSettings["Server"];
string cs = String.Format(ConfigurationManager.AppSettings[settings.ProviderName], ConfigurationManager.AppSettings[server]);
SqlConnection connection = new SqlConnection(cs);
string PolicyKeys = "";
for (int i = 0; i < keys.Count(); i++)
{
if (i == keys.Count() - 1)
PolicyKeys += keys[i] ;
else
PolicyKeys += keys[i] + ", ";
}
//have to change the code to pull the user's NBK.
string user = "'DATALOAD'";
const string updateQuery = @"UPDATE [InsurableRisk].[dbo].[Policy]
SET [InsuranceCarrierKey] = @ToKey
,[AuditUser] = @User
,[AuditDate] = SYSDATETIME()
WHERE PolicyKey in (@PolicyKeys) and InsuranceCarrierKey = @FromKey";
using (connection)
{
using (SqlCommand dataCommand = new SqlCommand(updateQuery, connection))
{
dataCommand.Parameters.AddWithValue("@ToKey", toKey);
dataCommand.Parameters.AddWithValue("@User", user);
dataCommand.Parameters.AddWithValue("@PolicyKeys", PolicyKeys);
dataCommand.Parameters.AddWithValue("@FromKey", fromKey);
connection.Open();
dataCommand.ExecuteNonQuery();
connection.Close();
}
}
res = true;
}
catch (Exception ex)
{
MessageBox.Show("There is an error while try in save the changes " + ex.Message, "Error Message", MessageBoxButtons.OKCancel);
res = false;
}
return res;
现在,当我运行此代码时,它说查询无法执行。它抛出异常并声明,它无法将 NVarchar 转换为变量 @PolicyKeys 的 int
关于我在这段代码中缺少什么的任何建议?