1. How to connect a database in MS SQL Server First create...

5
1. How to connect a database in MS SQL Server First create a database using SQL Management Studio. In Visual Studio after creating a new website project click on View -- > Server Exploler. After doing that as you can see in the picture below, Right click on Data Connection and then choose Add Connection option.

Transcript of 1. How to connect a database in MS SQL Server First create...

Page 1: 1. How to connect a database in MS SQL Server First create ...cis.csuohio.edu/~sschung/cis612/HowToConnect... · 1. How to connect a database in MS SQL Server First create a database

1. How to connect a database in MS SQL Server

First create a database using SQL Management Studio.

In Visual Studio after creating a new website project click on View -- > Server Exploler.

After doing that as you can see in the picture below, Right click on Data Connection and then choose

Add Connection option.

Page 2: 1. How to connect a database in MS SQL Server First create ...cis.csuohio.edu/~sschung/cis612/HowToConnect... · 1. How to connect a database in MS SQL Server First create a database

After clicking on Add connection, the following window should open.

If you are using MS SQL SERVER then click on CHANGE button and set it to Microsoft SQL server.

Then Server name drop Down list will show the Server Name. If it doesn't show up, use ./Sqlexpress as a

server name.

Then in the last drop down list it should display all the database names which you have created in SQL

Management Studio. Select a database you want to connect to and click Ok. Before doing that, you

might want to press Test Connection to see if everything is fine.

Page 3: 1. How to connect a database in MS SQL Server First create ...cis.csuohio.edu/~sschung/cis612/HowToConnect... · 1. How to connect a database in MS SQL Server First create a database

After that, you should be able to see the database in the Server Exploler tab. Right click the on Database

Name and click on Properties and then on right side of the 3rd property is Connection String which you

need to copy.

Highlighted part below is the one you have copied which has to be pasted in web.config file as shown in

the next step.

<connectionStrings>

<add name="RS" connectionString="Data Source=SUNNY\SQLEXPRESS;Initial

Catalog=RoomManagementDB;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>

Page 4: 1. How to connect a database in MS SQL Server First create ...cis.csuohio.edu/~sschung/cis612/HowToConnect... · 1. How to connect a database in MS SQL Server First create a database

Now open web.config file using solution explorer and then paste the connection string in web.config file

as follows:

<connectionStrings> <add name="RS" connectionString="Data Source=SUNNY\SQLEXPRESS;Initial

Catalog=RoomManagementDB;Integrated Security=True" providerName="System.Data.SqlClient"/>

</connectionStrings>

Example codes:

Connect to the database using following code.

public class ConnectionManager {

public static SqlConnection GetRoomReservationConnection()

{ // Get the connection string from the configuration file

string connectionString =

ConfigurationManager.ConnectionStrings["RS"].ConnectionString;

//make sure instead of RS you must use appropriate name from the web.config file

// Create a new connection object SqlConnection connection = new SqlConnection(connectionString);

// Open the connection, and return it connection.Open();

return connection;

}

Page 5: 1. How to connect a database in MS SQL Server First create ...cis.csuohio.edu/~sschung/cis612/HowToConnect... · 1. How to connect a database in MS SQL Server First create a database

}

Executing Stored procedure and displaying data on web page

The yellow highlighted part is the name of Stored Procedure and the @Month and @ Year are

parameters in the Stored Procedure.

public static DataTable GetReservationsByMonth(int month,int year)

{

DataTable dt = new DataTable();

using (SqlConnection connection =

ConnectionManager.GetRoomReservationConnection()) {

SqlCommand command = new SqlCommand("GetEventsByMonth", connection);

command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@Month", SqlDbType.Int).Value = month;

command.Parameters.Add("@Year", SqlDbType.Int).Value = year; SqlDataAdapter dataAdapter = new SqlDataAdapter(command);

dataAdapter.Fill(dt);

}

return dt;

}

in page load event

DataTable dt = Reservation.GetReservationsByMonth(month,year); GridView1.DataSource = dt;

GridView1.DataBind();