开发者

inserted writes

开发者 https://www.devze.com 2023-01-21 10:23 出处:网络
code : SqlConnection sqlc = new SqlConnection( 开发者_运维问答\"Data Source=\" + Environment.MachineName + @\"\\SQLEXPRESS;\" +

code :

SqlConnection sqlc = new SqlConnection(
                  开发者_运维问答      "Data Source=" + Environment.MachineName + @"\SQLEXPRESS;" +
                        "Integrated security=true;" +
                        "database=someDB");

SqlCommand sqlcmd;
string tmp = string.Empty;

for(int i = 0; i < 100000; i++)
{
    tmp += "inserto into [db].[Files](...) values (...);"
}

sqlcmd = new SqlCommand(tmp, sqlc);
try { sqlc.Open(); sqlcmd.ExecuteNonQuety(); } cathc{}

inserted only < 1000 writes How write all 100000 writes ?? May be destroy and create sqlcmd?


You should probably split the commands up into smaller commands. Say, inserting 500 records per SqlCommand until you've inserted all your records.

int totalRecordsToWrite = 100000;
int maxRecordsPerCommand = 500;

SqlConnection sqlc = new SqlConnection(
                        "Data Source=" + Environment.MachineName + @"\SQLEXPRESS;" +
                        "Integrated security=true;" +
                        "database=someDB");

int currentRecord = 0;

while (currentRecord < totalRecordsToWrite)
{
    SqlCommand sqlcmd;
    string tmp = string.Empty;

    for(int j = 0; j < maxRecordsPerCommand; j++)
    {
        currentRecord++;

        if (currentRecord >= totalRecordsToWrite)
          break;

        // Insert record "currentRecord" of the 100000 here.

        tmp += "inserto into [db].[Files](...) values (...);"
    }

    using (sqlcmd = new SqlCommand(tmp, sqlc))
    {
       try { sqlc.Open(); sqlcmd.ExecuteNonQuety(); } catch{}
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号