常规方式转存储过程的问题!
我做的插入数据的代码,以前使用的常规方式,现改为使用存储过程,不知哪儿有问题!请大家帮忙看看!
protected void BtnLW_Click(object sender, EventArgs e)
{
string Lw_type = this.DropDownType.SelectedValue.ToString();
string Lw_Title = this.TxtTitle.Text.ToString();
string Lw_Content = this.TxtContent.Text.ToString();
string Lw_Name = this.TxtName.Text.ToString();
string Lw_Email = this.TxtEmail.Text.ToString();
string Lw_Phone = this.TxtPhone.Text.ToString();
string Lw_Address = this.TxtAddress.Text.ToString();
string Lw_zip = this.TxtZipCode.Text.ToString();
string newsid = Request.QueryString[ "id "];
SqlConnection conn = new SqlConnection();
ConnectionStringSettings s = ConfigurationManager.ConnectionStrings[ "Wlw_SqlDataConnectionString1 "];
String connString = s.ConnectionString;
conn.ConnectionString = s.ConnectionString;
try
{
//用常规方式,可以正常运行:
//string sql = string.Format( "insert into LeaveWord(lwType,lwTitle,lwContent,lwName,lwEmail,lwPhone,lwAddress,lwZip)values( '{0} ', '{1} ', '{2} ', '{3} ', '{4} ', '{5} ', '{6} ', '{7} ') ", Lw_type, Lw_Title, Lw_Content, Lw_Name, Lw_Email, Lw_Phone, Lw_Address, Lw_zip);
//用存储过程,总不能插入数据,提示“数据更新失败”:
string sql = string.Format( "exec LeaveWrodProc {0},{1},{2},{3},{4},{5},{6},{7} ", Lw_type, Lw_Title, Lw_Content, Lw_Name, Lw_Email, Lw_Phone, Lw_Address, Lw_zip);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
conn.Open();
command.ExecuteNonQuery();
Response.Write( "留言成功! ");
}
catch
{
Response.Write( "数据更新失败 ");
}
conn.Close();
}
以下是SQL存储过程,在SQL内可以测试通过的。
ALTER PROCEDURE dbo.LeaveWordProc
@Lw_Type int,
@Lw_Title nvarchar(80),
@Lw_Content ntext,
@Lw_Name nvarchar(20),
@Lw_Email nvarchar(30),
@Lw_Phone nvarchar (30),
@Lw_Address nvarchar(80),
@Lw_zip nvarchar(8)
AS
insert into LeaveWord(lwType,lwTitle,lwContent,lwName,lwEmail,lwPhone,lwAddress,lwZip)values(@Lw_Type,@Lw_Title,@Lw_Content,
@Lw_Name,@Lw_Email,@Lw_Phone,@Lw_Address,@Lw_zip)
RETURN
[解决办法]
更改下commandtype
[解决办法]
方法有很多种 ,希望楼主要对 ADO.NET 操作要好熟悉。并且现在有好多开源的框架、、
[解决办法]
少了单 '号
[解决办法]
如果是char类型析
LeaveWrodProc '{0} ', '{1} ' ...........
[解决办法]
估计象楼上讲的
string sql = string.Format( "exec LeaveWrodProc {0}, '{1} ', '{2} ', '{3} ', '{4} ', '{5} ', '{6} ', '{7} ' ", Lw_type, Lw_Title, Lw_Content, Lw_Name, Lw_Email, Lw_Phone, Lw_Address, Lw_zip);