文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>c# 事务处理

c# 事务处理

时间:2011-04-27  来源:鱼不爱水

 1 创建一个connection

 string connstr = ConfigurationManager.ConnectionStrings["default"].ConnectionString;
 SqlConnection sqlconn = new SqlConnection(connstr);

 2 创建一个command 连接到 connection上

 SqlCommand cmd = new SqlCommand();
 cmd.Connection = sqlconn;
 3 创建一个 transaction 当 connection 打开的时候将事务和connection 绑定 然后将transaction也和command绑定

 SqlTransaction sqltran;
 
 sqlconn.Open();
 sqltran = sqlconn.BeginTransaction();
 cmd.Transaction = sqltran;

 4 处理

try
                        {
                            StreamReader sr = new StreamReader(postedFile.InputStream);
                            string headLine = sr.ReadLine();
                            headLine = headLine.Replace(";", "],[");
                            headLine = "[" + headLine + "]";

                            int lineNumber = 1;

                            while (!sr.EndOfStream)
                            {
                                string saleDataLine = sr.ReadLine();
                                //rdLine = rdLine.Replace("N/A", "null");
                                saleDataLine = saleDataLine.Replace("'", "''");
                                saleDataLine = saleDataLine.Replace(";", "','");
                                string command = "insert into SalesUploadTest (" + headLine + ") values ('" + saleDataLine + "')";
                                cmd.CommandText = command;
                                cmd.CommandType = CommandType.Text;
                                cmd.ExecuteNonQuery();
                                lineNumber++;
                            }
                            sqltran.Commit();

                        }
                        catch (Exception)
                        {
                            sqltran.Rollback();
                        }
                        finally
                        {
                            cmd.Dispose();
                            sqlconn.Close();
              }

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载