描述:执行期间发生了未处理的异常 当前的网络请求。请查看堆栈跟踪了解更多信息 有关错误的信息以及它在代码中的起源。
异常详细信息:System.Data.SqlClient.SqlException:必须声明 标量变量“@LocationID”。
来源错误:
第43行:第44行:conn.Open();第45行:
adapter.Fill(DT);第46行:conn.Close();第47行:源文件:C:\ Users \ Dev D \ Documents \ Visual Studio 2015 \ Projects \ MySafar \ MySafar \ Models \ DBUtility.cs Line:45
Location.cs(模型类)
public class Location
{
public int LocationID;
public string LocationName;
public string PhotoPath;
public string Details;
public string CityID;
}
public int Update()
{
string query = "UPDATE Location SET LocationName = @LocationName, PhotoPath = @PhotoPath, Details = @Details, CityID = @CityID WHERE LocationID = @LocationID";
List<SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@LocationID", this.LocationID));
parameters.Add(new SqlParameter("@LocationName", this.LocationName));
parameters.Add(new SqlParameter("@PhotoPath", this.PhotoPath));
parameters.Add(new SqlParameter("@Details", this.Details));
parameters.Add(new SqlParameter("@CityID", this.CityID));
return DBUtility.ModifyData(query, parameters);
}
public bool SelectByPK()
{
string query = "SELECT * FROM Location WHERE LocationID = @LocationID";
List<SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@LocationID", this.LocationID));
DataTable dt = DBUtility.SelectData(query, parameters);
if (dt.Rows.Count > 0)
{
this.LocationName = dt.Rows[0]["LocationName"].ToString();
this.PhotoPath = dt.Rows[0]["PhotoPath"].ToString();
this.Details = dt.Rows[0]["Details"].ToString();
//this.CityID = Convert.ToInt32(dt.Rows[0]["CityID"]);
this.CityID = dt.Rows[0]["CityID"].ToString();
return true;
}
else
{
return false;
}
}
}
AdminController中的查看方法
public ActionResult Location_Edit()
{
Location l = new Location();
l.LocationID = Convert.ToInt32(Request.QueryString["ID"]);
l.SelectByPK();
return View(l)
}
[HttpPost]
public ActionResult Location_List_Edit()
{
Location l = new Location();
l.LocationID = Convert.ToInt32(Request.QueryString["ID"]);
l.LocationName = Request.Form["LocationName"];
l.PhotoPath = Request.Form["PhotoPath"];
l.Details = Request.Form["
l.CityID = Request.Form["CityID"];
l.Update();
return View("Location_List", new DataTable());
}
DBUtility.cs(模型类为数据库方法定义,用于修改插入和选择数据)
public static DataTable SelectData(String query, List<SqlParameter> parameter)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\Dev D\\Documents\\Visual Studio 2015\\Projects\\MySafar\\MySafar\\App_Data\\MySafarDB.mdf\";Integrated Security=True";
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = query;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
conn.Open();
adapter.Fill(dt);
conn.Close();
return dt;
}
答案 0 :(得分:4)
你有这个方法的问题
public static DataTable SelectData(String query, List<SqlParameter> parameter)
这是您传递List<SqlParameter> parameter
但未将其添加到command
参数列表。
这就是它给你错误的原因。添加如下所示的参数,在方法中添加这行代码。
foreach( var param in List<SqlParameter> parameter))
command.Parameters.Add(param);