Thomas Sampson

Notes on SQL Server

Leave a comment

 

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//

Advertisements

Author: tomtech999

I have recently graduated with a 1st class degree in MComp Games Software Development at Sheffield Hallam University, focusing primarily on application development in C++, with experience in graphics programming, scripting languages, DVCS/VCS and web technology. In my spare time I enjoy Drumming, Reading and Snowboarding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s