日期:2014-05-18 浏览次数:20917 次
string connectionStringServer = ConfigurationManager.AppSettings["ConnectionStringServer"].ToString(); string connectionStringLocal = ConfigurationManager.AppSettings["ConnectionStringLocal"].ToString(); int returnValue = 0; string strCommand = "insert into [Users](UserName,UserPhone) values(@name,@phone)"; StringWriter writer = new StringWriter(); using (TransactionScope scope = new TransactionScope()) { using (SqlConnection cn1 = new SqlConnection(connectionStringServer)) { try { cn1.Open(); SqlCommand cmd1 = new SqlCommand(strCommand, cn1); cmd1.Parameters.Add("@name", SqlDbType.VarChar).Value = txtName.Text; cmd1.Parameters.Add("@phone", SqlDbType.VarChar).Value = txtPhoneNumber.Text; returnValue = cmd1.ExecuteNonQuery(); writer.WriteLine("插入远程数据库表时影响行数:{0}", returnValue); using (SqlConnection cn2 = new SqlConnection(connectionStringLocal)) { try { cn2.Open(); SqlCommand cmd2 = new SqlCommand(strCommand, cn2); cmd2.Parameters.Add("@name", SqlDbType.VarChar).Value = txtName.Text; cmd2.Parameters.Add("@phone", SqlDbType.VarChar).Value = txtPhoneNumber.Text; returnValue = 0; returnValue = cmd2.ExecuteNonQuery(); writer.WriteLine("插入本地数据库表时影响行数:{0}", returnValue); } catch (Exception ex) { writer.WriteLine("插入本地数据库表时影响行数:{0}", returnValue); writer.WriteLine("插入本地数据库表异常信息:{0}", ex.Message); } } } catch(Exception ex) { writer.WriteLine("插入远程数据库表时影响行数:{0}", returnValue); writer.WriteLine("插入远程数据库表异常信息:{0}", ex.Message); } } scope.Complete(); } if (returnValue > 0) { writer.WriteLine("事务已经提交."); } else { writer.WriteLine("事务已经回滚."); } Response.Write(writer);