Asp.net 存储过程不工作
我无法将此转换为存储过程 //sql语句中包含的字符串:Asp.net 存储过程不工作,asp.net,sql,sql-server,Asp.net,Sql,Sql Server,我无法将此转换为存储过程 //sql语句中包含的字符串: string employer = Session["Employer"].ToString(); 然后是sql语句 更新tblWorkData集EmployerName='“+txtemployername.text+”,其中EmployerName='“+Employer+”//上面的字符串 这在asp.net中运行良好 但当我把它转换成存储过程时 create proc updateWork @E
string employer = Session["Employer"].ToString();
然后是sql语句
更新tblWorkData集EmployerName='“+txtemployername.text+”,其中EmployerName='“+Employer+”//上面的字符串
这在asp.net中运行良好
但当我把它转换成存储过程时
create proc updateWork
@EmployerName nvarchar(max)
as
begin
update tblWorkData set EmployerName=@EmployerName where EmployerName=@EmployerName
end
现在,当我在asp.net上执行sp时
string update = "updateWork '"+employer+"','"+txtemployername.text+"'";
我说“争论太多”时出错了。我该怎么办?您的存储过程只接受一个参数,而您用两个参数调用它。要解决此问题,您需要更改过程,以采用如下两个参数:
create proc updateWork
@EmployerName nvarchar(max),
@Employer nvarchar(max)
as
begin
update tblWorkData set EmployerName=@EmployerName where EmployerName=@Employer
end
我更改了where
子句,我想这是你的本意。就像以前一样,它实际上什么也没做
另一方面,您可能希望了解如何正确调用过程,以及如何以不易受SQL注入攻击的方式添加参数 您必须连接到数据库才能执行sql语句:
string employer = Session["Employer"].ToString();
// assume connectionString is a valid connection string
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandText = "updatework";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@EmployerName", employer);
command.ExecuteNonQuery();
}
update-tblWorkData set-EmployerName=@EmployerName,其中EmployerName=@EmployerName
没有任何意义,也不做任何事情。