Notes on SQL Server
NB: any SQLSERVER code is in red
Include the reference;
using System.Data.SqlClient;
to connect and open...
SqlConnection myConnection = new SqlConnection("user id=SQLSERVERUSERNAME;" +"password=SQLSERVERPASSWORD;server=SERVER PATH HERE, USE . IF LOCAL;" + "Trusted_Connection=yes;"+ "database=DATABSE TO CONNECT TO; ");
myConnection.Open();
NOTE: Connection must be made EVERY time data is added / retrieved
To Add Data the connected Database....
//create new command to add data//
SqlCommand COMANDNAMEHERE=newSqlCommand("INSERTINTO tablename(collumname) " //here colum names can be comma seperated//
+ "Values (valuetoadd)", myConnection);//here the values to add can be comma seperated//
// myConnection is name of establishd connection//
//this command will insert new value (valuetoadd) into tablename in specified column//
COMMANDNAMEHERE.ExecuteNonQuery();
//run command//
To Retrieve Data from the connected Database and assign to a label....
SqlDataReader READERNAMEHERE = null; //create new reader object//
//create new command to retrieve data//
SqlCommand COMMANDNAMEHERE = new SqlCommand("select * from tablename where collum LIKE 'value', myConnection);
//this command selects all the values from the table where the value appears in the collum//
READERNAMEHERE = COMMANDNAMEHERE.ExecuteReader();
//code above runs the command into the reader and starts it 'reading'//
while(READERNAMEHERE.Read())
{
labelname.Text+=myReader["Collum"].ToString();
}
//while ever the reader is reading, grab the values from the collumn and add them to a multiline text box//
CLOSE THE CONNECTION (Must be done after data has been added / retrieved)
MyConnection.Close(); //myConnection is the name used when creating connection//
Include the reference;
using System.Data.SqlClient;
to connect and open...
SqlConnection myConnection = new SqlConnection("user id=SQLSERVERUSERNAME;" +"password=SQLSERVERPASSWORD;server=SERVER PATH HERE, USE . IF LOCAL;" + "Trusted_Connection=yes;"+ "database=DATABSE TO CONNECT TO; ");
myConnection.Open();
NOTE: Connection must be made EVERY time data is added / retrieved
To Add Data the connected Database....
//create new command to add data//
SqlCommand COMANDNAMEHERE=newSqlCommand("INSERTINTO tablename(collumname) " //here colum names can be comma seperated//
+ "Values (valuetoadd)", myConnection);//here the values to add can be comma seperated//
// myConnection is name of establishd connection//
//this command will insert new value (valuetoadd) into tablename in specified column//
COMMANDNAMEHERE.ExecuteNonQuery();
//run command//
To Retrieve Data from the connected Database and assign to a label....
SqlDataReader READERNAMEHERE = null; //create new reader object//
//create new command to retrieve data//
SqlCommand COMMANDNAMEHERE = new SqlCommand("select * from tablename where collum LIKE 'value', myConnection);
//this command selects all the values from the table where the value appears in the collum//
READERNAMEHERE = COMMANDNAMEHERE.ExecuteReader();
//code above runs the command into the reader and starts it 'reading'//
while(READERNAMEHERE.Read())
{
labelname.Text+=myReader["Collum"].ToString();
}
//while ever the reader is reading, grab the values from the collumn and add them to a multiline text box//
CLOSE THE CONNECTION (Must be done after data has been added / retrieved)
MyConnection.Close(); //myConnection is the name used when creating connection//
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home