简单-三层-存储过程-增删改<四>

本文详细介绍了如何使用存储过程在数据库中更新和删除登录用户信息,包括创建存储过程、前台调用及关键代码实现,旨在提供实际操作指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第四,首次谈到运用存储过程进行修改和删除。

首先,你要写一个存储过程

create Procedure [dbo].[Updatelogin_User]
(
@txtuser nvarchar(10),
@txtpassword nvarchar(20)

)
AS

UPDATE login_user

SET
txtpassword= @txtpassword

WHERE
txtuser = @txtuser

前台调用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Xml;

public partial class index : System.Web.UI.Page
{
public static string aa;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dvDataBind();
}
}
private void dvDataBind()
{
//SqlConnection conn = new SqlConnection("server=.;database=cs;uid=sa;pwd=");
//SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString());
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Server.MapPath("XMLFile.xml"));
//SqlConnection conn = new SqlConnection(xDoc.SelectSingleNode("PrintSetting/SQL_CONN").InnerText);
XmlNodeList nodes1 = xDoc.GetElementsByTagName("PrintSettings");
foreach (XmlNode node1 in nodes1) //第一层
{
//TextBox1.Text = node1.ToString();
if (node1.Attributes["name"].Value == "zong")
{
XmlNodeList nodes2 = node1.ChildNodes;

foreach (XmlNode node2 in nodes2)//第二层nodes1
{
if (node2.Attributes["name"].Value == "1")
{
aa = node2["SQL_CONN"].InnerText;
//TextBox1.Text = aa;
}
}
}
}
SqlConnection conn = new SqlConnection(aa);
conn.Open();
string sql_string = "select * from login_user";
SqlDataAdapter da = new SqlDataAdapter(sql_string, conn);
DataSet ds = new DataSet();
da.Fill(ds, "login_user");
GridView1.DataSource = ds;
GridView1.DataBind();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
dvDataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txt_temp_description = (TextBox)GridView1.Rows[e.RowIndex].FindControl("textbox1");
SqlConnection conn = new SqlConnection("server=.;database=cs;uid=sa;pwd=");
conn.Open();
//SqlCommand cmd = new SqlCommand("update login_user set txtpassword='" + txt_temp_description.Text + "' where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'", conn);
SqlCommand cmd = new SqlCommand("updatelogin_user",conn);
//调用存储过程名
cmd.CommandType = CommandType.StoredProcedure;
//设置存储过程的参数值,其中@id 为存储过程的参数.
SqlParameter txtuser = cmd.Parameters.Add("@txtuser", SqlDbType.NVarChar);
txtuser.Value = ((Label)GridView1.Rows[e.RowIndex].FindControl("Label2")).Text;
SqlParameter txtpassword = cmd.Parameters.Add("@txtpassword", SqlDbType.NVarChar);
txtpassword.Value = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("textbox1")).Text;
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
dvDataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
dvDataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from login_user where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlConnection conn = new SqlConnection("server=.;database=cs;uid=sa;pwd=");
conn.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conn);
cmd.ExecuteNonQuery();
dvDataBind();

}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{



}
}

主要是在修改中,做的存储过程的调用,不管怎么样,你得会存储过程对不对。

转载于:https://www.cnblogs.com/daywrite/archive/2011/12/27/2303281.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值