| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2277 人关注过本帖
标题:ASP.NET 2.0如何使用存储过程执行数据库更新表记录
只看楼主 加入收藏
wskiawv
Rank: 1
等 级:新手上路
帖 子:42
专家分:5
注 册:2008-6-28
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:3 
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();
        }      
    }  
}


我点击确定按钮执行的结果总是失败,希望高手指点指点,发一些源码看看也可以。在此小弟先谢谢各位大哥位了。
搜索更多相关主题的帖子: 数据库 记录 ASP NET 
2010-01-31 23:51
xuliang1265
Rank: 2
等 级:论坛游民
帖 子:7
专家分:23
注 册:2010-2-7
收藏
得分:20 
我觉得你到网上下一个SqlHelper看一下。

http://shop34553454./
2010-02-07 17:29
xuliang1265
Rank: 2
等 级:论坛游民
帖 子:7
专家分:23
注 册:2010-2-7
收藏
得分:0 
string sqlConnectionString = @"Data Source=LocalHost\Sql2000;Initial Catalog=Test;User ID=sa;password=woaiylj";
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            SqlCommand sqlCommand = new SqlCommand();
             = CommandType.StoredProcedure;
             = "Update_User";
            sqlCommand.Parameters.Add("@UserName", SqlDbType.VarChar);
            sqlCommand.Parameters.Add("@NickName", SqlDbType.VarChar);

            sqlCommand.Parameters[0].Value = "1";
            sqlCommand.Parameters[1].Value = "ddd";

            sqlCommand.Connection = conn;
            try
            {
                conn.Open();
                sqlCommand.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }

http://shop34553454./
2010-02-07 17:53
wskiawv
Rank: 1
等 级:新手上路
帖 子:42
专家分:5
注 册:2008-6-28
收藏
得分:0 
没有啊,我已经通过一个公共类来连接数据库了。我在web.config文件中配置连接数据库的字符串,我在其他页中使用更新操作数据库中的表都没有问题 。
2010-02-23 15:44
快速回复:ASP.NET 2.0如何使用存储过程执行数据库更新表记录
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.049626 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved