Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VWD - ASP 2.0 - Database Connections

Status
Not open for further replies.

dhrehor

IS-IT--Management
Jun 16, 2005
23
US
Wondering if anyone can tell me (a newbie) how to connect to a SQL database and pull fields and assign them to variables.

A sample script would be sufficient.

Here some sample data:

Database Fields: LastName, FirstName, MiddleName

Variables: varLastName, varFirstName, varMiddleName


I hope this makes sense and as always, thank you in advance for the assistance.

Don
 
By the way, I am doing the site in Visual Web Developer via C#.

Don
 
Here's an example on how to connect to a SQL Server database and loop through the records - once you loop through the records you can assign whatever variables you wish:

faq855-5662


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Are you able to connect to the database via the database explorer? I had troube with that for SQL Server Express Edition (2005). When you connect the database you need to put "computername\sqlexpress" (I believe that is correct).

Just thought I would mention that in case anyone runs into it - although I'm sure it has been mentioned many times on here.
 
I like this idea:

Dim MyConnection As System.Data.SQLClient.SqlConnection
Dim MyDataAdapter As SQLClient.SQLDataAdapter
Dim MyDataTable As New DataTable
Dim MyDataRow As DataRow
Dim strSQL As String = "SELECT FIELD1, FIELD2 FROM MYTABLE"

MyConnection = New System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=MyDatabase;uid=USERNAME;pwd=PASSWORD")
MyConnection.Open()
MyDataAdapter = New SQLClient.SQLDataAdapter(strSQL, MyConnection)
MyDataAdapter.Fill(MyDataTable)

' Loop through DataTable
For Each MyDataRow In MyDataTable.Rows
Response.Write(MyDataRow.Item(0))
Next



Now my next question is that as we loop thru the MyDataRow, how can I populate FIELD1 and FIELD2 into variables varField1 and varField2? Using c# of course.

Again, sorry for the newbie questions .. but I am learning.

Don
 
Instead of this section:
Code:
    ' Loop through DataTable
    For Each MyDataRow In MyDataTable.Rows
            Response.Write(MyDataRow.Item(0))
    Next
You would do:
Code:
    ' Loop through DataTable
    For Each MyDataRow In MyDataTable.Rows
            varField1 = MyDataRow.Item(0)
            varField2 = MyDataRow.Item(1)
    Next
Obviously if there are more than one record, the variables will be changed with each loop.



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top