A SqlConnection is an object, just like any other C# object. Most of the time, you just declare and instantiate the SqlConnection.
Ado.net will contain following steps.
Ado.net will contain following steps.
- Instantiate the SqlConnection.
- Open the connection.
- Pass the connection to other ADO.NET objects.
- Perform database operations with the other ADO.NET objects.
- Close the connection.
using (SqlConnection connection = new SqlConnection(connectionString)) { // Create the Command and Parameter objects. SqlCommand command = new SqlCommand("Employee_Insert"
, connection); command.Parameters.AddWithValue("@CityName", txtCity.Text); command.Parameters.AddWithValue("@Name", txtName.Text); command.CommandType = CommandType.StoredProcedure; try{ connection.Open(); command.ExecuteNonQuery(); } catch (SqlException ex){ throw ex; } catch (Exception ex){ throw ex; } finally{ connection.Close(); } }
Database connection in ado.net |
No comments:
Post a Comment