简体   繁体   中英

Sql Server Connection C#: max number of insert queries

I've a problem with SqlConnection in C#. I do a large number of INSERT NonQuery, but in any case SqlConnection save in the database always the first 573 rows. This is the method I use for queries. In this method there is a lock because I use different thread to save the data.

   public void InsertElement(string link, string titolo, string text)
    {
        string conString = "*****************";
        using (SqlConnection connection = new SqlConnection(conString))
        {
            connection.Open();

            text = text.Replace("\"", "");
            DateTime localDate = DateTime.Now;

            lock (thisLock)
            {
                string query = "IF (NOT EXISTS(SELECT * FROM Result " +
                " WHERE Link = '" + link + "')) " +
                " BEGIN " +
                " INSERT INTO Result ([Titolo],[Link],[Descrizione],[DataRicerca],[FKDatiRicercheID]) " +
                " VALUES('" + titolo + "', '" + link + "', '" + text + "', '" + localDate + "', 1) " +
                " END";

                if (connection != null)
                {
                    SqlCommand cmd = new SqlCommand(query, connection);
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }

This is the code of the loop that call the method InsertElement()

public void Save()
{
    string[] DatiLetti;
    string url = "";

    while (result.Count > 0)
    {
        try
        {
            url = result.Last();
            result.RemoveAt(result.Count - 1);

            DatiLetti = ex.DirectExtractText(url);

            if (DatiLetti[0].Length > 2)
            {
                ssc.InsertGare(url, DatiLetti[0], DatiLetti[1]);
            }
        }
        catch (Exception exc)
        {
            logger.Error("Exception SpiderSave> " + exc);
        }
    }
}

Result is a volatile array that is progressively filled from other thread. I'm sure that the array contains more than 573 items.

I try to search one solution, but all the answers say that the number of database connections for SQLServer is over 32K at a time and I've already checked this number in my database. Is there anyone who can help me understand the problem?

If you are executing InsertElement() once for each rows of data to insert, then the execution will be too slow for large no. of rows. (Also, you are creating SqlConnection for each query execution.) Try adding many rows at once using a single INSERT query:

INSERT INTO tablename
(c1,c2,c3)
VALUES
(v1,v2,v3),
(v4,v5,v6)
...

Don't open a connection for every insert. Use one connection, then pass that connection through to your insert, like this :

public void InsertElement(string link, string titolo, string text, SqlConnection conn)
{
    text = text.Replace("\"", "");
    DateTime localDate = DateTime.Now;

    lock (thisLock)
    {
        string query = "IF (NOT EXISTS(SELECT * FROM Result " +
                        " WHERE Link = '" + link + "')) " +
                        " BEGIN " +
                        " INSERT INTO Result ([Titolo],[Link],[Descrizione],[DataRicerca],[FKDatiRicercheID]) " +
                            " VALUES('" + titolo + "', '" + link + "', '" + text + "', '" + localDate + "', 1) " +
                            " END";

        if (connection != null)
        {
            SqlCommand cmd = new SqlCommand(query, connection);
            cmd.ExecuteNonQuery();
        }
    }
}

I recommend also looking at paramatizing your query, as well as using bulk inserts, and not individual inserts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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