Converting String to bit before inserting into the database

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

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.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why