Ok, so suppose I have a .sql file (e.g., data.sql) with a couple thousand insert commands, like this:
...
INSERT INTO some_table VALUES ('person_name_1', 31, 'CA');
INSERT INTO some_table VALUES ('person_name_2', 29, 'NC');
INSERT INTO some_table VALUES ('person_name_3', 18, 'NY');
INSERT INTO some_table VALUES ('person_name_4', 21, 'IL');
INSERT INTO some_table VALUES ('person_name_5', 25, 'FL');
...
I wanted to know the best way to insert this data into a SQL Server 2012 database from C# code, and just as additional info, this is something that will be done once daily, but manually, from a web interface.
Right now, what I have is: I parse the .sql file and build this really big INSERT, so I end up with a StringBuilder (sb) that has
INSERT INTO some_table VALUES
('person_name_1', 31, 'CA'),
('person_name_2', 29, 'NC'),
('person_name_3', 18, 'NY'),
('person_name_4', 21, 'IL'),
('person_name_5', 25, 'FL'),
...
And then I run
using (var cmd = new SqlCommand { Connection = conn })
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = sb.ToString().Remove(sb.Length - 1);
cmd.ExecuteNonQuery();
}
Any thoughts on a better way to do this?