views:

18

answers:

1

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?

A: 

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{}
    }
}
Pieter
plase write code, 'cause not understand
4iNo
or a better way would be to optimize the sql to not have to do 100000 statements. perhaps a stored procedure also might be a better option?
RPM1984
and that as an argument to pass?
4iNo
The above code is simply splitting up our queries into multiple smaller queries. How you use it depends on what your application does. Without further information, this is the best I can do :).
Pieter
And if records was 47 ??? How to rewrite the code ?
4iNo
Updated answer with a dynamic total records and max records per command.
Pieter
Thank a lot!!!!
4iNo