原文连接: https://bbs.csdn.net/topics/390871995
现有2个以上数据库服务,要保证他们间的数据一致。
现在楼主这么写 是否妥当? 会有什么问题吗?
private void btn_submit_Click(object sender, System.EventArgs e)
{
string strconn = System.Configuration.ConfigurationManager.AppSettings["dbconnect"];
SqlConnection cnn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand();
SqlTransaction transaction = null;
string strconn2 = System.Configuration.ConfigurationManager.AppSettings["dbconnect2"];
SqlConnection cnn2 = new SqlConnection(strconn2);
SqlCommand cmd2 = new SqlCommand();
SqlTransaction transaction2 = null;
try
{
cnn.Open();
cnn2.Open();
string strSql = "insert into shop (code, name) values('" + lbcode.Text.Trim() + "','" + TxtName.Text + "')";
// 先插入A库
transaction = cnn.BeginTransaction();
cmd.Transaction = transaction;
cmd.Connection = cnn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
cmd.ExecuteNonQuery();
// 后插入B库
transaction2 = cnn2.BeginTransaction();
cmd2.Transaction = transaction2;
cmd2.Connection = cnn2;
cmd2.CommandType = CommandType.Text;
cmd2.CommandText = strSql;
cmd2.ExecuteNonQuery();
// 提交事务
transaction.Commit();
transaction2.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
transaction2.Rollback();
}
finally
{
cnn.Close();
cnn2.Close();
}
}
本来楼主想用建立链接服务器的方法,在数据库中通过存储过程来同步数据。
可是阿里云的RDS不支持连接服务器,同时也不提供SA的权限。
除了上面同时写2个库的办法,不知道有何高人还有其他的建议?
现在楼主这么写 是否妥当? 会有什么问题吗?
private void btn_submit_Click(object sender, System.EventArgs e)
{
string strconn = System.Configuration.ConfigurationManager.AppSettings["dbconnect"];
SqlConnection cnn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand();
SqlTransaction transaction = null;
string strconn2 = System.Configuration.ConfigurationManager.AppSettings["dbconnect2"];
SqlConnection cnn2 = new SqlConnection(strconn2);
SqlCommand cmd2 = new SqlCommand();
SqlTransaction transaction2 = null;
try
{
cnn.Open();
cnn2.Open();
string strSql = "insert into shop (code, name) values('" + lbcode.Text.Trim() + "','" + TxtName.Text + "')";
// 先插入A库
transaction = cnn.BeginTransaction();
cmd.Transaction = transaction;
cmd.Connection = cnn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
cmd.ExecuteNonQuery();
// 后插入B库
transaction2 = cnn2.BeginTransaction();
cmd2.Transaction = transaction2;
cmd2.Connection = cnn2;
cmd2.CommandType = CommandType.Text;
cmd2.CommandText = strSql;
cmd2.ExecuteNonQuery();
// 提交事务
transaction.Commit();
transaction2.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
transaction2.Rollback();
}
finally
{
cnn.Close();
cnn2.Close();
}
}
本来楼主想用建立链接服务器的方法,在数据库中通过存储过程来同步数据。
可是阿里云的RDS不支持连接服务器,同时也不提供SA的权限。
除了上面同时写2个库的办法,不知道有何高人还有其他的建议?
<div marginwidth="0" marginheight="0" scrolling="no" width="100%">
</div>
</div>
<!-- <div class="question_wrap"><span>问题点数:</span><em>分</em></div> -->
</div>