Program.cs file
App.config File
using System.Configuration; using System.Data; using System.Data.SqlClient; using System.IO; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { DataTable dt = new DataTable(); string line = null; int i = 0; using (StreamReader sr = File.OpenText(@"c:\temp\table1.csv")) { while ((line = sr.ReadLine()) != null) { string[] data = line.Split(','); if (data.Length > 0) { if (i == 0) { foreach (var item in data) { dt.Columns.Add(new DataColumn()); } i++; } DataRow row = dt.NewRow(); row.ItemArray = data; dt.Rows.Add(row); } } } using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConsoleApplication3.Properties.Settings.daasConnectionString"].ConnectionString)) { cn.Open(); using (SqlBulkCopy copy = new SqlBulkCopy(cn)) { copy.ColumnMappings.Add(0, 0); copy.ColumnMappings.Add(1, 1); copy.ColumnMappings.Add(2, 2); copy.ColumnMappings.Add(3, 3); copy.ColumnMappings.Add(4, 4); copy.DestinationTableName = "Censis"; copy.WriteToServer(dt); } } } } }
App.config File
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="ConsoleApplication3.Properties.Settings.daasConnectionString" connectionString="Data Source=malcolm-pc\sqlexpress;Initial Catalog=TestData;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
Post a Comment Blogger Facebook