必须声明标量变量@Id?

Posted

技术标签:

【中文标题】必须声明标量变量@Id?【英文标题】:Must declare scalar variable @Id? 【发布时间】:2017-02-07 20:49:02 【问题描述】:

所以我试图从我的数据库中获取我的“客户”,但我得到了一个例外

System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理

附加信息:必须声明标量变量"@Id"

    using Core;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace DatabaseAccess
    
        public class DbCustomer
        
            private string ConnectionString = ConfigurationManager.ConnectionStrings["local"].ConnectionString;
            private SqlConnection connection  get; set; 

            public DbCustomer()
            
                connection = new SqlConnection(ConnectionString);
            

            public Customer GetCustomer(int Id)
            
                Customer customer = null;
                connection.Open();

                using (SqlCommand command = connection.CreateCommand())
                
                    command.CommandText = "SELECT * FROM CUSTOMER WHERE Id = @Id;";
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    
                        customer = new Customer();
                        customer.Id = reader.GetInt32(reader.GetOrdinal("Id"));
                        customer.FirstName = reader.GetString(reader.GetOrdinal("FirstName"));
                        customer.LastName = reader.GetString(reader.GetOrdinal("LastName"));
                        customer.Address = reader.GetString(reader.GetOrdinal("Address"));
                    
                    command.ExecuteNonQuery();
                    connection.Close();
                
                return customer;
            
        
    

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Core

    [DataContract]
    public class Customer
    
        [DataMember]
        public int Id  get; set; 
        [DataMember]
        public string FirstName  get; set; 
        [DataMember]
        public string LastName  get; set; 
        [DataMember]
        public string Address  get; set; 
        [DataMember]
        public string Country  get; set; 
        [DataMember]
        public string PhoneNumber  get; set; 

    


using Core;
using DatabaseAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BusinessLogic

    public class CustomerController
    
        public DbCustomer DbCustomer  get; set; 

        public CustomerController()
        
            DbCustomer = new DbCustomer();
        

        public Customer GetCustomer(int Id)
        
            return DbCustomer.GetCustomer(Id);
        
    



using BusinessLogic;
using Core;
using DatabaseAccess;
using System.Collections.Generic;

namespace WCF

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class CustomerService : ICustomerService
    
        CustomerController CustomerController = new CustomerController();

        public Customer GetCustomer(int Id)
        
            return CustomerController.GetCustomer(Id);
        

        public List<Customer> GetCustomers()
        
            return new List<Customer>();
        
    


using Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCF

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface ICustomerService
    
        [OperationContract]
        Customer GetCustomer(int Id);

        [OperationContract]
        List<Customer> GetCustomers();

    

【问题讨论】:

参数需要传递一些地方对吗? 您设置了参数@id,但您从未分配过值。因此编译器会给你这个异常。 "Must declare scalar variable" error的可能重复 哦,是的,我知道,我的老师告诉我们很多次,但这只是为了测试目的:) 谢谢你 @NeerajKumar 我觉得这个问题是一个更好的问题并且已经有了更好的答案,我关闭了另一个问题并在这里指出了它。 (是的,如果较新的问题更好,可以关闭较旧的问题并将它们指向较新的问题) 【参考方案1】:

你需要实际传入一个参数:

command.CommandText = "SELECT * FROM CUSTOMER WHERE Id = @Id;";
command.Parameters.Add("@Id", SqlDbType.Int).Value = Id;
var reader = command.ExecuteReader();

【讨论】:

Can we stop using AddWithValue() already? 奇怪地看到这个特定的帖子在投票中上下波动......似乎有点被操纵了。 @ScottChamberlain 调整 哇..我之前确实使用过 AddWithValue 但又出现了一个错误,现在它可以工作了,非常感谢 @Kixoka 这个答案没有反对票(有高代表的人可以看到总的赞成票和反对票)。然而,有些人可能已经删除了赞成票并将其交给了 Jamiec,因为最初这有 AddWithValue 而 Jamiec 没有。【参考方案2】:

您应该添加一个名为@IdSqlParameter

command.CommandText = "SELECT * FROM CUSTOMER WHERE Id = @Id;";
command.Parameters.Add("@Id", SqlDbType.Int32).Value = Id;

【讨论】:

以上是关于必须声明标量变量@Id?的主要内容,如果未能解决你的问题,请参考以下文章

表变量和错误“必须声明标量变量”

必须用参数声明标量变量

不正确的语法必须声明标量变量

例外 |必须声明标量变量@IdAlbum

查询“必须声明标量变量”

Dapper:必须在 Select 命令中声明标量变量