简体   繁体   English

C#-系统数据SqlClient SqlException

[英]C# - System Data SqlClient SqlException

So I am in the midst of debugging a program I have been working on quite sometime that will email the user an email that will the user an email that contains information on an error a client is having trying to get into a mobile app. 因此,我正在调试我一直在研究的程序,该程序将向用户发送一封电子邮件,向用户发送一封电子邮件,其中包含有关客户端尝试进入移动应用程序的错误的信息。 The problem that I am having is even though I have set the parameter, I am getting the following error message: 即使设置了参数,我仍然遇到的问题是,我收到以下错误消息:

The parameterized query '(@USER_NAME varchar(8000))SELECT USER_NAME, EMAIL, FIRST_NAME, L' expects the parameter '@USER_NAME', which was not supplied. 参数化查询'(@USER_NAME varchar(8000))SELECT USER_NAME,EMAIL,FIRST_NAME,L'需要未提供的参数'@USER_NAME'。

I have never encountered this error before so I am not sure if there is something I am missing in my code or if I just put in the wrong syntax. 我之前从未遇到过此错误,因此我不确定代码中是否缺少某些内容或者语法是否错误。 Below is both the method where the error is happening in and the class that this method uses: 下面是发生错误的方法以及该方法使用的类:

Method: 方法:

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: 类:

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; }  
}

Change the sqlError.Parameters.Add to AddWithValue 将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;
}

Here is an example of the correct way to use the parameters collection add function: 这是使用参数集合添加功能的正确方法的示例:

SqlParameter param = new SqlParameter("@USER_NAME", SqlDbType.NVarChar, 8000);
param.Value = "Sam";
sqlError.Parameters.Add(param);

Note the setting of the value to Sam. 注意将值设置为Sam。

You could also use the function AddWithValue if you don't want to create the param variable. 如果不想创建param变量,也可以使用AddWithValue函数。 (However, @JoelCoehoorn will be worried about performance issues - see question comments) (但是,@ JoelCoehoorn将担心性能问题-请参阅问题注释)

You add your parameter to the call, but give the parameter no value. 您将参数添加到调用中,但不给参数值。

change 更改

sqlError.Parameters.Add("@USER_NAME", System.Data.SqlDbType.VarChar); 

to

sqlError.Parameters.Add("@USER_NAME", System.Data.SqlDbType.VarChar).value = username;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Data.SqlClient.SqlException C# - System.Data.SqlClient.SqlException C# Linq Windows窗体C#System.Data.SqlClient.SqlException - Linq Windows Form C# System.Data.SqlClient.SqlException C#MySQL错误System.Data.SqlClient.SqlException: - C# MySQL error System.Data.SqlClient.SqlException: c#System.Data.SqlClient.SqlException(0x80131904) - c# System.Data.SqlClient.SqlException (0x80131904) System.Data.SqlClient.SqlException:&#39;,&#39;附近的语法不正确。 C# - System.Data.SqlClient.SqlException: 'Incorrect syntax near ','.' c# 系统数据SqlClient SqlException - System Data SqlClient SqlException 在System.Data.dll中发生了C#MS Access数据库连接System.Data.SqlClient.SqlException” - C# MS Access Database Connection System.Data.SqlClient.SqlException' occurred in System.Data.dll 错误System.Data.SqlClient.SqlException:&#39;将数据类型nvarchar转换为数字时出错。 在c#中 - Error System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.' in c# 尝试从C#代码运行脚本SQL时System.Data.SqlClient.SqlException - System.Data.SqlClient.SqlException when try to run script SQL from C# code C# System.Data.SqlClient.SqlException 无法将值 NULL 插入列 ERROR FOREIGN KEY - C# System.Data.SqlClient.SqlException Cannot insert the value NULL into column ERROR FOREIGN KEY
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM