必须声明标量变量@Id? [英] Must declare scalar variable @Id?

查看:32
本文介绍了必须声明标量变量@Id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm trying to get my "Customers" from my database, but i get an exception

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

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

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

Additional information: Must declare the scalar variable "@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

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

这篇关于必须声明标量变量@Id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆