Answered by:
database connectivity in asp.net

Question
-
hw to connect to a database in asp.net
Thursday, September 13, 2007 4:53 PM
Answers
-
install MDAC in your system and use oledb
the connection string is
"Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("a1.mdb")
use oledbconnection and oledbcommand classes .......
cheers,
nana
Thursday, September 13, 2007 4:59 PM
All replies
-
install MDAC in your system and use oledb
the connection string is
"Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("a1.mdb")
use oledbconnection and oledbcommand classes .......
cheers,
nana
Thursday, September 13, 2007 4:59 PM -
Kumar,
Have a look at http://www.connectionstrings.com/ for connection strings to almost all the database's available.
Friday, September 14, 2007 11:54 AM -
Please provide more details. There are many types of databases with many possible ways of connecting to them. Most likely you will be wanting to simply connect to a SQL Express database. Firstly, Create a new database or move your existing database to a special folder under the root directory of your web site. Name the folder "App_Data". Now in your web.config file, add the following :
<add name="<insert any name here>" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\<your database name>.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
The System.Data.SqlClient is the best way to connect in this scenario.
Code SnippetSqlConnection sconn = new SqlConnection(ConfigurationManager.ConnectionStrings["<connection string name>"].ToString());
sconn.Open();
SqlCommand sc = new SqlCommand("<whatever operation you want>");
SqlDataReader sdv = sc.ExecuteReader();
while (sdv.Read())
{
this.label1.Text = sdv.GetString(0);
//and so on
}
//data reader will be automatically closed
sconn.Close();
This method is called the "Connected" connection model. Alternatively, there is the Disconnected model which is slightly more difficult and used in particular cases. This should be good enough for your needs
Friday, September 14, 2007 12:51 PM -
First, you will need to import the System.Data.OleDb name space at the top of your page.
Imports System.Data.OleDb
Next, we write the actual code to connect to Access and insert the data. We apply the below commands to the Click Event of a button control. Notice that we are getting our values from the .Text property of the txtFirstName TextBox and the txtLastName TextBox.
Dim cmd As New OleDbCommand("INSERT INTO Customers (FirstName, LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')", New OleDbConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
After we add the data above, we want to display it on the page along with all past records added. This is done by calling a Method that will bind the data to a repeater control.
Dim strSQL As String = "Select * From Customers"
Dim cmd As New OleDbCommand(strSQL, New OleDbConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
The full listing for the code behind page is as follows
Imports System.Data.OleDb
Public Class index
Inherits System.Web.UI.Page#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()End Sub
Protected WithEvents btnInsert As System.Web.UI.WebControls.Button
Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.ObjectPublic strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Server.MapPath("db1.mdb")
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData()
End SubPrivate Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
Dim cmd As New OleDbCommand("INSERT INTO Customers (FirstName, LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')", New OleDbConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End SubSub LoadData()
Dim strSQL As String = "Select * From Customers"
Dim cmd As New OleDbCommand(strSQL, New OleDbConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End SubEnd Class
Friday, September 14, 2007 8:07 PM