3
for (int i = 0; i < myClass.Length; i++)
{
        string upSql = "UPDATE CumulativeTable SET EngPosFT = @EngPosFT,EngFTAv=@EngFTAv WHERE RegNumber =@RegNumber AND Session=@Session AND Form=@Form AND Class=@Class";
        SqlCommand cmdB = new SqlCommand(upSql, connection);

        cmdB.CommandTimeout = 980000;

        cmdB.Parameters.AddWithValue("@EngPosFT", Convert.ToInt32(Pos.GetValue(i)));
        cmdB.Parameters.AddWithValue("@RegNumber", myClass.GetValue(i));
        cmdB.Parameters.AddWithValue("@EngFTAv", Math.Round((engtot / arrayCount), 2));
        cmdB.Parameters.AddWithValue("@Session", drpSess.SelectedValue);
        cmdB.Parameters.AddWithValue("@Form", drpForm.SelectedValue);
        cmdB.Parameters.AddWithValue("@Class", drpClass.SelectedValue);


        int idd = Convert.ToInt32(cmdB.ExecuteScalar());
}

假设myClass.Length是 60。这会执行 60 条更新语句。如何将其限制为 1 个更新语句。请使用上述代码的代码示例将不胜感激。谢谢

试过用这个

StringBuilder 命令 = new StringBuilder();

            SqlCommand cmdB = null;
            for (int i = 0; i < myClass.Length; i++)
            {
                command.Append("UPDATE CumulativeTable SET" + " EngPosFT = " + Convert.ToInt32(Pos.GetValue(i)) + "," + " EngFTAv = " + Math.Round((engtot / arrayCount), 2) +
        " WHERE RegNumber = " + myClass.GetValue(i) + " AND Session= " + drpSess.SelectedValue + " AND Form= " + drpForm.SelectedValue + " AND Class= " + drpClass.SelectedValue + ";");

               //or command.AppendFormat("UPDATE CumulativeTable SET EngPosFT = {0},EngFTAv={1} WHERE RegNumber ={2} AND Session={3} AND Form={4} AND Class={5};", Convert.ToInt32(Pos.GetValue(i)), Math.Round((engtot / arrayCount), 2), myClass.GetValue(i), drpSess.SelectedValue, drpForm.SelectedValue, drpClass.SelectedValue);



            }//max length is 128 error is encountered
4

4 回答 4

1

在这种情况下,最好创建一个接受表值参数的存储过程。在 .NET 方面,您DataTable为要使用的每组值创建一个包含一行的对象。

在 SQL Server 方面,您可以将参数视为查询中的另一个表。因此,在存储过程中,您将拥有:

UPDATE a
SET
    EngPosFT = b.EngPosFT,
    EngFTAv=b.EngFTAv
FROM
    CumulativeTable a
       inner join
    @MyParm b
       on
           a.RegNumber =b.RegNumber AND
           a.Session=b.Session AND
           a.Form=b.Form AND
           a.Class=b.Class

@MyParm您的表值参数在哪里。

然后,这将作为到服务器的单次往返处理。

于 2012-10-22T06:53:29.717 回答
1

查看BULK INSERT T-SQL 命令。但是由于我对该命令没有太多的个人经验,因此我确实看到了一些直接的机会,可以通过在循环外创建命令和参数并仅在循环内进行必要的更改来使用相同的 sql 改进此代码:

string upSql = "UPDATE CumulativeTable SET EngPosFT = @EngPosFT,EngFTAv=@EngFTAv WHERE RegNumber =@RegNumber AND Session=@Session AND Form=@Form AND Class=@Class";
SqlCommand cmdB = new SqlCommand(upSql, connection);

cmdB.CommandTimeout = 980000;

//I had to guess at the sql types you used here. 
//Adjust this to match your actual column data types
cmdB.Parameters.Add("@EngPosFT", SqlDbType.Int);  
cmdB.Parameters.Add("@RegNumber", SqlDbType.Int); 

//It's really better to use explicit types here, too.
//I'll just update the first parameter as an example of how it looks:
cmdB.Parameters.Add("@EngFTAv", SqlDbType.Decimal).Value = Math.Round((engtot / arrayCount), 2));
cmdB.Parameters.AddWithValue("@Session", drpSess.SelectedValue);
cmdB.Parameters.AddWithValue("@Form", drpForm.SelectedValue);
cmdB.Parameters.AddWithValue("@Class", SqlDbTypedrpClass.SelectedValue);

for (int i = 0; i < myClass.Length; i++)
{
    cmdB.Parameters[0].Value = Convert.ToInt32(Pos.GetValue(i)));
    cmdB.Parameters[1].Value = myClass.GetValue(i));

    int idd = Convert.ToInt32(cmdB.ExecuteScalar());
}
于 2012-10-20T21:12:59.683 回答
0

在这种情况下,最好编写一个存储过程并在for循环中调用该存储过程,并在每次调用时传递必要的参数。

于 2012-10-22T06:45:06.180 回答
0
using System;
using System.Data;
using System.Data.SqlClient;

    namespace DataTableExample
    {
        class Program
        {
          static void Main(string[] args)
            {
                DataTable prodSalesData = new DataTable("ProductSalesData");

                // Create Column 1: SaleDate
                DataColumn dateColumn = new DataColumn();
                dateColumn.DataType = Type.GetType("System.DateTime");
                dateColumn.ColumnName = "SaleDate";

                // Create Column 2: ProductName
                DataColumn productNameColumn = new DataColumn();
                productNameColumn.ColumnName = "ProductName";

                // Create Column 3: TotalSales
                DataColumn totalSalesColumn = new DataColumn();
                totalSalesColumn.DataType = Type.GetType("System.Int32");
                totalSalesColumn.ColumnName = "TotalSales";

                // Add the columns to the ProductSalesData DataTable
                prodSalesData.Columns.Add(dateColumn);
                prodSalesData.Columns.Add(productNameColumn);
                prodSalesData.Columns.Add(totalSalesColumn);

                // Let's populate the datatable with our stats.
                // You can add as many rows as you want here!

                // Create a new row
                DataRow dailyProductSalesRow = prodSalesData.NewRow();
                dailyProductSalesRow["SaleDate"] = DateTime.Now.Date;
                dailyProductSalesRow["ProductName"] = "Nike";
                dailyProductSalesRow["TotalSales"] = 10;

                // Add the row to the ProductSalesData DataTable
                prodSalesData.Rows.Add(dailyProductSalesRow);

                // Copy the DataTable to SQL Server using SqlBulkCopy
                using (SqlConnection dbConnection = new SqlConnection("Data Source=ProductHost;Initial Catalog=dbProduct;Integrated Security=SSPI;Connection Timeout=60;Min Pool Size=2;Max Pool Size=20;"))
                {
                    dbConnection.Open();
                    using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
                    {
                        s.DestinationTableName = prodSalesData.TableName;

                        foreach (var column in prodSalesData.Columns)
                            s.ColumnMappings.Add(column.ToString(), column.ToString());

                        s.WriteToServer(prodSalesData);
                    }
                }
            }
        }
    }
于 2013-08-30T04:08:55.483 回答