Access database at runtime using ADO.NET
Steps to connect to database:- Declare and initialize database connection object with appropriate connection string.
- Create a DataAdapter and a Dataset object.
- Using fill method of the DataAdapter object, load the data into the Dataset Object.
- Display the data using data binding feature of controls like Datagrid or Datalist.
Code to connect to database using VB.NETDim m_adptEmp As SqlDataAdapter
Private Sub Page_Load(ByVal sender As System.Object, _ByValeAsSystem.EventArgs) HandlesMyBase.Load<BR> DimEmpConnAsNewSqlConnection_<BR> ("server=(local);database=Employees;Trusted_Connection=yes")
m_adptEmp = New SqlDataAdapter("select * from Emp", EmpConn)
Dim dsEmp As New DataSet
m_adptEmp.Fill(dsEmp, "Emp")
DataGrid1.DataSource = dsEmp.Tables("Emp").DefaultView
DataGrid1.DataBind()
End Sub