Friday, March 23, 2012

How to Connect SQL server Using ASP.net

Dear ASP.NET user...

I'm a new user in ASP.NET...
i have a problem facing now...
I'm using SQL server as my database...
how do i CONNECT my web application using ASP.NET to SQL server??

What should i do?
any coding do i need to insert to my web HTML?

what i heard from my friend...she said web config can connect the web application to SQL server...?...it that work?

pls help me guys and gals...

From jhwong2610
URGENTVery often the web.config is used to store the connection string - that alone does not connect you to a database - using the connection string you have to open a sqlConnection and then proceed from there.

My best recommendation would be to pick up one of the ASP.NET books and look at one of the examples that uses data binding.|||Hi:
Like This


Dim Conn As New SqlConnection("ConnectionStringHere")
Dim Conn As New SqlCommand("Select * From Table", Conn)
Dim DRs As SqlDataReader
Conn.Open()
DRs = Comm.ExecuteReader()
If DRs.Read() Then
Response.Write(DRs("Column"))
End If
DRs.Close()
Conn.Close()
|||Add this to the web.config file:

<configuration>
<appSettings>
<add key="SQLConnStr" value="<your connection string" />
</appSettings
...

You can use the connectionstring in the code like this:

Dim str as connStr = System.Configuration.ConfigurationSettings.AppSettings("SQLConnStr")

No comments:

Post a Comment