| Author |
Topic Search Topic Options
|
VBScript
Senior Member
Joined: 14 July 2004
Location: United Kingdom
Status: Offline
Points: 219
|
Post Options
Thanks(0)
Quote Reply
Topic: Database stuff... Posted: 29 August 2006 at 11:09am |
|
I'm trying to learn ASP .Net (VB) and I can't figure out how to connect to a database using a method similar to using the ADODB.Connection and Recordset objects that you use in ASP.
Can someone give me an example of how to connect to a database using a method that allows MS SQL, mySQl and Access?
Thanks in advance.
|
|
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
Posted: 29 August 2006 at 3:08pm |
|
Well you can still use ADODB but why would you?
ASP.nets' beauty is that you can create Data Providers, so you create one for SQL Server maybe even one for ADODB but if you can create specialized ones. There is a mySQL .net Provider you can use as opposed to adodb.
|
|
|
 |
VBScript
Senior Member
Joined: 14 July 2004
Location: United Kingdom
Status: Offline
Points: 219
|
Post Options
Thanks(0)
Quote Reply
Posted: 29 August 2006 at 3:15pm |
|
Umm... ok....
Could you show me an example of how I can do this. I just want something that behavies like ADODB in the fact that I can place all the tables data into an array (arrData = rsCommon.GetRows()) etc...
All the examples I have found have used things like datagrids which i dont want to use.
|
|
|
 |
Freon22
Groupie
Joined: 04 December 2005
Status: Offline
Points: 42
|
Post Options
Thanks(0)
Quote Reply
Posted: 31 August 2006 at 5:21am |
It is a little bit of a pain moving from vbscript to vb.net. But so far the closest thing I can find to our old way of doing thing is using the reader.
Here is an example of how I fill a dropdownlist using the reader.
Private Sub FillNameList()
NameList.Items.Clear()
'Define the Select statement.
Dim selectSql As String = "SELECT name, name_ID FROM tbNames"
'Define the ADO.NET objects.
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(selectSql, con)
Dim reader As SqlDataReader
'Try to open database and read information.
Try
con.Open()
reader = cmd.ExecuteReader()
'For each item, add the name to the displayed list box text,
'and store the name ID in the Value property.
Do While reader.Read()
Dim newItem As New ListItem()
newItem.Text = reader( "name")
newItem.Value = reader( "name_ID").ToString()
NameList.Items.Add(newItem)
Loop
'I want the default value in my dropdownlist be.
NameList.Items.Insert(0, New ListItem("---Choose One---", ""))
reader.Close()
Catch err As Exception
lblResults.Text = "Error in reading list of names. "
lblResults.Text &= err.Message
Finally
con.Close()
End Try
End Sub
|
The reader works a little like our old recordset, not the same but? Hope this helps some, you may want to get a good book on asp.net.
|
 |
VBScript
Senior Member
Joined: 14 July 2004
Location: United Kingdom
Status: Offline
Points: 219
|
Post Options
Thanks(0)
Quote Reply
Posted: 31 August 2006 at 8:08am |
|
Thats great!
Do you know how to put the records into an array like: rsCommon.GetRows()
|
|
|
 |
MadDog
Mod Builder Group
Joined: 01 January 2002
Status: Offline
Points: 3008
|
Post Options
Thanks(0)
Quote Reply
Posted: 31 August 2006 at 8:16am |
|
ASP.Net doesnt support getRows.
I researched this months ago when trying to do something with ASP.Net
|
|
|
 |
Freon22
Groupie
Joined: 04 December 2005
Status: Offline
Points: 42
|
Post Options
Thanks(0)
Quote Reply
Posted: 31 August 2006 at 3:22pm |
Here is a tutorial on arraylist, you can use the datareader to fill the arraylist. The problem that I have is alot of the example that I find on the net is using C#. I guess there are more C programmer out there then VB programmer. lol Anyway I have had to take an example that was written in C# try to understand it, then rewrite it in VB. What a pain!
If you find any good infor out there please share. 
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
Posted: 31 August 2006 at 5:46pm |
|
If you have trouble translating code from c# use a website like this: http://www.carlosag.net/Tools/CodeTranslator/Default.aspx to do it for you. works well enough to get most things done.
As far as getrows goes. Once you populate the reader you have already a collection, why put it in another one and not just use the reader. You need to get off the asp mindset to do efficient asp.net coding otherwise you are just renaming files not doing much towards oop
|
|
|
 |