Created
August 14, 2012 21:13
-
-
Save Falven/3353088 to your computer and use it in GitHub Desktop.
Sql Bulk Insert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void BulkInsertFile(string fileName, string tableName) | |
{ | |
FileInfo info = new FileInfo(fileName); | |
string name = info.Name; | |
string shareDirectory = ""; //the path of the share: \\servername\shareName\ | |
string serverDirectory = ""; //the local path of the share on the server: C:\shareName\ | |
File.Copy(fileName, shareDirectory + name); | |
// or you could call your method to parse the file and write it to the share directory. | |
using (SqlConnection cnn = new SqlConnection("connectionString")) | |
{ | |
cnn.Open(); | |
using (SqlCommand cmd = cnn.CreateCommand()) | |
{ | |
cmd.CommandText = string.Format("bulk insert {0} from '{1}' with (fieldterminator = ',', rowterminator = '\n')", tableName, serverDirectory + name); | |
try | |
{ | |
cmd.ExecuteScalar(); | |
} | |
catch (SqlException ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment