Best practice question:
I have a CSV file that looks like this:
1,2,3
1,,3
,2,3
,,3
1
1
1
I want it to go into SQL:
1,2,3
1,null,3
null,2,3
null,null,3
1,null,null
1,null,null
1,null,null
The last three rows are giving me problems. Without the correct number of commas, my "cmd.Parameters.Add" throws an exception: IndexOutOfRangeException when no value has been assigned to that location in the array. I would like it to just pass null values like it already does for instances where a comma delimits an empty space.
while
{
string parts = new string[10];
parts = parser.ReadFields();
if (parts==null)
{break};
SqlCommand cmd = new SqlCommand("sp", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@one",SqlDbType.Int).Value = parts[0];
cmd.Parameters.Add("@two",SqlDbType.NVarChar).Value = parts[1];
cmd.Parameters.Add("@three",SqlDbType.Float).Value = parts[2];
[run cmd]
How should I be doing this? I have a couple hundred parameters, so avoiding a solution where I test every single value for null and then apply DBNull.Value would be best if possible. Is this [string parts = new string[10];] my problem? Is there a better way to declare this array that will avoid these exceptions?