This is a question from an experienced beginner!
Using ASP.NET 4 C# AND SQL server,
I have a connection string in web.config to myDatabase named “myCS”.
I have a database named myDB.
I have a table named myTable with a primary key named myPK
What are the NECESSARY lines of code behind (minimal code) to create a SQL connection, then select from myTable where myPK==”simpleText”
it will probably include:
sqlconnection conn = new sqlconnection(??? myCS)
string SQLcommand = select * from myDB.myTable where myPK==myTestString;
sqlCommand command = new SqlCommand(SQL,conn);
conn.Open();
booleanFlag = ????
conn.Close();
conn.Dispose();
then
If ( theAnswer != NULL ) // or (if flag)
{
Response.Redirect("Page1.aspx");
}
else
{
Response.Redirect("Page2.aspx");
}
Here is a limited simple tutorial:
First, you want to have a class to do the hard work for you, then you will use it with ease.
First, you have to crate the connection string in your web.config file and name it.
Here it is named
DatabaseConnectionString, but you may named itmyCSas required in the question.Now, in App_Code create a new class file and name it
SqlComm(this is just an example name) like:Okay, this only a class, and now you should know how to use it:
If you wish to execute a command like delete, insert, update etc. use this:
but if you need to retrieve a specific value from the database use this:
You can retrieve a bunch of rows from the database this way (others like other ways)
This is relevant to your sepecific question
If you need to execute a stored procedure with or without returning a value back this is the way to do that (in this example there are no returning value)
Again, for your specific question return the table using the
SqlDataTable, and redirect ifdt.Rows.Count >0Have fun.