09. december 2004 - 19:39Der er
13 kommentarer og 1 løsning
hente fra access helt fra bunde
Hej.
Jeg er helt ny indefor det her.
Nogen der kan hjælpe mig med et helt project der viser hvordan jeg henter data fra min access DB. Skal gerne være lige til at afprøve som exe-fil.... og selvfølgelig også med sourcefiler..
Class MainClass Public Shared Sub Main(ByVal args As String()) Dim con As OleDbConnection = New OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Database\MSAccess\Test.mdb;User Id=admin;Password=") con.Open Dim cmd As OleDbCommand = New OleDbCommand ("SELECT * FROM T1", con) Dim rdr As OleDbDataReader = cmd.ExecuteReader While rdr.Read Dim f1 As Integer = CType(rdr(0), Integer) Dim f2 As String = CType(rdr(1), String) Console.WriteLine(f1 & " " & f2) End While con.Close End Sub End Class
GUI app som bruger data adapter og data set:
Imports System Imports System.Data Imports System.Data.OleDb Imports System.Drawing Imports System.Windows.Forms
Namespace DefaultNamespace Public Class MainForm Inherits Form Private dg As DataGrid Private save As Button Private con As OleDbConnection Private da As OleDbDataAdapter Private cb As OleDbCommandBuilder Private ds As DataSet
Public Shared Sub Main Dim fMainForm As New MainForm fMainForm.ShowDialog() End Sub
Public Sub New() MyBase.New Me.InitializeComponent End Sub
Private Sub InitializeComponent() con = New OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Database\MSAccess\Test.mdb;UserId=admin;Password=") con.Open da = New OleDbDataAdapter("SELECT * FROM T1", con) cb = New OleDbCommandBuilder(da) da.UpdateCommand = cb.GetUpdateCommand ds = New DataSet da.Fill(ds, "T1") dg = New DataGrid save = New Button SuspendLayout dg.Location = New Point(50, 50) dg.Size = New Size(300, 200) dg.SetDataBinding(ds, "T1") save.Location = New Point (50, 300) save.Size = New Size (100, 25) save.Text = "Save" AddHandler save.Click, AddressOf SaveClick ClientSize = New Size(400, 400) Controls.Add(dg) Controls.Add(save) Text = "Main Form" ResumeLayout(false) End Sub
Sub SaveClick(ByVal sender As Object, ByVal e As EventArgs) da.Update(ds, "T1") ds.AcceptChanges dg.Refresh End Sub End Class End Namespace
og der er masser af andre måder at gøre det på ...
Men bemærk at valg af data reader eller data adapter + data set er uafhængigt af om det er console app eller GUI app eller ASP.NET side eller noget helt fjerde ...
Error 1 Namespace or type specified in the Imports 'System.Data.OleDb' cannot be found. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 4 8 Error 2 Namespace or type specified in the Imports 'System.Data' cannot be found. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 3 8 Error 4 Type 'OleDbCommandBuilder' is not defined. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 15 22 Error 5 Type 'OleDbDataAdapter' is not defined. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 14 22 Error 6 Type 'OleDbConnection' is not defined. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 13 23 Error 7 Type 'OleDbConnection' is not defined. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 29 22 Error 8 Type 'OleDbDataAdapter' is not defined. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 31 21 Error 9 Type 'OleDbCommandBuilder' is not defined. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 32 21 Error 10 Type 'DataSet' is not defined. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 34 21 Error 3 Type 'DataSet' is not defined. C:\Documents and Settings\JHA\Dokumenter\Visual Studio\Projects\WindowsApplication3\WindowsApplication3\Form1.vb 16 22
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.