26. marts 2005 - 22:04Der er
21 kommentarer og 1 løsning
vb.net & mysql
Hejsa, jeg har snart søgt google helt til ende for at finde et simpelt script der connecter vb.net til mysql, men uden held, et jeg dog har prøvet... Men uden held er det her på denne side
Super simpelt eksempel (console app med ByteFX driver):
Imports System Imports System.Data Imports ByteFX.Data.MySqlClient
Class MainClass Public Shared Sub Main(ByVal args As String()) Dim con As MySqlConnection = New MySqlConnection ("Database=Test;Data Source=localhost;User Id=;Password=") con.Open Dim sel As MySqlCommand = New MySqlCommand ("SELECT * FROM T1", con) Dim rdr As MySqlDataReader = sel.ExecuteReader While rdr.Read Dim f1 As Integer = rdr(0) Dim f2 As String = rdr(1) Console.WriteLine(f1 & " " & f2) End While rdr.Close con.Close End Sub End Class
Imports System Imports System.Data Imports System.Drawing Imports System.Windows.Forms
Imports MySql.Data.MySqlClient
Namespace DefaultNamespace Public Class MainForm Inherits Form Private dg As DataGrid Private save As Button Private con As MySqlConnection Private da As MySqlDataAdapter Private cb As MySqlCommandBuilder 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 MySqlConnection ("Database=Test;Data Source=localhost;User Id=;Password=") con.Open da = New MySqlDataAdapter("SELECT * FROM T1", con) cb = New MySqlCommandBuilder(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
Error 1 Namespace or type specified in the Imports 'System.data' cannot be found. C:\Documents and Settings\Thomas Larsen\Lokale indstillinger\Application Data\Temporary Projects\Test mysql\Form1.vb 2 8 Hmmm den der hedder imports system.data giver ovenstående fejl, ved ikke helt hvad den betyder ?
Nej ikke om database(mysql) skal bare lige være så man trykker på en cmdbtn og så skal den vise noget data i et datagrid, det må da kunne laves mere simplere en det her ??+??
simplere... ?? Det er så simpelt det overhovedet kan blive. Du får lige noget kode genereret i Visual Studio, men vi på det kraftigste opfordre dig til at læse en bog og kode i notepad indtil du forstår principperne i VB.Net, og derefter kan du gå hen og bruge Visual Studio.
Imports System.Data.OleDb
Public Class Form1 Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer. InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.DataGrid1 = New System.Windows.Forms.DataGrid Me.Button1 = New System.Windows.Forms.Button CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'DataGrid1 ' Me.DataGrid1.DataMember = "" Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid1.Location = New System.Drawing.Point(8, 16) Me.DataGrid1.Name = "DataGrid1" Me.DataGrid1.Size = New System.Drawing.Size(240, 160) Me.DataGrid1.TabIndex = 0 ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(192, 216) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 1 Me.Button1.Text = "Button1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.DataGrid1) Me.Name = "Form1" Me.Text = "Form1" CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connString As String = "hej" Dim query As String = "hej"
Dim conn As MySqlConnection = New MySqlConnection(connString) Dim adap As MySqlDataAdapter = New MySqlDataAdapter(query, conn)
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.