Before putting into the database, convert String to bit.

asp.net c# sql sqlbulkcopy sql-server

Question

Can anyone help in converting string value in C# to bit equivalent in Sql. I'm trying to bulkcopy the values of a datatable into Sql table. All the values I've in the datatable are in string format. When I try to bulkcopy to SQL table I'm getting an error for bit datatype columns.Can anyone please post the C# code to convert string to bit type before I bulkcopy to SQL table.

Thanks, Vix

1
0
11/9/2009 5:40:11 PM

Popular Answer

If your strings are "true" and "false" (ignoring case and whitespace) this will work:

bool bit = bool.Parse(str);

If your strings are something else, you could use:

bool bit = !string.IsNullOrEmpty(str) &&
  (str[0]=='Y' || str[0]=='y' || str[0]=='T' || str[0]=='t' || str[0]=='1');

SQL wants a bool value.

5
11/8/2009 5:24:39 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow