c#调用存储过程实现批量增加和修改数据

1 例如当我在编辑表格数据,一次编辑了多行需要保存到数据库时,就需要判断数据库中是否已经存在,存在则修改,不存在则新增一条或多条数据,即所谓批量增加或者跟新数据。

    首先需要构建数据包,把要添加或者跟新的数据构建成一个datatable,例如我数据库中的表结构是这样

   

CREATE TABLE [dbo].[FlagDescription]
(
[FundId] [CHAR(10)] not null,
[SurveyType] [tinyint] not null,
[DefectReason] TINYINT null,
[Flag] TINYINT null,
[LastUpdate] smalldatetime null,
[UserId] [INT] NULL
CONSTRAINT [PK_FlagDescription] PRIMARY KEY CLUSTERED
(
[FundId] ASC,
[SurveyType] ASC
)ON [PortfolioCoreData_Data]
)ON [PortfolioCoreData_Data]

GO

 

我先要构建一个datatable,将表格中变动的每一行数据添加到这个datatable中

private static DataTable BuildDatatable()
{
DataTable dtFlag = new DataTable();
if (!dtFlag.Columns.Contains("FundId"))
{
dtFlag.Columns.Add("FundId", typeof(string));
}

if (!dtFlag.Columns.Contains("SurveyType"))
{
dtFlag.Columns.Add("SurveyType", typeof(int));
}

if (!dtFlag.Columns.Contains("Flag"))
{
dtFlag.Columns.Add("Flag", typeof(int));
}

if (!dtFlag.Columns.Contains("DefectReason"))
{
dtFlag.Columns.Add("DefectReason", typeof(int));
}

if (!dtFlag.Columns.Contains("UserId"))
{
dtFlag.Columns.Add("UserId", typeof(int));
}

return dtFlag;
}

然后将datatable转换为xml字符串,并将它作为存储过程的参数传递给存储过程,代码如下:

public static void AddFlagDescription(DataTable dt)
{

string storedProcedure = string.Empty;
storedProcedure = "dbo.addFlagDescriptions";
using (SqlConnection conn = SQLUtility.GetConnection("AcquisitionData", "Editor", -1))
{
SqlCommand cmd = SQLUtility.InitStoredProcedure(storedProcedure, conn);
cmd.Parameters["@p_Xml"].Value = ConvertDataTableToXML(dt);
cmd.ExecuteNonQuery();
}
}

public static string ConvertDataTableToXML(DataTable dtKeys)
{
dtKeys.TableName = "Key";
System.IO.StringWriter tw = new System.IO.StringWriter();
dtKeys.WriteXml(tw);
return tw.ToString();
}

 

最后编写存储过程完成批量增加和跟新的功能:

USE [AcquisitionData]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[addFlagDescriptions]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[addFlagDescriptions]
GO
CREATE PROCEDURE dbo.addFlagDescriptions
@p_Xml XML
AS
SET NOCOUNT ON

BEGIN

;WITH b AS
(
SELECT
QueryExpression.Criteria.value('FundId[1]', 'CHAR(10)') AS FundId,
QueryExpression.Criteria.value('SurveyType[1]', 'TINYINT') AS SurveyType,
QueryExpression.Criteria.value('Flag[1]', 'TINYINT') AS Flag,
QueryExpression.Criteria.value('DefectReason[1]', 'TINYINT') AS DefectReason,
QueryExpression.Criteria.value('UserId[1]', 'INT') AS UserId
FROM @p_Xml.nodes('/DocumentElement/Key') QueryExpression(Criteria)
)

-- Update
MERGE INTO AcquisitionData.dbo.FlagDescription a
USING b ON a.FundId=b.FundId and a.SurveyType=b.SurveyType
WHEN MATCHED THEN
UPDATE
SET a.FundId=b.FundId,
a.SurveyType=b.SurveyType,
a.Flag=b.Flag,
a.DefectReason=b.DefectReason,
a.UserId=b.UserId
--INSERT
WHEN NOT MATCHED THEN
INSERT(FundId,
SurveyType,
DefectReason,
Flag,
UserId,
LastUpdate)
VALUES(b.FundId,
b.SurveyType,
b.DefectReason,
b.Flag,
b.UserId,
GETDATE()
);

END

RETURN @@ERROR

GO
GRANT EXECUTE ON [dbo].[addFlagDescriptions] TO [rl_DataUpload]
GO

 

posted @ 2016-09-08 15:41  白羽轻飘  阅读(4697)  评论(0编辑  收藏  举报