gpt4 book ai didi

sql-server-2008 - 批量插入到 SQL Server 2008

转载 作者:行者123 更新时间:2023-12-03 02:00:22 25 4
gpt4 key购买 nike

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

最佳答案

看看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());
}

关于sql-server-2008 - 批量插入到 SQL Server 2008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12992598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com