第四,首次谈到运用存储过程进行修改和删除。
首先,你要写一个存储过程
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)
{
}
}
主要是在修改中,做的存储过程的调用,不管怎么样,你得会存储过程对不对。