Using String Builder, I need to check whether the text in 3 fields in my app are already in 3 corresponding columns in my database table before inserting any of them.
First I put the text from the 3 fields into object properties called LastName, StreetAddress and ZipCode. Then I created my parameters. hen I did this:
var sb = new StringBuilder();
sb.Append("DECLARE @countLastName INT;");
sb.Append("DECLARE @countStreetAddress INT;");
sb.Append("DECLARE @countZipCode INT;");
sb.Append("SELECT @countLastName = COUNT (*) FROM tblCustomer WHERE LastName = @LastName;");
sb.Append("SELECT @countStreetAddress = COUNT (*) FROM tblCustomer WHERE StreetAddress = @StreetAddress;");
sb.Append("SELECT @countZipCode = COUNT (*) FROM tblCustomer WHERE ZipCode = @ZipCode;");
if (@countLastName < 1 || @countStreetAddress < 1 || @countZipCode < 1)
{
sb.Append("INSERT INTO tblCustomer (");
sb.Append("LastName, ");
sb.Append("StreetAddress, ");
sb.Append("ZipCode) ");
sb.Append("VALUES (@LastName, @StreetAddress, @ZipCode) ");
}
else
{
[Code that results in 1 of 3 messages being displayed to user based on which field is already in database. IE "LastName already exists"]
}
reader.CommandText = sb + "";
}
reader.Execute();
I assigned the String Builder to my reader.CommandText, then did a reader.Execute();, as I've done for all my database hits.
Visual Studio doesn't like that I put the @count variables into my IF statement. There's a red squiggly lines under each one of them.
So how would I check using String Builder that my count variables are less than 1 before inserting my values?
Thank you!