This is how I wrote a select statement to check if there's a value in the database.
bool userIsPresent=false;
string sqlQuery = string.Format("SELECT * FROM Person WHERE Name = '{0}'", name);
SqlCommand s = new SqlCommand(sqlQuery, con);
con.Open();
SqlDataReader sqlread = s.ExecuteReader();
userIsPresent = sqlread.HasRows;
con.Close();
But now I need to save some values into the database. How can I do this ? I don't think I should use SqlDataReader, so how can I save and confirm if the data is saved to the database?
public static bool saveToDb(string name1,string nam2, DateTime dat)
{
bool ok=false;
string sqlQuery = string.Format("INSERT into NumbersTable values ('{0}', '{1}','{2}')",name1,nam2,dat );
SqlCommand s = new SqlCommand(sqlQuery, con);
con.Open();
SqlDataReader sr = s.ExecuteReader(); // MIGHT BE WRONG
ok = sr.HasRows;
con.Close();
return ok;
}
string.Formatdoes not prevent you from sql-injection attacks. Use SQL-Parameters instead!