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!

data access & populating objects

Status
Not open for further replies.

msay

Programmer
Aug 17, 2001
56
US
Hello, I'm new to VB.net upgrading from VB6. I use the following code to in VB6 to access a MS access database and fill a comboBox:

Code:
 Dim db1 As DAO.Database
 Dim rst1 As DAO.Recordset
 Set db1 = DBEngine.OpenDatabase("c:\program files\acs\acs sales.mdb")
   'Open the Recordset
 Set rst1 = db1.OpenRecordset("comm")
 
 rst1.MoveFirst
 While Not rst1.EOF
 Combo4.AddItem rst1!commRate
 On Error Resume Next
 Combo10.AddItem rst1!selfLeadFee
  rst1.MoveNext
  Wend  
  rst1.Close
  db1.Close
My question is what would the code look like for the same process in Vb.net?

I have a start with the following code:

Code:
Private m_cnADONetConnection As New OleDb.OleDbConnection
Private m_daDataAdapter As New OleDb.OleDbDataAdapter
' in the Form1_Load section
m_cnADONetConnection.ConnectionString = _
       "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\program files\acs\acs sales.mdb"
m_cnADONetConnection.Open()

Thanks for any help!!
MS
 
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourDataSoucePath"
Dim SQL As String
Dim DA As OleDbDataAdapter
Dim DS As New DataSet

'Initialize SQL string.
SQL = "SELECT * FROM TableName"

'Initialize the OleDbDataAdapter with the SQL and Connection String.
DA = New OleDbDataAdapter(SQL, conStr)

'Use the OleDbDataAdapter to fill the DataSet.
DA.Fill(DS, "TableName")

'Bind ComboBox.
ComboBox1.DataSource = DS.Tables("TableName")
ComboBox1.ValueMember = DS.Tables("TableName").Columns("yourColumnName").ColumnName
ComboBox1.DisplayMember = DS.Tables("TableName").Columns("yourColumnName").ColumnName

 
Thanks! One more question. If I want to add data to my database from a textbox or combobox what would that look like?
Here's an example from vb6:
Code:
Dim Data1 As DAO.Database
 Dim tbl1 As DAO.Recordset
 Set Data1 = DBEngine.OpenDatabase("c:\program files\acs\acs sales.mdb")
   'Open the Recordset
 Set tbl1 = Data1.OpenRecordset("table1", dbOpenDynaset)
 tbl1.AddNew
 tbl1!clientName = Text11.Text
 tbl1!clientNum = Text10.Text
 tbl1.update
Theres a bit of a learning curve upgrading, so thanks for your help!
 
Code:
Dim objCommand As New SqlCommand

'Open the connection, execute the command.
myConnection.Open()

'Set the SqlCommand object properties.
objCommand.Connection = myConnection
objCommand.CommandText = "INSERT INTO yourTableName " & _
    "(field1, field2) " & _
    "VALUES(@field1, @field2)"

'Add parameters for the placeholders in the SQL in the
'CommandText property.

'Parameter for the field1 column.
objCommand.Parameters.Add("@field1", yourTextBox.Text)

'Parameter for the field2 column.
objCommand.Parameters.Add("@field2", yourTextBox.Text)

'Execute the SqlCommand object to insert the new data.
Try
    objCommand.ExecuteNonQuery()
Catch err As SqlException
    MessageBox.Show(err.Message)
End Try

'Close the connection
myConnection.Close()

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top