存储过程配合UpdateDaset方法批量插入Dataset数据实现代码
<div class="codetitle"><a style="CURSOR: pointer" data="85270" class="copybut" id="copybut85270" onclick="doCopy('code85270')"> 代码如下:
<div class="codebody" id="code85270">
public bool SaveSMSMessage(SMSBatch smsBatch,DataSet smsMessages)
{
//using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Suppress))
//{
foreach (DataRow row in smsMessages.Tables[0].Rows)
row.SetModified();
sqlDatabase db = new
sqlDatabase(this.ConsultantsConnString);
string
sqlCmd = "EXEC cn.SMSSaveSMSMessage @SMSBatchID,@SMSTypeID,@SubsidiaryID,@ContactID,@Message,@PhoneNumber";
using (
sqlCommand cmd = db.Get
sqlStringCommand(
sqlCmd) as
sqlCommand)
{
cmd.CommandTimeout = 600;
cmd.Parameters.AddWithValue("@SMSBatchID",smsBatch.SMSBatchID);
cmd.Parameters.AddWithValue("@SMSTypeID",smsBatch.SMSType.SMSTypeID);
cmd.Parameters.AddWithValue("@SubsidiaryID",smsBatch.SMSType.SubsidiaryID);
db.AddInParameter(cmd,"@ContactID",DbType.Int64,"ContactID",DataRowVersion.Current);
db.AddInParameter(cmd,"@Message",DbType.String,"Message",DataRowVersion.Current);
if (smsMessages.Tables[0].Columns.Contains("PhoneNumber"))
{
db.AddInParameter(cmd,"@PhoneNumber","PhoneNumber",DataRowVersion.Current);
}
else
{
db.AddInParameter(cmd,DBNull.Value);
}
int rowsEffected = db.UpdateDataSet(smsMessages,"SMSMessage",null,cmd,UpdateBehavior.Transactional);
sqlCmd = rowsEffected.ToString();
}
// ts.Complete();
//}
return true;
}