Avinash Verma

Avinash Verma

  • NA
  • 3
  • 936

Bulk text import into SQL server using C#

Mar 16 2018 5:47 AM
Hi,
 
I have the text file and want to use sqlbulk copy to load the data in to SQL using C#
using below code getting error- Column requires a valid DataType.
 
String strConnection = "Data Source=localhost;Initial Catalog=" + pname + ";Integrated Security=True";
String textConnString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"text\"", fname);
DataTable dt = new DataTable();
string line = null;
int i = 0;
using (StreamReader sr = File.OpenText(fname))
{
while ((line = sr.ReadLine()) != null)
{
string[] data = line.Split(' ');
if (data.Length > 0)
{
if (i == 0)
{
foreach (var item in data)
{
DataColumn column;
column = new DataColumn();
column.DataType = System.Type.GetType("System.char");
dt.Columns.Add(new DataColumn());---->(getting error Column requires a valid DataType)
}
i++;
}
DataRow row = dt.NewRow();
row.ItemArray = data;
dt.Rows.Add(row);
}
}
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(strConnection))
{
bulkCopy.BulkCopyTimeout = 0;
bulkCopy.DestinationTableName = "dbo.AdmissionControl";
// Write from the source to the destination.
bulkCopy.WriteToServer(dt);
}
 
please support

Answers (1)