亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

必須聲明標量變量@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條經驗 獲得超16個贊

您應該添加一個SqlParameter名稱@Id


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

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


查看完整回答
反對 回復 2019-11-04
  • 2 回答
  • 0 關注
  • 1800 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號