public int ImportData(string SQL, DataSet ds)
{ Transaction originalTransaction = Transaction.Current;
CommittableTransaction transaction = new CommittableTransaction();
int result = 0;
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd;
Cmd = CreateCmd("", Conn);
try
{
Transaction.Current = transaction;
Cmd.CommandText = SQL;
Cmd.ExecuteNonQuery(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string strIn = string.Format("insert into dbo.ashare_test values({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14})",
ds.Tables[0].Rows[i]["gddm"].ToString(),
ds.Tables[0].Rows[i]["gdxm"].ToString(),
ds.Tables[0].Rows[i]["bcrq"].ToString(),
ds.Tables[0].Rows[i]["cjbh"].ToString(),
ds.Tables[0].Rows[i]["gsdm"].ToString(),
ds.Tables[0].Rows[i]["cjsl"].ToString(),
ds.Tables[0].Rows[i]["bcye"].ToString(),
ds.Tables[0].Rows[i]["zqdm"].ToString(),
ds.Tables[0].Rows[i]["sbsj"].ToString(),
ds.Tables[0].Rows[i]["cjsj"].ToString(),
ds.Tables[0].Rows[i]["cjjg"].ToString(),
ds.Tables[0].Rows[i]["cjje"].ToString(),
ds.Tables[0].Rows[i]["sqbh"].ToString(),
ds.Tables[0].Rows[i]["bs"].ToString(),
ds.Tables[0].Rows[i]["mjbh"].ToString()); Cmd.CommandText = strIn; try
{
result = Cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
transaction.Rollback(ex);
throw ex;
}
}
transaction.Commit(); }
catch (Exception ex)
{
transaction.Rollback(ex);
throw ex;
} finally
{
Transaction.Current = originalTransaction;
transaction.Dispose();
} Dispose(Conn);
return result; }
{ Transaction originalTransaction = Transaction.Current;
CommittableTransaction transaction = new CommittableTransaction();
int result = 0;
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd;
Cmd = CreateCmd("", Conn);
try
{
Transaction.Current = transaction;
Cmd.CommandText = SQL;
Cmd.ExecuteNonQuery(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string strIn = string.Format("insert into dbo.ashare_test values({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14})",
ds.Tables[0].Rows[i]["gddm"].ToString(),
ds.Tables[0].Rows[i]["gdxm"].ToString(),
ds.Tables[0].Rows[i]["bcrq"].ToString(),
ds.Tables[0].Rows[i]["cjbh"].ToString(),
ds.Tables[0].Rows[i]["gsdm"].ToString(),
ds.Tables[0].Rows[i]["cjsl"].ToString(),
ds.Tables[0].Rows[i]["bcye"].ToString(),
ds.Tables[0].Rows[i]["zqdm"].ToString(),
ds.Tables[0].Rows[i]["sbsj"].ToString(),
ds.Tables[0].Rows[i]["cjsj"].ToString(),
ds.Tables[0].Rows[i]["cjjg"].ToString(),
ds.Tables[0].Rows[i]["cjje"].ToString(),
ds.Tables[0].Rows[i]["sqbh"].ToString(),
ds.Tables[0].Rows[i]["bs"].ToString(),
ds.Tables[0].Rows[i]["mjbh"].ToString()); Cmd.CommandText = strIn; try
{
result = Cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
transaction.Rollback(ex);
throw ex;
}
}
transaction.Commit(); }
catch (Exception ex)
{
transaction.Rollback(ex);
throw ex;
} finally
{
Transaction.Current = originalTransaction;
transaction.Dispose();
} Dispose(Conn);
return result; }
解决方案 »
- 用c#封装access的连接为com组件供c++调用,但是c#中的dataset等关键字,在c++认为是IUnknownPtr
- 老师说创建一个人民币类..可是怎么包括元角分的数据呢?
- 关于Direct3D Mobile C#的问题
- 怎样用LinkLabel,才能达到这样的效果呢?....
- 在窗口界面想设一个回车执行控键,回车后连接SOL数据库,怎么弄啊。谢谢!
- 问:在MSDN里面的Socket异步通信例子的服务器端代码是不是可以接受多个客户端的连接???
- 代码分享,WinForm中LinkLabel实现关键字高亮
- C# listview控件如何显示text文件中的内容
- 怎样转换图片的类型?
- VS2013新建sql server数据库的问题
- 根据帖子里的楼层号自动发帖,这个能做到吗?
- 求思路,哪位大侠帮忙想想如何求一个整型数组中,第一个不重复的数字。数组值的范围是1-100
{
result = Cmd.ExecuteNonQuery(); //如果此处出错
}
catch (Exception ex)
{
transaction.Rollback(ex);
throw ex;
}
} transaction.Commit(); //这句会不会执行了?
{
result = Cmd.ExecuteNonQuery(); //如果此处出错
}
catch (Exception ex)
{
transaction.Rollback(ex);
throw ex;
}
} transaction.Commit(); //这句会不会执行了?
Cmd.ExecuteNonQuery();Cmd.ExecuteNonQuery();
Cmd.ExecuteNonQuery();runTransaction.Commit();
事务都提交了,后边的都没法执行了
{
result = Cmd.ExecuteNonQuery(); //如果此处出错
}
catch (Exception ex)
{
transaction.Rollback(ex);
flag = false;
break;
throw ex;
}if(flag) //如果for循环中没有出错
transaction.Commit();
())
{操作1if(操作1出错)
return false;
操作2if(操作2出错)
return false;
.......
.....
ts.Complete();
}
{
//方法1
//方法2
scope.Complete();
}