ASP.NET 2.0如何使用存储过程执行数据库更新表记录
小弟在数据库中已经创建有一个存储过程,用于更新表中的记录,由于存储过程中使用参数太多,在执行时出现异常。存储过程创建的SQL命令如下:
create procedure update_user
(@username varchar(30),@usernichen varchar(30),@xingming varchar(30),@sex varchar (2),@birthdate smalldatetime,@useraddress varchar(150),@youbian varchar(6),@dianhua varchar(12),@shouji varchar(20),@email varchar(50),@chartype varchar(6),@charID varchar(30),@tishi varchar(30),@tishidaan varchar(50) )
as
update userinformationtable set usernichen=@usernichen,xingming=@xingming,sex=@sex,birthdate=@birthdate,useraddress=@useraddress,youbian=@youbian,dianhua=@dianhua,shouji=@shouji,email=@email,chartype=@chartype,charID=@charID,tishi=@tishi,tishidaan=@tishidaan
where username=@username
GO
aspx.cs文件代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class user_userset : System.Web.UI.Page
{
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//如果页面是第一次加载时执行以下语句
{
if (Session["username"] == null)
{
Response.Redirect("../Default.aspx");
}
}
this.bind();
}
//绑定数据方法
protected void bind()
{
try
{
string sqlstr = "select usernichen,xingming,birthdate,useraddress,youbian,dianhua,shouji,email,chartype,charID,tishi,tishidaan,sex from userinformationtable where username='" + Convert.ToString(Session["username"]) + "'";
commomclass commc = new commomclass();
commc.selectdsfun(sqlstr, ds);
TextBox1.Text = Convert.ToString(ds.Tables[0].Rows[0][0]);
TextBox2.Text = Convert.ToString(ds.Tables[0].Rows[0][1]);
TextBox3.Text = Convert.ToString(ds.Tables[0].Rows[0][2]);
TextBox4.Text = Convert.ToString(ds.Tables[0].Rows[0][3]);
TextBox5.Text = Convert.ToString(ds.Tables[0].Rows[0][4]);
TextBox6.Text = Convert.ToString(ds.Tables[0].Rows[0][5]);
TextBox7.Text = Convert.ToString(ds.Tables[0].Rows[0][6]);
TextBox8.Text = Convert.ToString(ds.Tables[0].Rows[0][7]);
TextBox10.Text = Convert.ToString(ds.Tables[0].Rows[0][9]);
TextBox11.Text = Convert.ToString(ds.Tables[0].Rows[0][10]);
TextBox12.Text = Convert.ToString(ds.Tables[0].Rows[0][11]);
string sex = Convert.ToString(ds.Tables[0].Rows[0][12]);
if (sex == "男")
{
RadioButton1.Checked = true;
}
else
{
RadioButton2.Checked = true;
}
}
catch
{
Response.Redirect("../Default.aspx");
}
}
//调用存储过程执行数据更新
protected void upuser()
{
string sexx ="男";
if (RadioButton1.Checked)
{
sexx = RadioButton1.Text;
}
else if (RadioButton2.Checked)
{
sexx = RadioButton2.Text;
}
string str = commomclass.blackstr();
SqlConnection sqlconn = new SqlConnection(str);
sqlconn.Open();
try
{
SqlDataAdapter sqlda = new SqlDataAdapter("update_user", sqlconn);
sqlda. = "update_user";
sqlda. = CommandType.StoredProcedure;
SqlParameter prams1 = new SqlParameter("@username", SqlDbType.VarChar, 30);
SqlParameter prams2 = new SqlParameter("@usernichen", SqlDbType.VarChar, 30);
SqlParameter prams3 = new SqlParameter("@xingming", SqlDbType.VarChar, 30);
SqlParameter prams4 = new SqlParameter("@sex", SqlDbType.VarChar, 2);
SqlParameter prams5 = new SqlParameter("@birthdate", SqlDbType.SmallDateTime);
SqlParameter prams6 = new SqlParameter("@useraddress", SqlDbType.VarChar, 150);
SqlParameter prams7 = new SqlParameter("@youbian", SqlDbType.VarChar, 6);
SqlParameter prams8 = new SqlParameter("@dianhua", SqlDbType.VarChar, 12);
SqlParameter prams9 = new SqlParameter("@shouji", SqlDbType.VarChar, 20);
SqlParameter prams10 = new SqlParameter("@email", SqlDbType.VarChar, 50);
SqlParameter prams11 = new SqlParameter("@chartype", SqlDbType.VarChar, 6);
SqlParameter prams12 = new SqlParameter("@charID", SqlDbType.VarChar, 30);
SqlParameter prams13 = new SqlParameter("@tishi", SqlDbType.VarChar, 30);
SqlParameter prams14 = new SqlParameter("@tishidaan", SqlDbType.VarChar, 50);
prams1.Value = Convert.ToString(Session["username"]);
prams2.Value = TextBox1.Text;
prams3.Value = TextBox2.Text;
prams4.Value = sexx;
prams5.Value = TextBox3.Text;
prams6.Value = TextBox4.Text;
prams7.Value = TextBox5.Text;
prams8.Value = TextBox6.Text;
prams9.Value = TextBox7.Text;
prams10.Value = TextBox8.Text;
prams11.Value = DropDownList1.Text;
prams12.Value = TextBox10.Text;
prams13.Value = TextBox11.Text;
prams14.Value = TextBox12.Text;
sqlda.UpdateCommand.Parameters.Add(prams1);
sqlda.UpdateCommand.Parameters.Add(prams2);
sqlda.UpdateCommand.Parameters.Add(prams3);
sqlda.UpdateCommand.Parameters.Add(prams4);
sqlda.UpdateCommand.Parameters.Add(prams4);
sqlda.UpdateCommand.Parameters.Add(prams6);
sqlda.UpdateCommand.Parameters.Add(prams7);
sqlda.UpdateCommand.Parameters.Add(prams8);
sqlda.UpdateCommand.Parameters.Add(prams9);
sqlda.UpdateCommand.Parameters.Add(prams10);
sqlda.UpdateCommand.Parameters.Add(prams11);
sqlda.UpdateCommand.Parameters.Add(prams12);
sqlda.UpdateCommand.Parameters.Add(prams13);
sqlda.UpdateCommand.Parameters.Add(prams14);
sqlda.Fill(ds);
Response.Write("<script lanuage=javascript>alert('修改成功!');</script>");
}
catch
{
Response.Write("<script lanuage=javascript>alert('修改失败!');</script>");
}
finally
{
sqlconn.Close();
}
}
//执行更新操作按钮单击事件
protected void Button1_Click(object sender, EventArgs e)
{
if(TextBox1.Text.Trim()==""||TextBox2.Text.Trim()==""||TextBox8.Text.Trim()==""||TextBox10.Text.Trim()==""||TextBox11.Text.Trim()==""||TextBox12.Text.Trim()=="")
{
Response.Write("<script lanuage=javascript>alert('请填写完整必填项的信息! ');</script>");
}
else
{
this.upuser();
this.bind();
}
}
}
我点击确定按钮执行的结果总是失败,希望高手指点指点,发一些源码看看也可以。在此小弟先谢谢各位大哥位了。