简体   繁体   English

使用相同的MySQL连接插入多行?

[英]Insert multiple row using the same MySQL connection?

I was wondering how you can insert multiple rows into a MySQL database using one connection instead of opening and closing multiple connections. 我想知道如何使用一个连接而不是打开和关闭多个连接将多行插入MySQL数据库。 The data being inserted is coming from string[] so I used a foreach loop to get to each value. 插入的数据来自string []所以我使用foreach循环来获取每个值。

Here my current non-working C# code: 这是我目前的非工作C#代码:

string[] tempfin = table.Split(',');
string username = null;
connection.Open();
foreach (object hope in tempfin)
{
    command.CommandText = "INSERT INTO ATable (Tried, Username) VALUES" + "('" + hope + "','" + username + "')";
    command.ExecuteReader();
} 
connection.Close();

I could open and close the connection in the foreach loop but that has been proven unreliable to me when inserting a large amounts of rows, so is there a way insert multi rows using one connection in C#? 我可以在foreach循环中打开和关闭连接但是在插入大量行时我已经证明这是不可靠的,所以有没有办法在C#中使用一个连接插入多行?

Update: Never mind, I found my problem. 更新:没关系,我发现了我的问题。 It was about using command.ExecuteReader() instead of command.ExecuteNonQuery() 它是关于使用command.ExecuteReader()而不是command.ExecuteNonQuery()

The code you posted is already using only one connection. 您发布的代码已经使用了一个连接。 I would suggest to use a parametrized query instead the one you are using. 我建议使用参数化查询而不是您正在使用的查询。

Why isn't your code working? 你的代码为何不起作用? Can you update your question with the error you're getting? 你能用你得到的错误更新你的问题吗?

If you are issuing an insert command, you should use ExecuteNonQuery instead of ExecuteReader . 如果要发出insert命令,则应使用ExecuteNonQuery而不是ExecuteReader

You can do something like this with MySQL: 你可以用MySQL做这样的事情:

INSERT INTO ATable (X, Y) VALUES (1, 2), (3, 4), (5, 6), (7, 8)

Which will insert 4 rows. 这将插入4行。 So just update your code to build up a single statement and execute that if you're concerned about executing multiple INSERT statements in a row. 因此,只需更新代码以构建单个语句,并执行该语句,如果您担心连续执行多个INSERT语句。

not sure but the other ways i tried ended up returing mysql errors such as "existing connection must be closed" 不确定,但我尝试的其他方式最终导致修复mysql错误,如“现有连接必须关闭”

This is most likely because you are using ExecuteReader instead of ExecuteNonQuery as Pablo Santa Cruz pointed out. 这很可能是因为您正在使用ExecuteReader而不是ExecuteNonQuery正如Pablo Santa Cruz指出的那样。

There, Cleaned it up a bit. 在那里,清理了一下。 Yes, it only uses that one connection. 是的,它只使用那个连接。

string[] tempfin = table.Split(',');
    string username = null;
    try
    {
        connection.Open();
        SQLParameter parUserName = new SqlParameter("@username", System.Data.SqlDbType.VarChar,100);
        if(username != null) parUserName.Value = username; else parUserName.Value = DBNull.Value;

        SQLParameter parHope = new SqlParameter("@hope", System.Data.SqlDbType.VarChar,256);

        command.Parameters.Add(parUserName);
        command.Parameters.Add(parHope);

        command.CommandText = "INSERT INTO ATable (Tried, Username) VALUES (@hope,@username)";
        foreach (string hope in tempfin)
        {
            parHope.Value = hope;
            command.ExecuteNonQuery();
        }
    }
    finally{
        if (connection.State != System.Data.ConnectionState.Closed)
            connection.Close();
        if(command != null)
            command.Dispose();  
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM