如何使用C#将多个数据表传递给存储过程?

如何使用C#将多个数据表传递给存储过程?,c#,datatable,sql-server-2008-r2,C#,Datatable,Sql Server 2008 R2,我在SQLServer2008R2中有两种表类型 transTable带有transDate、transTime、invoiceId、invoiceDesc列 transItemTable带有staffId、price、quantity列 我创建了一个回滚存储过程,并使用这两种表类型作为参数: CREATE PROCEDURE TransInInsert ( @dt AS transTable READONLY, @dt2 AS transItemTable READONLY

我在SQLServer2008R2中有两种表类型

  • transTable
    带有
    transDate、transTime、invoiceId、invoiceDesc
  • transItemTable
    带有
    staffId、price、quantity
我创建了一个回滚存储过程,并使用这两种表类型作为参数:

CREATE PROCEDURE TransInInsert
(
    @dt AS transTable READONLY,
    @dt2 AS transItemTable READONLY
)
AS
    DECLARE @transId int

    BEGIN TRY
    BEGIN TRAN
        INSERT INTO transIn (transDate, transTime, invoiceId, invoiceDesc)
            SELECT 
                transDate, transTime, invoiceId, invoiceDesc 
            FROM @dt

        SET @transId = SCOPE_IDENTITY()

        INSERT INTO transInStaff (transId, staffId, price, quantity)
            SELECT 
                @transId, staffId, price, quantity 
            FROM @dt2
    COMMIT TRAN
    END TRY

    BEGIN CATCH
    /*************************************
    *  Get the Error Message for @@Error
    *************************************/
    IF @@TRANCOUNT > 0 ROLLBACK
    END CATCH
transIn
transInStaff
表格之间的关系如下:

我想使用rollback在这两个表中插入数据,因为有敏感和相关的数据。因此,我用c#编写了一个代码,用DataTable格式生成数据,并准备将它们发送到存储过程中:

DataTable dtTrans = new DataTable();
dtTrans.Columns.AddRange(new DataColumn[4] {
                             new DataColumn("transDate", typeof(string)),
                             new DataColumn("transTime", typeof(string)),
                             new DataColumn("invoiceId", typeof(string)),
                             new DataColumn("invoiceDesc", typeof(string))});

DateTime dt = (DateTime)dtp.Value;
string transDate = clsEasy.makeDateToPersian(dt);
string transTime = clsEasy.makeTime7(DateTime.Now);
string invoiceId = txtInvoiceNumber.Text;
string invoiceDesc = txtDescription.Text;

dtTrans.Rows.Add(transDate, transTime, invoiceId, invoiceDesc);

DataTable dtTransItems = new DataTable();
dtTransItems.Columns.AddRange(new DataColumn[3] { 
                                  new DataColumn("staffId", typeof(string)),
                                  new DataColumn("price", typeof(string)),
                                  new DataColumn("quantity", typeof(string))});

 for (int i = 0; i < dgv.Rows.Count; i++)
 {
      string staffId = dgv.Rows[i].Cells[0].Value.ToString();
      string price = dgv.Rows[i].Cells[2].Value.ToString();
      string quantity = dgv.Rows[i].Cells[3].Value.ToString();

      dtTransItems.Rows.Add(staffId, price, quantity);
 }

 int result = cls.executeDataTable("TransInInsert","@dt", dtTrans);
但我想将这两个数据表都发送到SQL Server。我该怎么做呢


感谢您的帮助:)

在我看来,直接从代码中使用SQL或更好的方式,ORM将导致编写和维护的代码减少数倍。在我看来,直接从代码中使用SQL或更好的方式,ORM将导致编写和维护的代码减少数倍。
protected int executeDataTable(string spName,string tableName, DataTable dt)
{
    try
    {
        connect();
        int affect = 0;

        SqlCommand cmd = new SqlCommand(query, _con);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue(tableName, dt);

        affect = cmd.ExecuteNonQuery();
        disconnect();
        return affect;
    }
    catch (SqlException ex)
    {
        disconnect();
        return ex.Number * (-1);
    }
}