Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk update SQL Server C#

I have a 270k-row database with primary key mid and a column called value, and I have a text file with mid's and values. Now I would like to update the table such that each value is assigned to the correct mid.

My current approach is reading the text file from C#, and updating a row in the table for each line that I read. There must be quicker way to do things I feel.. any ideas?

EDIT: There are other columns in the table, so I really need a method to update according to mid.

like image 209
Freek8 Avatar asked Jan 18 '26 21:01

Freek8


2 Answers

You could use the SQL Server Import and Export Wizard:

http://msdn.microsoft.com/en-us/library/ms141209.aspx

Alternatively you could use the BULK TSQL Statement:

BULK
INSERT YourTable
FROM 'c:\YourTextFile.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO


SELECT * FROM YourTable
GO

Assuming you are splitting on a comma delimiter. If you are using another character such as a space, change the FIELDTERMINATOR to the associated character.

Edit (To achieve the update from my comment):

UPDATE RealTable
SET value = tt.value
FROM
  RealTable r
INNER JOIN temp_table tt ON r.mid = tt.mid
like image 130
Darren Avatar answered Jan 21 '26 11:01

Darren


Do you reuse the SqlCommand?

    struct Item
    {
        public int mid;
        public int value;
    }

    public int Update(string connectionstring)
    {
        int res = 0;
        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
        using (cmd.Connection = new System.Data.SqlClient.SqlConnection(connectionstring))
        {
            cmd.CommandText = @"UPDATE [table] SET [value] = @value WHERE [mid] = @mid;";
            cmd.CommandType = CommandType.Text;
            System.Data.SqlClient.SqlParameter mid = cmd.Parameters.Add("@mid", SqlDbType.VarChar);
            System.Data.SqlClient.SqlParameter value = cmd.Parameters.Add("@value", SqlDbType.VarChar);
            try
            {
                cmd.Connection.Open();
                foreach (Item item in GetItems("pathttothefile"))
                {
                    mid.Value = item.mid;
                    value.Value = item.value;
                    res += cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                cmd.Connection.Close();
            }
        }
        return res;
    }

    IEnumerable<Item> GetItems(string path)
    {
        string[] line;
        foreach (var item in System.IO.File.ReadLines(path))
        {
            line = item.Split(',');
            yield return new Item() { mid = int.Parse(line[0]), value = int.Parse(line[1]) };
        }
    }
like image 27
jahu Avatar answered Jan 21 '26 10:01

jahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!