Last post Mar 28, 2012 03:14 PM by Ramesh T
Mar 28, 2012 02:13 PM|smksu|LINK
I have a string array that i am trying to compare to values in a column in my database table. How can I check to see if the values in the array are present in my table or not , and if there are not present in the table how can i store them in another arrray?
thank you for all your time and i would really apperciate any suggestions.
Mar 28, 2012 02:29 PM|steve.watson04|LINK
take array value in comma separated string, then pass this string to query where you have to use in operator for check value.
Array to string look like "one,two,three"
then query: select count(*) from tablename where columnname in (one,two,three)
if it results more greater than 0 then db has same value in table.
Mar 28, 2012 03:14 PM|Ramesh T|LINK
Try something like this
List<string> sourceStringArray = new List<string>();
List<string> targetStringArray = new List<string>();
List<string> tempStringArray = new List<string>();
StringBuilder sp = new StringBuilder();
sp.Append("Select ColumnName from UrTableName Where ColumnName Not In (");
foreach(var val in sourceStringArray)
sp.Append( "'"+ val + "', ");
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Ur Connection String";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sp.ToString().Remove(sp.ToString().LastIndexOf(", "), 1);
var dataReader = cmd.ExecuteReader();
var result = from a in sourceStringArray
where !(from b in tempStringArray