Hi All,
I am reading notepads files and inserting data in sql tables from the notepad-
while performing sql bulk copy on this line it throws exception - "bulkcopy.WriteToServer(dt); -"data type related(mentioned in subject )".
Please go through my logic and tell me what to change to avoid this error -
public void Main() { Dts.TaskResult = (int)ScriptResults.Success; string[] filePaths = Directory.GetFiles(@"C:\Users\jainruc\Desktop\Sudhanshu\master_db\Archive\test\content_insert\"); for (int k = 0; k < filePaths.Length; k++) { string[] lines = System.IO.File.ReadAllLines(filePaths[k]); //table name needs to extract after = sign string[] pathArr = filePaths[0].Split('\\'); string tablename = pathArr[9].Split('.')[0]; DataTable dt = new DataTable(tablename); | string[] arrColumns = lines[1].Split(new char[] { '|' }); foreach (string col in arrColumns) { dt.Columns.Add(col); } for (int i = 2; i < lines.Length; i++) { { string[] columnsvals = lines[i].Split(new char[] { '|' }); DataRow dr = dt.NewRow(); for (int j = 0; j < columnsvals.Length; j++) { //Console.Write(columnsvals[j]); if (string.IsNullOrEmpty(columnsvals[j])) dr[j] = DBNull.Value; else dr[j] = columnsvals[j]; } dt.Rows.Add(dr); } } SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=UI3DATS009X;" + "Initial Catalog=BHI_CSP_DB;" + "User Id=sa;" + "Password=3pp$erv1ce$4"; conn.Open(); SqlBulkCopy bulkcopy = new SqlBulkCopy(conn); bulkcopy.DestinationTableName = dt.TableName; bulkcopy.WriteToServer(dt); conn.Close(); } }
sudhanshu sharma Do good and cast it into river :)