为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUWWW 2019-11-04 13:17:10
因此,我试图从数据库中获取“客户”,但出现异常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;
查看完整描述

2 回答

?
侃侃尔雅

TA贡献1801条经验 获得超15个赞

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


command.CommandText = "SELECT * FROM CUSTOMER WHERE Id = @Id;";

command.Parameters.Add("@Id", SqlDbType.Int32).Value = Id;


查看完整回答
反对 回复 2019-11-04
  • 2 回答
  • 0 关注
  • 1705 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信