Tuesday, February 26, 2008

ADO.NET Hello World !!!

Today I learned how to connect to Microsoft SQL database from ASP.NET page using ADO.NET.
Here is the Page Load method which connects to the database and assigns the values taken from the database to the dropdownlist control.

protected void Page_Load(object sender, EventArgs e)

// SQL Connection
SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = "Data Source=COMPUTERV22;Initial Catalog=CIMNET2;User ID=sa;Password=manager;";

//SQL Command
SqlCommand MyCommand = new SqlCommand();
MyCommand.Connection = MyConnection;
MyCommand.CommandText = "Select * from Employees";

//Open a connection to the database

// SQL Reader, place where rows from database is stored.
SqlDataReader MyReader;
MyReader = MyCommand.ExecuteReader();

// Read each record from the database
while (MyReader.Read())

//Close the connection to the database


No comments: