如何将guid作为存储过程的参数传递

时间:2013-03-24 22:32:11

标签: c#-4.0

嗨,我只是在学习C#,我正在努力解决问题,并希望得到任何帮助。

我的第一个问题是,我有一个aspx表单,当你点击保存按钮时,它将数据插入到2个单独的表中,[Transaction和TransactionDetails]两个表都包含定义为Guids的TransID字段。事务表Guid也是主键。我的问题是我如何在两个表中插入相同的guid(TransID),​​这是一个uniqueidentifier ....然后同时(这是我的希望)我有一个需要运行的存储过程,需要相同的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();

}

}

1 个答案:

答案 0 :(得分:0)

我认为您可以将GUID存储在适当的中间变量中,然后将相同的变量传递给storedprocedure和commandtext的参数

 Guid g = Guid.NewGuid();
 cmd.Parameters.AddWithValue("@TransactionID", g);

 .....

 cmd3.Parameters.Add("@TransactionID", SqlDbType.UniqueIdentifier, 16 ).Value = g;