Stop using var unless you have to. Use the appropriate object. Using var limits readability and can cause a throwback to hungarian notation for variables, which leads to long, often pointless, variable names
Why use string.Format without using its major benefit?
Code:
String.Format("INSERT INTO KeyStorage (Email, Hash, isUsed) VALUES ('" + @emailAddr + "', '" +
@stringArray + "', 0)");
Should be
Code:
String.Format("INSERT INTO KeyStorage (Email, Hash, isUsed) VALUES ('{0}', '{1}', 0)", emailAddr, stringArray);
Although some would argue that using stringbuilder or plain concatenation would be the better (faster) route.
Then, you set up your command and execute it. Two things
#1:
Code:
var sqlComm = new SqlCommand(sqlStr, DataConn.Connect());
-- should have SqlCommand instead of var (as noted above) and then you should use paramaterized queries to run this.
Example: change your querystring into
"INSERT INTO KeyStorage (Email, Hash, isUsed) VALUES (@email, @hash, 0)"
And then, after you've initialized your command, add the following:
Code:
cmd.Parameters.Add(new SqlParameter("@email", System.Data.SqlDbType.VarChar, 50)).Value = emailAddr;
cmd.Parameters.Add(new SqlParameter("@hash", System.Data.SqlDbType.VarChar, 50)).Value = stringArray;
note:** change the "varchar, 50" to whatever your datatype is.
This does two things: first, it stops any sql injection possibilities, which were a problem with your code above. Secondly, it checks the datatype being put into the database *before* the insert is done. This allows you to catch the error before your program crashed because of a database exception.
#2: Isn't necessary, but it is nice to do:
where you do:
Code:
sqlComm.ExecuteNonQuery();
Try switching it to
Code:
if (sqlComm.ExecuteNonQuery() == 1)
ExecuteNonQuery returns the number of rows affected, so doing this allows you to verify that something was done before moving on.
It is also good to add more than just an SqlException to your try/catch. Obviously you want to cover the specific error, but catching just a general Exception means you can handle any exception, even ones you never thought of, somewhat gracefully. You should still try to make sure exceptions don't happen, and if they do, handle them with their specific exceptions first, otherwise troubleshooting can become a pain.
Then we move into your other class. Once again, you seem to misuse string.Format. There is no point in using it the way you do. I would recommend, once again, to switch it from:
Code:
String.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename="+ HttpContext.Current.Server.MapPath("~") +@"\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
to:
Code:
String.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0}\App_Data\Database.mdf;Integrated Security=True;User Instance=True", HttpContext.Current.Server.MapPath("~"));
It is also a good idea, when building a web app, to store your connection string in the web.config, so you don't have to re-compile code to change it.
There are plenty of other things, like your lack of try/catches or catching possible errors, but I think you get the gist. It is a good effort for a first attempt, but I definitely think you should go back and re-write some of the code. Never hurts to keep improving :)