存储过程配合UpdateDaset方法批量插入Dataset数据实现代码

前端技术 2023/09/01 MSSQL
复制代码 代码如下:

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.GetSqlStringCommand(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\", DbType.String, \"PhoneNumber\", DataRowVersion.Current);
}
else
{
db.AddInParameter(cmd, \"@PhoneNumber\", DbType.String, DBNull.Value);
}
int rowsEffected = db.UpdateDataSet(smsMessages, \"SMSMessage\", null, cmd, null, UpdateBehavior.Transactional);
sqlCmd = rowsEffected.ToString();
}
// ts.Complete();
//}
return true;
}

本文地址:https://www.stayed.cn/item/2800

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。