嗨,我刚刚学习 C# 并且正在努力解决一个问题,如果我能得到任何帮助,我将不胜感激。
我的第一个问题是,我有一个 aspx 表单,当您点击保存按钮时,它会将数据插入到 2 个单独的表中,[Transaction 和 TransactionDetails] 两个表都包含定义为 Guid 的 TransID 字段。事务表 Guid 也是主键。我的问题是如何将作为唯一标识符的相同 guid (TransID) 插入两个表中......然后同时(这是我的希望)我有一个需要运行的存储过程,它需要与相同的 Guid一个参数。所以简而言之,我需要在两个表中使用相同的 GUID,并且必须将新创建的 Guid 传递给我的存储过程。
到目前为止,这是我的代码代码,您能帮我解决我需要添加或更改的内容吗?
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection((System.Configuration.ConfigurationManager.ConnectionStrings["NovaConnectionString"].ConnectionString));
sc.Open();
string insertSQL;
insertSQL = "INSERT INTO [Transaction] (";
insertSQL += "TransactionID, FileNumber, TransactionDate, RegistryID, InstrumentID, TransactionTypeID, CompanyID, ";
insertSQL += "InvoiceNo, Unit, Price, DueDiligance, Legal)";
insertSQL += "VALUES (";
insertSQL += "@TransactionID, @FileNumber, @TransactionDate, @RegistryID, @InstrumentID, @TransactionTypeID, @CompanyID, ";
insertSQL += "@InvoiceNo, @Unit, @Price, @DueDiligance, @Legal)";
//string query1 = String.Format(@"Insert Into Transaction (TransactionID, FileNumber, TransactionDate, RegistryID, InstrumentID, TransactionTypeID, CompanyID, InvoiceNo, Unit, Price, DueDiligance, Legal)"
//+ " VALUES (@TransactionID, @FileNumber, @TransactionDate, @RegistryID, @InstrumentID, @TransactionTypeID, @CompanyID, @InvoiceNo, @Unit, @Price, @DueDiligance, @Legal)");
SqlCommand cmd = new SqlCommand(insertSQL, sc);
SqlCommand cmd3 = new SqlCommand("uspProcessPurchasedOffsets", sc);
cmd3.CommandType = CommandType.StoredProcedure;
//Add the parameters.
cmd.Parameters.AddWithValue("@TransactionID", Guid.NewGuid());
cmd.Parameters.AddWithValue("@FileNumber", txtFileNumber.Text);
cmd.Parameters.AddWithValue("@TransactionDate", txtTransactionCreated.Text);
cmd.Parameters.AddWithValue("@RegistryID", ddlRegistry.Text);
cmd.Parameters.AddWithValue("@InstrumentID", ddlInstrument.Text);
cmd.Parameters.AddWithValue("@TransactionTypeID", txtTransactionTypeID.Text);
cmd.Parameters.AddWithValue("@CompanyID", ddlCompany.Text);
cmd.Parameters.AddWithValue("@InvoiceNo", txtInvoiceNo.Text);
cmd.Parameters.AddWithValue("@Unit", txtTotalVolume.Text);
cmd.Parameters.AddWithValue("@Price", txtPrice.Text);
cmd.Parameters.AddWithValue("@DueDiligance", txtDueDiligance.Text);
cmd.Parameters.AddWithValue("@Legal", txtLegal.Text);
//Parameter for stored Proc
cmd3.Parameters.Add("@TransactionID", SqlDbType.UniqueIdentifier);
cmd.ExecuteNonQuery();
string insertSQL2;
insertSQL2 = "INSERT INTO [TransactionDetails] (StartSerialNumber,VintageYear,Units)";
insertSQL2 += "VALUES (@StartSerialNumber, @VintageYear, @Units)";
SqlCommand cmd1 = new SqlCommand(insertSQL2, sc);
//Add the parameters.
cmd1.Parameters.AddWithValue("@StartSerialNumber", txtStartSerial.Text);
cmd1.Parameters.AddWithValue("@VintageYear", txtVintage.Text);
cmd1.Parameters.AddWithValue("@Units", txtVolume.Text);
cmd1.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
}
}