Wednesday, March 28, 2012

I dont know how to connect with Ms Sql

Hello,

I am new at asp.net. I want to design a website using asp.net as frontend and sql database as backend. I am able to connect and add,update as well delete records when I use MsAcess and Asp.net using the following connection strings...
**********
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("db1.mdb"))
dbconn.Open()
sql="SELECT * FROM table1"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
data1.DataSource=dbread
data1.DataBind()
dbread.Close()
dbconn.Close()
end sub

**********

But I don't know how to connect with Ms Sql.

I have MS SQL Server 2003 in my computer aleady Installed.

Please Tell me the exact code or procedure to make connection with MS SQL Server.

HELP ME!!!!

The equivelent code for connecting to SQL server is:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim dbconn As SqlConnection = Nothing
Dim dbread As SqlDataReader = Nothing

Try
dbconn = New SqlConnection("server=;uid=;password=;database=;")

Dim sql As String = "SELECT * FROM table1"
Dim dbcomm As New SqlCommand(sql, dbconn)

dbconn.Open()
dbread = dbcomm.ExecuteReader()

data1.DataBind()
Finally
If Not dbconn Is Nothing Then
If dbconn.State = ConnectionState.Open Then
dbconn.Close()
End If
End If

If Not dbread Is Nothing Then
If Not dbread.IsClosed Then
dbread.Close()
End If
End If
End Try
End Sub

HTH,
Ryan

sql

No comments:

Post a Comment