微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

必须声明标量变量@Id吗?

如何解决必须声明标量变量@Id吗?

您应该添加一个sqlParameter名称@Id

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

解决方法

因此,我试图从数据库中获取“客户”,但出现异常

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();

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。