主表与从表同时维护
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
if(myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
SqlCommand myCommand = myConnection.CreateCommand();
SqlTransaction myTrans;
// Start a local transaction
myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted,"SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
myCommand.Connection = myConnection;
myCommand.Transaction = myTrans;
string uname = txt_name.Text;
int uage = (Convert.ToInt32(drp_age.SelectedValue.ToString()));
string usex = drp_sex.SelectedValue.ToString();
string uaddress = txt_address.Text;
uaddress = formatString(uaddress);
string uip = Request.UserHostAddress;
try
{
string sheet_id = Guid.NewGuid().ToString("D");
myCommand.CommandText = "Insert into QuerySheet(QuerySheetID, QuerySheetTemplateID, UserName, Age, Sex, Address, IP)VALUES ('"+sheet_id +"', '"+querytemplateID+"','"+uname+"','"+uage+"','"+usex+"','"+uaddress+"','"+uip+"')";
myCommand.ExecuteNonQuery();
StringBuilder sbSql = new StringBuilder();
for(int iii =0;iii<arldetail1.Count;iii++)
{
string result = arldetail1[iii].ToString();
string result_content = arldetail[iii].ToString();
string newguid = Guid.NewGuid().ToString("B");
if(myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
sbSql.Append("Insert into queryResult (ResultID, DetailItemID,QuerySheetID, Result) VALUES ('"+newguid+"', '"+result_content+"','"+sheet_id+"', '"+result+"')");
sbSql.Append(" ");
}
myCommand.CommandText = sbSql.ToString();
myCommand.ExecuteNonQuery();
myTrans.Commit();
}
catch
{
myTrans.Rollback();
RegisterStartupScript("alert","<script language=javascript>window.alert('提交失败!');</script>");
return;
if(myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
SqlCommand myCommand = myConnection.CreateCommand();
SqlTransaction myTrans;
// Start a local transaction
myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted,"SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
myCommand.Connection = myConnection;
myCommand.Transaction = myTrans;
string uname = txt_name.Text;
int uage = (Convert.ToInt32(drp_age.SelectedValue.ToString()));
string usex = drp_sex.SelectedValue.ToString();
string uaddress = txt_address.Text;
uaddress = formatString(uaddress);
string uip = Request.UserHostAddress;
try
{
string sheet_id = Guid.NewGuid().ToString("D");
myCommand.CommandText = "Insert into QuerySheet(QuerySheetID, QuerySheetTemplateID, UserName, Age, Sex, Address, IP)VALUES ('"+sheet_id +"', '"+querytemplateID+"','"+uname+"','"+uage+"','"+usex+"','"+uaddress+"','"+uip+"')";
myCommand.ExecuteNonQuery();
StringBuilder sbSql = new StringBuilder();
for(int iii =0;iii<arldetail1.Count;iii++)
{
string result = arldetail1[iii].ToString();
string result_content = arldetail[iii].ToString();
string newguid = Guid.NewGuid().ToString("B");
if(myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
sbSql.Append("Insert into queryResult (ResultID, DetailItemID,QuerySheetID, Result) VALUES ('"+newguid+"', '"+result_content+"','"+sheet_id+"', '"+result+"')");
sbSql.Append(" ");
}
myCommand.CommandText = sbSql.ToString();
myCommand.ExecuteNonQuery();
myTrans.Commit();
}
catch
{
myTrans.Rollback();
RegisterStartupScript("alert","<script language=javascript>window.alert('提交失败!');</script>");
return;