因此,我試圖從數(shù)據(jù)庫(kù)中獲取“客戶”,但出現(xiàn)異常System.Data.dll中發(fā)生類型'System.Data.SqlClient.SqlException'的異常,但未在用戶代碼中處理附加信息:必須聲明標(biāo)量變量"@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;
必須聲明標(biāo)量變量@Id嗎?
HUWWW
2019-11-04 13:17:10