在部分更新存储过程中,
需要只更新某一列而保持其他列的更改,既实现只给需要更改的列赋值,不需更改的列不用赋值,关键在于定义存储过程参数时为参数指定默认值 null;示例如下。
==================================
ALTER PROCEDURE dbo.LogInfoUpdate
@LogID int,
@LogName nvarchar(50) = null,
@LogPassWd nvarchar(50) = null,
@LogPopedom nvarchar(50) = null
AS
declare @Name nvarchar(50),@PassWd nvarchar(50),@Popedom nvarchar(50)
select @Name=LogName,@PassWd=LogPassWd,@Popedom=LogPopedom from LogInfo where LogID = @LogID
if(@LogName is null)
begin
select @LogName = @Name
end
if(@LogPassWd is null)
begin
select @LogPassWd = @PassWd
end
if(@LogPopedom is null)
begin
select @LogPopedom = @Popedom
end
update LogInfo set LogName = @LogName ,LogPassWd=@LogPassWd, LogPopedom=@LogPopedom where LogID=@LogID
RETURN
================================================
调用示例:
================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MyUpdate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string strConn = @"Data Source=zouqi\sqlexpress;Initial Catalog=myDBTest;Integrated Security=True;Pooling=False";
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(strConn))
{
conn.Open();
SqlCommand cmd = new SqlCommand("LogInfoUpdate", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@LogID", SqlDbType.Int, 4, "LogID").Value = 1;
cmd.Parameters.Add("@LogName", SqlDbType.NVarChar, 50, "LogName").Value = "只修改用户名";
cmd.ExecuteNonQuery();
Form1_Load(null, null);
}
}
private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from loginfo", conn);
DataSet ds = new DataSet();
adapter.Fill(ds, "logInfo");
this.dataGridView1.DataSource = ds.Tables[0];
}
}
}
}
需要只更新某一列而保持其他列的更改,既实现只给需要更改的列赋值,不需更改的列不用赋值,关键在于定义存储过程参数时为参数指定默认值 null;示例如下。
==================================
ALTER PROCEDURE dbo.LogInfoUpdate
@LogID int,
@LogName nvarchar(50) = null,
@LogPassWd nvarchar(50) = null,
@LogPopedom nvarchar(50) = null
AS
declare @Name nvarchar(50),@PassWd nvarchar(50),@Popedom nvarchar(50)
select @Name=LogName,@PassWd=LogPassWd,@Popedom=LogPopedom from LogInfo where LogID = @LogID
if(@LogName is null)
begin
select @LogName = @Name
end
if(@LogPassWd is null)
begin
select @LogPassWd = @PassWd
end
if(@LogPopedom is null)
begin
select @LogPopedom = @Popedom
end
update LogInfo set LogName = @LogName ,LogPassWd=@LogPassWd, LogPopedom=@LogPopedom where LogID=@LogID
RETURN
================================================
调用示例:
================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MyUpdate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string strConn = @"Data Source=zouqi\sqlexpress;Initial Catalog=myDBTest;Integrated Security=True;Pooling=False";
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(strConn))
{
conn.Open();
SqlCommand cmd = new SqlCommand("LogInfoUpdate", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@LogID", SqlDbType.Int, 4, "LogID").Value = 1;
cmd.Parameters.Add("@LogName", SqlDbType.NVarChar, 50, "LogName").Value = "只修改用户名";
cmd.ExecuteNonQuery();
Form1_Load(null, null);
}
}
private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from loginfo", conn);
DataSet ds = new DataSet();
adapter.Fill(ds, "logInfo");
this.dataGridView1.DataSource = ds.Tables[0];
}
}
}
}