Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Over the past year I have found your site to be EXCELLENT. Never have I been able to find so many answers to such vast problems and it is an excellent service..."

Geography

Where in the world do Tek-Tips members come from?

Handling Empty Values in SqlBulkCopy

larrydavid (Programmer)
14 Jun 12 15:17
Hello, I have a FileImport class which is using SqlBulkCopy method. It works great if all the values in the .txt file are populated, but if there are any empty values it fails.

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public class FileImport
{
public static void Import(string savePath)
{
DataTable dt = new DataTable();
string line = null;
int i = 0;

using (StreamReader sr = File.OpenText(savePath))
{
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++;
}
else
{
DataRow row = dt.NewRow();
/*
for (int j = 0; j < data.Length; j++)
{
if (!string.IsNullOrEmpty(data[j]))
row[j] = System.Convert.ChangeType(data[i], row.Table.Columns[i].DataType);
}
*/
row.ItemArray = data;
dt.Rows.Add(row);
}
}
}

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TESTConnectionString"].ToString()))
{
conn.Open();
using (SqlBulkCopy copy = new SqlBulkCopy(conn))
{
copy.ColumnMappings.Add(0, 0); //BATCH_TYPE_ID
copy.ColumnMappings.Add(1, 1); //LOAD_DATE (throws exception if empty)
copy.ColumnMappings.Add(2, 2); //VENDOR_ID
copy.ColumnMappings.Add(3, 3); //TAB_TYPE_ID
copy.ColumnMappings.Add(4, 4); //TYPE_1
copy.ColumnMappings.Add(5, 5); //TYPE_2

copy.DestinationTableName = "TestSubmission";
copy.WriteToServer(dt);
}
}
}
}
}

Any help would be greatly appreciated.

Thanks,
Larry
phinoppix (Programmer)
19 Jun 12 1:18
In the constructor, try adding the SqlBulkCopyOptions.KeepNulls option.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close