Visual Basic 2010 Express Connect to MySQL Database

0 comments

Posted on 11th July 2011 by Admin in Visual Basic .Net |Visual Basic 2010

, ,

This tutorial will guide you on how to connect MySQL Database to your visual basic 2010 express. Visual basic 2010 express doesn’t show a wizard connection to MySQL database; please see the picture in figure 1 it shows 3 data sources only Microsoft Access Database File, Microsoft SQL Server Compact 3.5 and Microsoft SQL Database File. It means you cannot connect your MySQL Database file using the data source wizard.

Figure 1.Follow these few steps to connect your MySQL Database to Visual Basic 2010 Express

1. Download and install the MySQL Connector Net 6.4.3 which is can be downloaded at this link
http://dev.mysql.com/downloads/connector/net/
2. Open your visual basic 2010 express and create a new project as shown in the picture below.

3. Next step, go to the ‘Project’ menu and click the ‘Add Reference’, check the picture below.

4. Add Reference form will show and by default Project tab, select the Browse tab and locate the MySql.Data.dll

For Windows Vista file located at Program Files->MySQL->MySQL Connector Net 6.4.3->Assemblies->v4.0->MySql.Data.dll

For Windows 7 file located at Program Files (x86)->MySQL->MySQL Connector Net 6.4.3.->Assemblies->v4.0->MySql.Data.dll

5. After adding the MySql.Data.dll reference then you are now ready to connect your MySQL database with visual basic 2010 express. Please try it to yourself or you may follow the sample code provided below.

Code for VB.Net 2010 Express

Imports MySql.Data.MySqlClient
Public
Class Form1

Dim
con As MySqlConnection = New MySqlConnection(“Data Source=192.168.1.104;Database=test;User ID=root;Password=mypassword;”)

Dim
sql As MySqlCommand = New MySqlCommand(“SELECT * FROM Employee”, con)

Dim
ds As DataSet = New DataSet()

Dim
DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()

Dim
Comb As MySqlCommandBuilder

‘Please add datagridview to your form and name it datagrid1
‘This will display the data to datagrid view

Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

con.Open()

DataAdapter1.SelectCommand = sql

DataAdapter1.Fill(ds, “Employee”)

DataGridView1.DataSource = ds

DataGridView1.DataMember = “Employee”

con.Close()

End
Sub

‘Update database. Change any data from datagrid then click this button it will save any changes from the grid
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Comb = New MySqlCommandBuilder(DataAdapter1)

Dim
i As Integer = DataAdapter1.Update(ds.Tables(“Employee”))

MessageBox
.Show(“modify the number “ & i.ToString & ” rows”)

End
Sub

‘Sample insert record to MySQL database
Private
Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim
MyCommand As New MySqlCommand

con.Open()

MyCommand.Connection = con

MyCommand.CommandText = “INSERT INTO Employee(IDNo,FirstName,LastName,email) VALUES( ‘” & txtID.Text & “‘,’” & txtfirstname.Text & “‘,’” & txtlastname.Text & “‘,’” & txtemail.Text & “‘)”

MyCommand.ExecuteNonQuery()

con.Close()

MsgBox(“Record successfully added!”)

End Sub

End Class

Insert Record/Data that Contain Quotes in Visual Basic

0 comments

Posted on 9th July 2011 by Admin in Database Programming |Visual Basic .Net |Visual Basic 6

,

‘This visual basic shows how to use ADO to insert records into a database when fields contain quotes

‘The program creates a SQL INSERT statement. To protect against quotes in the
‘user-entered values, the program replaces each single quote with two single
‘quotes. The database replaces the pairs of quotes with single quotes when it
‘inserts the values. For example, the database treats the text “Tess’s Bakery” as
‘”Tess’s ” when it adds it to the database.

Private Sub cmdInsert_Click()
Dim DB_Name As String
Dim SQL_Str As String
Dim Conn As ADODB.Connection
Dim ctl As Control
‘ Get the data.
DB_Name = App.Path
If Right$(DB_Name, 1) <> “” Then DB_Name = DB_Name & “”
DB_Name = DB_Name & “Employee.mdb”
‘ Open a connection.
Set Conn = New ADODB.Connection
Conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;” & “Data Source=” & DB_Name & _
“;” & “Persist Security Info=False”
Conn.Open
‘ Compose the INSERT statement.
SQL_Str = “INSERT INTO Addresses “ & “(Name, Street, City, State, Zip) “ & ” VALUES (“ & _
“‘” & Replace$(txtName.Text, “‘”, “””) & “‘, “ & “‘” & _
Replace$(txtStreet.Text, “‘”, “””) & “‘, “ & “‘” & _
Replace$(txtCity.Text, “‘”, “””) & “‘, “ & “‘” & _
Replace$(txtState.Text, “‘”, “””) & “‘, “ & “‘” & _
Replace$(txtZip.Text, “‘”, “””) & “‘” & “)”
‘ Execute the statement.
Conn.Execute SQL_Str, , adCmdText
‘ Close the connection.
Conn.Close

‘ Clear the TextBoxes.

For Each ctl In Controls
If TypeOf ctl Is TextBox Then
ctl.Text = “”
End If
Next ctl

End Sub

How to Import SQL Server data into an MS Access Database

0 comments

Posted on 1st July 2011 by Admin in Database Programming |Visual Basic 6

, ,

‘Import SQL Server data into an Access database
‘You can quickly import SQL Server data into an Access database using an
‘Execute statement in either ADO or DAO

Private Sub Import_Data()

Dim Conn As ADODB.Connection
Dim SQL_STR As String
‘Create ADO Connection Object
Set Conn = New ADODB.Connection
Conn.Open “Driver={Microsoft Access Driver (*.mdb)};” & “Dbq=ExportedData.mdb;” & _
“DefaultDir=d:Temp;” & “Uid=Admin;Pwd=;” SQL_STR = “SELECT * INTO “ & “[table_data] “ & “FROM “ & “[ODBC;Driver=SQL Server; " & _
"SERVER=ServerName;DATABASE=MyTable;" & "UID=MyUser;PWD=MyPassword;].” & _
“[table_data];”
Conn.Execute SQL_STR
Set Conn = Nothing

End Sub

Note : Please change the following :

ServerName = Your SQL Server Name or Address
UID = Your username to access the database
PWD = Your database password

‘you must create a temporary folder in drive c:, please see sample below
c:tempExportedData.mdb

Visual Basic 2010 – Statement Lambda Sample

0 comments

Posted on 4th May 2010 by Admin in Visual Basic 2010

, ,

Statement lambdas allow you to declare procedures with multiple statements inside other procedures.This sample demonstrates the use of statement lambdas in the following scenarios:

  • Single Line Sub/Function
  • Multiple Line Sub/Function
  • Multiple Line Function with AS clause
  • Assigning Multiple Line Function to Action
  • Recursive Statement Lambda

Download Project

Crystal Reports VB-SQL Writer/Helper

0 comments

Posted on 30th April 2010 by Admin in Database Programming |Visual Basic 6

, , ,

The application writes code for anyone developing using crystal reports and SQL Server. It generates the necessary ‘TTX’ and the ‘RPT’ files from an SQL statement, and then writes all the necessary Code into a window. You can then copy-paste it into a form, and immediately use it in your application. You must have Crystal Reports to use this great tiny app.

Download Project

Download File