c# – 必须声明标量变量@Id?

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

An exception of type ‘System.Data.sqlClient.sqlException’ occurred in System.Data.dll but was not handled in user code

附加信息:必须声明标量变量“@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的sqlParameter

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

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...