Sample code for sample database form
-
Hi, suppose that I have an Ms access database and I want to read its value from VB.NET and represent database to the form, I also want to perform the following basic action: Add new record, Update record and Delete record. Could any one give me a sample code (both for connect to the database and read/write the information from/to the database)?
A thousand mile of journey, begin with the first step. APO-CEDC Save Children Norway-Cambodia Office
-
Hi, suppose that I have an Ms access database and I want to read its value from VB.NET and represent database to the form, I also want to perform the following basic action: Add new record, Update record and Delete record. Could any one give me a sample code (both for connect to the database and read/write the information from/to the database)?
A thousand mile of journey, begin with the first step. APO-CEDC Save Children Norway-Cambodia Office
Imports System.data.oledb Private sub ExtractData() 'connection Dim oConn As New OleDbConnection 'command Dim oCmd As New OleDbCommand 'dataset Dim oDS As New DataSet 'dataadapter with command assigned Dim oDA As New OleDbDataAdapter(oCmd) 'set connection string, you'll have to fill that bit in though 'should be easy to find if you don't know how to form it oConn.ConnectionString = "Provider...." oCmd.CommandType = CommandType.Text oCmd.Connection = oConn oCmd.CommandText = "SELECT * FROM Table1" oDA.Fill(oDS) 'You then have a dataset to work with. If you know SQL then you should be able to adapt that to perform all actions Many Thanks Harry
-
Imports System.data.oledb Private sub ExtractData() 'connection Dim oConn As New OleDbConnection 'command Dim oCmd As New OleDbCommand 'dataset Dim oDS As New DataSet 'dataadapter with command assigned Dim oDA As New OleDbDataAdapter(oCmd) 'set connection string, you'll have to fill that bit in though 'should be easy to find if you don't know how to form it oConn.ConnectionString = "Provider...." oCmd.CommandType = CommandType.Text oCmd.Connection = oConn oCmd.CommandText = "SELECT * FROM Table1" oDA.Fill(oDS) 'You then have a dataset to work with. If you know SQL then you should be able to adapt that to perform all actions Many Thanks Harry
-
Thank you very much Harry, Now I try to adapt with your code. I will let you know when I have a problem during my development process. :)
A thousand mile of journey, begin with the first step. APO-CEDC Save Children Norway-Cambodia Office
I would like to add, to have this all take care of it for you. So it'll update I would suggest using the command builder componet. Dim cmb As New OleDbCommandBuilder oDA.SelectCommand = cmd2 Then add your row to the dataset, or modify it's data all you have to call is. oDA.Update(ds) That'll take care of your updates as it automatically fills in the Update, Add, Delete command strings for you. Hope this helps as a suggestion. Gerry
-
I would like to add, to have this all take care of it for you. So it'll update I would suggest using the command builder componet. Dim cmb As New OleDbCommandBuilder oDA.SelectCommand = cmd2 Then add your row to the dataset, or modify it's data all you have to call is. oDA.Update(ds) That'll take care of your updates as it automatically fills in the Update, Add, Delete command strings for you. Hope this helps as a suggestion. Gerry
I have a little bit problem with ADO.NET. The reason that I have a problem is an experience with ADO from the ground. In VB6 I used to connect to database using vb6 and access database with DAO. I have used ADO for one time only with VB6 when I connect to SQL server. But it is one year ago and may be I have to check my source to make sure how it work. When I move to VB.NET I have a problem with OleDbDataAdapter and SqlDataAdapter and other component... (like DataTable, DataRow, DataSet...). I really difficult to figure out with this abstract technology. I think the only way that I should do is return to ADO and master it as soon as possible, than I might have a change to master ADO.NET in VB.Net.
A thousand mile of journey, begin with the first step. APO-CEDC Save Children Norway-Cambodia Office
-
I have a little bit problem with ADO.NET. The reason that I have a problem is an experience with ADO from the ground. In VB6 I used to connect to database using vb6 and access database with DAO. I have used ADO for one time only with VB6 when I connect to SQL server. But it is one year ago and may be I have to check my source to make sure how it work. When I move to VB.NET I have a problem with OleDbDataAdapter and SqlDataAdapter and other component... (like DataTable, DataRow, DataSet...). I really difficult to figure out with this abstract technology. I think the only way that I should do is return to ADO and master it as soon as possible, than I might have a change to master ADO.NET in VB.Net.
A thousand mile of journey, begin with the first step. APO-CEDC Save Children Norway-Cambodia Office
-
Thank you very much for your comment. Because MSDN online is very big, sometime I could not find the topic that I want. If you know the URL please provide some to me because I really want to get start ADO.NET from the first step.
A thousand mile of journey, begin with the first step. APO-CEDC Save Children Norway-Cambodia Office