所以我正在调试我一直在工作的程序,它会向用户发送一封电子邮件,向用户发送一封电子邮件,其中包含客户端试图进入移动应用程序的错误信息。我遇到的问题是即使我已经设置了参数,我收到以下错误消息:
参数化查询'(@ USER NOTchar(8000))SELECT USER_NAME,EMAIL,FIRST_NAME,L'需要参数“@USER_NAME”,该参数未提供。
之前我从未遇到过这个错误,因此我不确定我的代码中是否存在某些内容,或者我是否只是输错了语法。下面是发生错误的方法和此方法使用的类:
方法:
private static MyHomeInformation GetUserDataFromMyHome(string username)
{
MyHomeInformation myHomeInformation = null;
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.MyHomeConnectionString))
{
SqlCommand sqlError = connection.CreateCommand();
sqlError.CommandText = @"SELECT USER_NAME, EMAIL, FIRST_NAME, LAST_NAME, TRAVELER_UID FROM TANDT_PORTAL.dbo.[USER] WHERE USER_NAME = @USER_NAME";
sqlError.Parameters.Add("@USER_NAME", System.Data.SqlDbType.VarChar);
connection.Open();
SqlDataReader reader = sqlError.ExecuteReader();
if (reader.Read())
{
myHomeInformation = new MyHomeInformation();
myHomeInformation.myHomeUserName = Utilities.FromDBValue<string>(reader["USER_NAME"]);
myHomeInformation.myHomeEmail = Utilities.FromDBValue<string>(reader["EMAIL"]);
myHomeInformation.myHomeFirstName = Utilities.FromDBValue<string>(reader["FIRST_NAME"]);
myHomeInformation.myHomeLastName = Utilities.FromDBValue<string>(reader["LAST_NAME"]);
myHomeInformation.myHomeTravelerUID = Utilities.FromDBValue<Guid>(reader["TRAVELER_UID"]);
}
}
return myHomeInformation;
}
类别:
class MyHomeInformation
{
public string myHomeUserName { get; set; }
public string myHomeEmail { get; set; }
public string myHomeFirstName { get; set; }
public string myHomeLastName { get; set; }
public Guid myHomeTravelerUID { get; set; }
}
答案 0 :(得分:2)
将sqlError.Parameters.Add更改为 AddWithValue
private static MyHomeInformation GetUserDataFromMyHome(string username)
{
MyHomeInformation myHomeInformation = null;
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.MyHomeConnectionString))
{
SqlCommand sqlError = connection.CreateCommand();
sqlError.CommandText = @"SELECT USER_NAME, EMAIL, FIRST_NAME, LAST_NAME, TRAVELER_UID FROM TANDT_PORTAL.dbo.[USER] WHERE USER_NAME = @USER_NAME";
sqlError.Parameters.AddWithValue("@USER_NAME", username); // THIS ROW SHOULD BE UPDATED
connection.Open();
SqlDataReader reader = sqlError.ExecuteReader();
if (reader.Read())
{
myHomeInformation = new MyHomeInformation();
myHomeInformation.myHomeUserName = Utilities.FromDBValue<string>(reader["USER_NAME"]);
myHomeInformation.myHomeEmail = Utilities.FromDBValue<string>(reader["EMAIL"]);
myHomeInformation.myHomeFirstName = Utilities.FromDBValue<string>(reader["FIRST_NAME"]);
myHomeInformation.myHomeLastName = Utilities.FromDBValue<string>(reader["LAST_NAME"]);
myHomeInformation.myHomeTravelerUID = Utilities.FromDBValue<Guid>(reader["TRAVELER_UID"]);
}
}
return myHomeInformation;
}
答案 1 :(得分:1)
以下是使用参数集合添加功能的正确方法的示例:
SqlParameter param = new SqlParameter("@USER_NAME", SqlDbType.NVarChar, 8000);
param.Value = "Sam";
sqlError.Parameters.Add(param);
请注意将值设置为Sam。
如果您不想创建param变量,也可以使用函数AddWithValue
。 (但是,@ JoelCoehoorn会担心性能问题 - 请参阅问题评论)
答案 2 :(得分:1)
您可以将参数添加到调用中,但请为参数指定无值。
更改
sqlError.Parameters.Add("@USER_NAME", System.Data.SqlDbType.VarChar);
到
sqlError.Parameters.Add("@USER_NAME", System.Data.SqlDbType.VarChar).value = username;