Connecting to a DataBase like i did in VB 6

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi all,

I wrote a cool little database program a while back, in VB6, and im
intending to rewrite it in .net.

I am new(ish) to .net, but an old hand at VB5/6.

In VB i would access the mdb file something like this.

set db=database
set db=opendatabase("mydb.mdb")
dim rec as recordset
set rec= db.openrecordset("select name form names")

while rec.eof=false
debug.write(rec!name)
wend


How do i do the same in .net, it seems to want me to set up these pages ets,
but i need to create queries and joins on the fly so data can be accessed as
the user requests.

Ive looked in the help, and followed the tutorials, but i cant seem to be
able to query the db properly, or even connect to it in the way that i want.

Thanks in advance

Chris
 
Use the OleDdDataAdapter, Connection and Command controls in winforms to
generate the strings that you require for your app. To manipulate these
strings programmatically try using the stringbuilder class.
 
Chris,

Without all error checking.
\\\
Dim ds as new dataset
Dim Conn As New OleDbConnection(Microsoft.Jet.OLEDB.4.0;Data
Source="C:\mydb.Mdb")
Dim da As New OleDbDataAdapter("Select name from names",Conn)
da.Fill(ds)
for each dr as datarow in ds.tables(0).rows
console.write(dr("name").ToString)
next
////

I hope this helps?

Cor
 
Something like this:

Dim mydbConnection As OleDb.OleDbConnection
mydbConnection.ConnectionString = "c:\mydb.mdb"
mydbConnection.Open()

Dim mydbCommand As OleDb.OleDbCommand
mydbCommand.Connection = mydbConnection
mydbCommand.CommandText = "SELECT * FROM MyTable"

Dim myreader As oleDB.OleDbDataReader

myreader = mydbCommand.ExecuteReader

You can now inspect the myreader object the same way as you did with your
recordset.
 
Thanks Nick thats great, but how do i get at the data?

eg.

debug.writeline(myreader!name)

just throws an error. AAh i know what i wnnt, but not how to sintax it.

BTW, all the objects need to be new(ed) on the dims.
 
Chris,
Use
Do While myreader.Read ' assuming I've got the VB syntax for this
loop
Dim myVar AS string = myreader.GetString(0) ' for a string at the
first field returned
' also see GetOrdinal to lookup the column #
from the name
Dim myInt AS int =
myreader.GetInt32(myreader.GetOrdinal("myFieldName"))
Loop

Ron Allen
 
Sorry ron but that does nothing but throw erreors.

I think i am missing a consecpt here, or .NET has taken DB programing and
made it so trikey that no one can use it propperly.

All i wnat is this translated into .NET, ive just complied it and got it
running in VB6, took me 5mins. In fact it took me less time to write that
code than it did to write this message. Unlike 2+ days for .NET to do the
same thing and still no joy.

here is excatly what i wnat converted, i need the VAR names to remain if
possible.
db.mdb has one table (Users) with 2 collums, (Name, Password)


///
dim DB as Database ' i wnat to use the db everywher in the program so i
only want to open it once.

sub main()

set DB = opendatabase("C:\db.mdb") 'open DB

dim qry as string = "Select * from users;" 'set query
dim usr as recordset 'create an empty
record set

set usr = db.openrecordset(qry) ' populate record set

while usr.eof = false ' loop unless
end of record set is reached
debug.writeline(usr!Name) 'dump name from
current record
debug.writeline(usr!Password) 'dump password from
current record
usr.movenext 'move to
next record
end while.

end sub
///

how can this be so diffacult in .NET !!!
 
Chris,
It doesn't seem that difficult to me. In C# I'd write

void main() // or some other function called at startup.
{
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\db.mdb;Mode=Share Deny None;";
OleDbConnection db = new OleDbConnection(connStr)
OleDbCommand cmd = new OleDbCommand("Select Name, Password FROM Users",
db);
_db.Open();
OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
System.Diagnostics.Debug.WriteLine(rdr.GetString(0));
System.Diagnostics.Debug.WriteLine(rdr.GetString(1));
}
_db.Close(); // close the connection
}

Note that to use the db variable in other modules you will have to make it
public and specific to the module and pass the class it is in to those
programs. Also make sure that your main doesn't just return after writing
these as that will cause the program to exit. I do this by having my own
class for doing database manipulation. I pass in an open connection or a
database name/server nam (filename for OleDb) and then do all the
manipulation there.

You may want to visit microsoft.public.dotnet.framework.adonet as that
is dedicated to ADO.NET questions.

Also the book by David Sceppa "ADO.NET Core Reference" is quite good and
has a lot of examples in both C# and VB.NET

Ron Allen
 
Chris,

I am really stumbled, I made a sample exactly as yours however now in VBNet
way and you even did not look to it.

Cor
 
Back
Top