首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >插入写入

插入写入
EN

Stack Overflow用户
提问于 2010-10-19 05:34:25
回答 1查看 48关注 0票数 0

代码:

代码语言:javascript
复制
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{}

只插入< 1000写怎么写所有100000写?可以销毁并创建sqlcmd?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-19 05:39:43

您可能应该将命令拆分为较小的命令。例如,在插入所有记录之前,每个SqlCommand插入500个记录。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3965596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档