【问题标题】:How to fix error 'Must declare scalar variable' in SQL query through c#?如何通过c#修复SQL查询中的错误“必须声明标量变量”?
【发布时间】:2018-06-26 04:26:32
【问题描述】:

我不断收到这个烦人的错误。我对 SQL 真的很陌生,这实际上是我的第一个查询,但我无法让它工作。我已经尝试了几个小时。请帮忙!

我得到错误:

System.Data.SqlClient.SqlException: '必须声明标量变量“@FirstNamez”。

代码:

public static void CreateNewEmployee(string FirstNamez, string LastNamez, int Pinz, string Departmentz)
{
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = @"Server = localhost\SQLEXPRESS; Database = Employee; Trusted_Connection = True;";

    SqlCommand command = new SqlCommand();

    using (connection)
    {
        connection.Open();
        string commandtext = "INSERT INTO dbo.EmployeeDatabase (FirstName, LastName, PIN, Department) VALUES (@FirstNamez, @Lastnamez, @Pinz, @Departmentz);";

        command.CommandText = commandtext;
        command.Connection = connection;

        command.ExecuteNonQuery();
    }
}

【问题讨论】:

标签: c# sql-server


【解决方案1】:

您在查询文本中定义所有参数 - 但您永远不会设置它们的值!

试试这个:

public static void CreateNewEmployee(string FirstNamez, string LastNamez, int Pinz, string Departmentz)
{
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = @"Server = localhost\SQLEXPRESS; Database = Employee; Trusted_Connection = True;";

    SqlCommand command = new SqlCommand();

    using (connection)
    {
        connection.Open();
        string commandtext = "INSERT INTO dbo.EmployeeDatabase (FirstName, LastName, PIN, Department) VALUES (@FirstNamez, @Lastnamez, @Pinz, @Departmentz);";

        command.CommandText = commandtext;
        command.Connection = connection;

        // define the parameters and set their values!
        command.Parameters.Add("@FirstNamez", SqlDbType.VarChar, 100).Value = FirstNamez;
        command.Parameters.Add("@LastNamez", SqlDbType.VarChar, 100).Value = LastNamez;
        command.Parameters.Add("@Pinz", SqlDbType.Int).Value = Pinz;
        command.Parameters.Add("@Departmentz", SqlDbType.VarChar, 100).Value = Departmentz;

        command.ExecuteNonQuery();
    }
}

【讨论】:

    【解决方案2】:

    您没有指定参数的值。查看这篇文章,了解如何将参数值添加到命令中: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx

    【讨论】:

      【解决方案3】:

      使用以下代码:

      在您的数据库中创建存储过程,而不是像这样的 AdHoc 查询:

      CREATE PROC spAddNewEmployee
      @FirstNamez nvarchar(100),
      @Lastnamez nvarchar(100),
      @Pinz nvarchar(100),
      @Departmentz nvarchar(100)
      
      AS
      
      BEGIN
          INSERT INTO dbo.EmployeeDatabase (FirstName, LastName, PIN, Department) 
          VALUES (@FirstNamez, @Lastnamez, @Pinz, @Departmentz)
      END
      

      添加您的 C# 代码:

      public static void CreateNewEmployee(string FirstNamez, string LastNamez, int Pinz, string Departmentz)
           {
              SqlConnection connection = new SqlConnection();
              connection.ConnectionString = @"Server = localhost\SQLEXPRESS; Database = Employee; Trusted_Connection = True;";
      
              SqlCommand cmd = new SqlCommand("spAddNewEmployee", connection);
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.AddWithValue("@FirstNamez", FirstNamez);
              cmd.Parameters.AddWithValue("@Lastnamez ", Lastnamez );
              cmd.Parameters.AddWithValue("@Pinz ", Pinz );
              cmd.Parameters.AddWithValue("@Departmentz ", Departmentz );
      
              connection.Open();
              cmd.ExecuteReader();
              connection.Close();
      
          }
      

      【讨论】:

      • 此代码有效,但它不回答 OP 的特定问题或提供任何关于 为什么 有效的上下文,并且还包含与 OP 的问题无关的其他位。关于可能的 SQL 注入和代码格式的注释最好保留为简单的注释。
      • 实际上废弃了,在当前状态下它甚至不会编译,因为没有名称为 con 的变量。
      • 是的,这是拼写错误。改了!我也投票删除它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多