C# 从C代码在单个SQL查询中插入多行?
我一次要插入多组数据,比如说10行 我的表格有三列:C# 从C代码在单个SQL查询中插入多行?,c#,asp.net,sql,sql-server-2008,C#,Asp.net,Sql,Sql Server 2008,我一次要插入多组数据,比如说10行 我的表格有三列:PlanId、MomEntryId和CreatedBy 我可以在一条SQL语句中插入全部10行吗 这是我的商店程序。如何在SP中传递10行值。我需要验证PlanId和MomEntryId的值是否大于零 CREATE PROCEDURE SP_Document_Plan_Or_MomEntry_Insert @PlanId int = null, @MomEntryId int = null, @CreatedBy varchar(20) A
PlanId
、MomEntryId
和CreatedBy
我可以在一条SQL语句中插入全部10行吗
这是我的商店程序。如何在SP中传递10行值。我需要验证PlanId
和MomEntryId
的值是否大于零
CREATE PROCEDURE SP_Document_Plan_Or_MomEntry_Insert
@PlanId int = null,
@MomEntryId int = null,
@CreatedBy varchar(20)
AS
BEGIN
if(@PlanId > 0)
begin
Insert into t_Document(PlanId,CreatedBy,CreatedDate)
values
(@PlanId,@CreatedBy,GETDATE())
end
else if (@MomEntryId>0)
begin
Insert into t_Document([MomEntryId],CreatedBy,CreatedDate)
values
(@MomEntryId,@CreatedBy,GETDATE())
end
END
GO
下面是我传递参数的C#代码
cblCheckList.Items.Cast<ListItem>().ToList().ForEach(d =>
{
if(d.Selected)
{
momEntry.AddDocumentDetailForMomEntry(Convert.ToInt32(d.Value), Session["userId"].ToString());
}
});
这是我的商店程序。如何在SP中传递10行值
用于此目的。您可以将DataTable
或对象集合作为参数从C#code传递到定义的表类型
请参阅:我将如何验证我的SP@SaroopTrivedi,请参见,我需要验证如果PlanId>0,则只有PlanId值插入执行其他明智的MomEntryId。两个条目不能在同一个目录上执行page@SaroopTrivedi如果您遵循该链接,则其中有store proc。您可以在那里进行验证。@Habib,我们可以使用WCF以外的适配器吗。您考虑过SqlBulkCopy吗?@Sruti:是的,但同时也需要执行验证。您不能在C端执行验证,单元测试的简易性是免费的:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
/// <summary>
/// Summary description for MOMGateway
/// </summary>
public class MOMGateway
{
private MOMGateway()
{
//
// TODO: Add constructor logic here
//
}
///
/// This method perform insert operation in Document table
///
public static int AddDocumentEntryforMomEntry(int momEntryId,string createdBy)
{
int i = Convert.ToInt32(DBHelper.GetDatabase().ExecuteScalar("SP_Document_Plan_Or_MomEntry_Insert",
null,momEntryId,createdBy));
return i;
}
}