Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多行插入->语句超出了允许的最大1000行值_C#_Sql Server - Fatal编程技术网

C# 多行插入->语句超出了允许的最大1000行值

C# 多行插入->语句超出了允许的最大1000行值,c#,sql-server,C#,Sql Server,我正在使用SQL2008ExpressEdition,我正在尝试通过我的C应用程序进行多行插入 我有大约100000条记录需要插入 好的,前1000条记录一切顺利,然后我得到了错误: INSERT语句中的行值表达式数超过了允许的最大行值数1000 我查看了我的列数据类型->int,所以这应该不是问题所在。 我检查了我的代码,我正在插入500条记录 所以我在谷歌上搜索了一下,但没有找到任何有用的东西。有人能解释一下我为什么会出现这个错误,如果可能的话,如何解决它。你可以使用这个类。它支持批处理、事

我正在使用SQL2008ExpressEdition,我正在尝试通过我的C应用程序进行多行插入

我有大约100000条记录需要插入

好的,前1000条记录一切顺利,然后我得到了错误:

INSERT语句中的行值表达式数超过了允许的最大行值数1000

我查看了我的列数据类型->int,所以这应该不是问题所在。 我检查了我的代码,我正在插入500条记录


所以我在谷歌上搜索了一下,但没有找到任何有用的东西。有人能解释一下我为什么会出现这个错误,如果可能的话,如何解决它。

你可以使用这个类。它支持批处理、事务处理,并且比标准insert语句更高效。

这就是我的代码处理多重插入的方式

var count = "SELECT COUNT(*) as rowcount FROM table_mysql GROUP BY id";

var countReader = Retrieve(count);
var countRows = 0;
try
{
    while (countReader.Read())
    {
        countRows += int.Parse(countReader.GetValue(0).ToString());
    }
}
catch (Exception ex)
{
    Log.LogMessageToFile("Import.cs -> table_mssql: " + ex.StackTrace + " /n" + ex.Message);
}
finally
{
    if (countReader != null) { countReader.Close(); _crud.close_conn(); }
}

for (var a = 0; a < countRows; )
{
    var sql = "SELECT id, traffic_id, dow, uu, imps, impsuu, otsw, otsm FROM table_mysql LIMIT " + a + ", " + (a + 500) + "";

    var reader = Retrieve(sql);
    try
    {
        var builder = new StringBuilder();
        builder.Append(
            "SET IDENTITY_INSERT table_mssql ON;INSERT INTO table_mssql(id, traffic_id, dow, uu, imps, impsuu, otsw, otsm) VALUES ");
        while (reader.Read())
        {
            Application.DoEvents();
            try
            {
                builder.Append("(" + reader.GetValue(0) + ", " + reader.GetValue(1) + ", " +
                               reader.GetValue(2) +
                               ", " + reader.GetValue(3) + ", " + reader.GetValue(4) +
                               ", " + reader.GetValue(5) + ", " + reader.GetValue(6) + ", " +
                               reader.GetValue(7) +
                               "), ");

            }
            catch (Exception ex)
            {
                Log.LogMessageToFile("Import.cs -> table_mssql: " + ex.StackTrace + " /n" + ex.Message);
            }
        }

        var sqlDB = builder.ToString(0, builder.Length - 2);

        sqlDB += ";SET IDENTITY_INSERT table_mssql OFF;";
        if (!InsertDB(sqlDB))
        {
            Log.LogMessageToFile("Import.cs -> table_mssql: No insert happend!");
        }
    }
    catch (Exception ex)
    {
        Log.LogMessageToFile("Import.cs -> table_mssql: " + ex.StackTrace + " /n" + ex.Message);
        return false;
    }
    finally
    {
        if (reader != null)
        {
            reader.Close();
            _crud.close_conn();
        }
    }
    a = a + 500;
}

我要检查一下复印件。也许这是一个更好的解决方案

你可以把整个事情归结为:

var sql = "SET IDENTITY_INSERT table_mssql ON;" 
        + "INSERT INTO table_mssql"
        +      "(id, traffic_id, dow, uu, imps, impsuu, otsw, otsm)"
        + " SELECT id, traffic_id, dow, uu, imps, impsuu, otsw, otsm "
        + " FROM table_mysql;"
        + "SET IDENTITY_INSERT table_mssql OFF;";

if (!InsertDB(sqlDB))
{
    Log.LogMessageToFile("Import.cs -> table_mssql: No insert happend!");
}

另外:您应该知道SQLServer不支持MySql的LIMIT关键字。它使用了TOP或ROW_编号。

您能给我们看看您的一些代码吗?只有在使用行值时才会出现该错误消息,即插入到。。。价值观插入查询之间是否需要延迟?是否尝试一次插入一行?如果您还没有执行上述操作,请尝试调用存储过程一次执行所有插入操作。正如lassevk所述,该方法的限制是1000行。您可能需要查看INSERT into tblName SELECT。。。从otherTbl语法,因为您所做的几乎直接来自另一个SQL语句。这没有行限制。当第一个查询访问mysql数据库以检索数据时,这也适用吗?我需要将数据从mysql数据库传输到我本地的SQLServer2008Express。啊,我现在知道你的命名约定了。您的代码片段和问题文本使所有内容看起来都是通过相同的连接进行的,无论是否有名称。在这种情况下,您需要在客户端短暂保存数据,而BULK_INSERT是目前为止您最快的选择。或者,如果运气好的话,您可以将mysql db作为ole db数据源连接到sql server:很抱歉造成混淆。我在代码中遗漏了注释。好的,那么我将尝试批量插入。链接中的示例是Enterprise Manager和SQL Server 2000,但您应该能够在2008年的Management Studio中执行类似操作。如果源表和目标表位于同一SQL Server实例中,使用Transact-SQL INSERT…SELECT语句复制数据更容易、更快。资料来源:仅供记录: