handlewithbeer Posted October 8, 2003 Posted October 8, 2003 I am new to dotNet. I need help with accessing an Access database. In the times of vb6, I'd use the adodb .Recordset and .Connection objects. How can I do the same with the ADO.Net objects. I know I need to Import system.data.oledb and system.data in the GenDec section. So what is the new thing for recordsets? and any code examples would be really, REALLY appreciated. Thanks in advance, HandleWithBeer (aka the Beer Baron :)) Quote
*Experts* Volte Posted October 8, 2003 *Experts* Posted October 8, 2003 (edited) You need to use an OledbConnection object (or SqlConnection for SQL Server) to connect. Then there are a few different ways to get data. For example, you can then do something like this: ' make a connection object called dc and connect to the database Dim cmd As New OleDbDataCommand("SELECT * FROM People", dc) Dim adapter As New OleDbDataAdapter(cmd) Dim data As New DataSet() adapter.Fill(data)DataSet then contains the information retrieved. There's too much too tell you here, but there's lots of ADO.NET reading material the MSDN. Edited October 8, 2003 by Volte Quote
handlewithbeer Posted October 8, 2003 Author Posted October 8, 2003 The code I'm using to try(emphasis on try) and connect to access. Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:\ata\60feet\60feet.mdb") MyConnection.Open() MsgBox(MyConnection.State) Dim MyCommand As New OleDbCommand("select * from suppliers", MyConnection) Dim MyReader As OleDbDataReader = MyCommand.ExecuteReader() MyReader.GetData() MsgBox("Supplier id is " & MyReader("SupplierID")) MyConnection.Close() MyReader.Close() MyCommand.Dispose() Thanks again. Quote
*Experts* Volte Posted October 8, 2003 *Experts* Posted October 8, 2003 Well, you didn't say what the problem was... However, if you want to loop through all the records, you need to loop:Do While MyReader.GetData() 'do stuff with the current record Loop Quote
handlewithbeer Posted October 8, 2003 Author Posted October 8, 2003 Sorry, The problem is that it's telling me that the data/record does not exits, even though I put some data in the dBase manually for testing. I guess my biggest problem is getting my head around the new syntax, such as the recordset obj not being used anymore. Thanks again. Quote
*Experts* Volte Posted October 8, 2003 *Experts* Posted October 8, 2003 Oh, I see the problem. You need to pass the ordinal that you want to get into the Get method that corresponds to the type (GetString, GetInt32, GetBoolean, etc). So in your case:MessageBox.Show("Supplier id is " & MyReader.GetInt32(MyReader.GetOrdinal("SupplierID")) Quote
handlewithbeer Posted October 8, 2003 Author Posted October 8, 2003 So you're saying that it no longer flies to get an item from a database (old school would look like txtName=rs!fldName or whatever), and I need to specify a type that will be read from the database? Or can I get the dataReader object to get information without specifying the type? Quote
*Experts* Volte Posted October 8, 2003 *Experts* Posted October 8, 2003 Well, you can use the GetData method rather than specifying the type, but it will return an Object, which you should cast to whatever type you will be using anyway. If you are blindly extracting data without knowing the type, GetData is the way to go. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.