用事务进行批量操作的问题?急!

caoit 2004-07-22 10:36:02
public bool AddOrganizeKPI( DataTable listTable )
{
Open();
SqlTransaction myTrans;
myTrans =this.conn.BeginTransaction();
try
{

foreach(DataRow dr in listTable.Rows )
{
KPIID = HRM.CPublicFn.IntObjectToInt( dr["KPIID"] );
KPIIndex = short.Parse( dr["KPIIndex"].ToString() );
KPIPower = short.Parse( dr["KPIPower"].ToString() );
DestValue = decimal.Parse( dr["DestValue"].ToString() );
RealValue = decimal.Parse( dr["RealValue"].ToString() );
BigType = dr["BigType"].ToString();
SmallType = dr["SmallType"].ToString();
CheckScore = decimal.Parse( dr["CheckScore"].ToString() );
sqlCmd = this.AddOrganizeKPIListCmd();
sqlCmd.Connection = this.conn;
sqlCmd.Transaction = myTrans;
int i = sqlCmd.ExecuteNonQuery();
iRet = (int)(sqlCmd.Parameters["@ReturnValue"].Value);
if ( iRet != 0 )
{
ErrorMsg = sqlCmd.Parameters["@ErrorMsg"].Value.ToString();
myTrans.Rollback();
return false;
}
}
myTrans.Commit();
return true;
}
catch(Exception e)
{
ErrorMsg = e.Message;
try
{
myTrans.Rollback();
}
catch (SqlException sqlexcept)
{
ErrorMsg = sqlexcept.Errors[0].Message;
}
}
finally
{
Close();
}
return false;
}

AddOrganizeKPIListCmd()这是增加操作方法,为什么增加有两次记录了,在没有记录的情况下,可以用修改方法来操作,想了两天了,还没有解决,请指教
...全文
275 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghuixue 2004-08-09
  • 打赏
  • 举报
回复
mark
liuyong_lll 2004-08-09
  • 打赏
  • 举报
回复
看来没有我的西了哦^_^
  • 打赏
  • 举报
回复
to: caoit (lost) 行了吗?
俺解决了,你将两个Sql语句写在一起,一并提交,这样就可以了。如下:
public bool DelCourse(int pID)
{
wisdeconnection conn=new wisdeconnection();
try
{
conn.BeginTransaction();
conn.RunSql("delete from courseware where id= "+pID
+"delete from coursepop where courseid="+pID);
conn.CommitTransaction();
return true;
}
catch(Exception er)
{
conn.Rollback();
throw new Exception(er.Message);
}
finally
{
conn.Dispose();
}

}
husiwei 2004-08-09
  • 打赏
  • 举报
回复
up
  • 打赏
  • 举报
回复
本人也在寻找方法中,不过建议你看看sqlhelper,楼主共同交流吧。
andrawsky 2004-07-22
  • 打赏
  • 举报
回复
SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
myConnection.Open();

// Start a local transaction.
SqlTransaction myTrans = myConnection.BeginTransaction();

// Enlist the command in the current transaction.
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.Transaction = myTrans;

try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}

Console.WriteLine("An exception of type " + e.GetType() +
"was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}

Seeko0 2004-07-22
  • 打赏
  • 举报
回复
AddOrganizeKPI 是不是被执行了两次? 根据数据量大小,可以考虑string sqlstr +=" insert ; ";生成完sqlstr语句后,再执行事务控制,插入数据库
caoit 2004-07-22
  • 打赏
  • 举报
回复
up
caoit 2004-07-22
  • 打赏
  • 举报
回复
就是加了两个table到数据表里面了。

在没有任何记录的情况下,可以用修改记录,来增加一条,也就是说在执行事务之前,table已增加一次。
goody9807 2004-07-22
  • 打赏
  • 举报
回复
SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
myConnection.Open();

// Start a local transaction.
SqlTransaction myTrans = myConnection.BeginTransaction();

// Enlist the command in the current transaction.
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.Transaction = myTrans;

try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}

Console.WriteLine("An exception of type " + e.GetType() +
"was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}
brightheroes 2004-07-22
  • 打赏
  • 举报
回复
Sorry没有看清楚
楼主的问题

AddOrganizeKPIListCmd()这是增加操作方法,为什么增加有两次记录了,在没有记录的情况下,可以用修改方法来操作,想了两天了,还没有解决,请指教
---什么意思?
caoit 2004-07-22
  • 打赏
  • 举报
回复
我的事务没错吧,
brightheroes 2004-07-22
  • 打赏
  • 举报
回复
楼主想根据ExecuteNonQuery()来控制是否回滚
请参考

http://community.csdn.net/Expert/topic/2995/2995537.xml?temp=.2118952
jiezhi 2004-07-22
  • 打赏
  • 举报
回复
你的事物写得不对。
string sql = "update ...";
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DSN"].ToString());
conn.Open();
SqlTransaction sqlTran = conn.BeginTransaction();
SqlCommand cmd = conn.CreateCommand();
cmd.Transaction = sqlTran;
try
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

sql = "update ...";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

sqlTran.Commit();
}
catch(Exception e1)
{
try
{
sqlTran.Rollback();
}
catch(SqlException ex)
{
throw ex;
}
throw e1;
}
finally
{
conn.Close();
}
caoit 2004-07-22
  • 打赏
  • 举报
回复
有人解决吗?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧