Dim conn As MysqlConnection
conn = New Mysqlconnection()
conn.ConnectionString = "server=justhost.com; user id=*****; password=****; database=login"
Try
conn.Open()
Catch myerror As MySqlException
MsgBox("Error connecting to database!")
End Try
this code not working ? how to connect online phpmyadmin database use vb.net?
Have you tried installing this one?
http://dev.mysql.com/downloads/connector/net/
and after installing that, try to reconfigure your code try this.
Imports MySql.Data.MySqlClient
Dim Connection As MySqlConnection = New MySqlConnection()
Dim ConnectionString As String = "Server=SERVERNAME or the Database online link;Database=DATABASE;Uid=root;Pwd=password;"
Connection.ConnectionString = ConnectionString
Connection.Open()
//Execute here what ever you want just follow your heart <3
myConnection.Close()
Related
So i have here my codes for fetching informations from the database using
the listview, and now i want to use the datagridview but i dont know how
to do it using the ADODB Connectionenter image description here.
If you are able to use an Odbc connection instead try this to retreive your data
Dim conn As New OleDb.OleDbConnection("path to your database")
Dim cmd As New OleDb.OleDbCommand
Dim da As New OleDb.OleDbDataAdapter
Dim sql as String
Sql = "your SQL Query"
conn.Open()
cmd.Connection = conn
cmd.CommandText = Sql
da.SelectCommand = cmd
You can try using Odbc Connection in VB.Net.
First add new Module named Connection.vb
Imports System.Data.Odbc
Module Connection
Public Con As New OdbcConnection
Public Adpt As New OdbcDataAdapter
Public Ds As New DataSet
Public Cmd As OdbcCommand
Public Read As OdbcDataReader
Public Sql As String
Public StrCon As String = "Dsn=Your DSN Name in Odbc Connector"
Public Sub Connect()
Con = New OdbcConnection(StrCon)
If Con.State <> ConnectionState.Closed Then Con.Close()
Con.Open()
End Sub
End Module
And next don't forget to add Imports System.Data.Odbc in every form you have to connect to your Odbc Connection.
Simple Code to add item on ComboBox from Database.
Call Connect()
Cmd = New OdbcCommand("SELECT * FROM `category` ", Con)
cmbKategori.Items.Clear()
cmbKategori.AutoCompleteCustomSource.Clear()
Read = Cmd.ExecuteReader()
If Read.HasRows = True Then
While Read.Read()
cmbKategori.AutoCompleteCustomSource.Add(Read("name_category"))
cmbKategori.Items.Add(Read("name_category"))
End While
End If
I want to Insert 2 different data in 2 different table of ms-access.
And it shows this error.
I have a code like this:
try
dim sql1,sql2 as string
sql1 = "INSERT INTO table1(something)VALUES(something)"
cmd = new oledbcommand(sql1, connection)
cmd.executenoquery()
sql2 = "INSERT INTO table2(something)VALUES(something)"
cmd2 = new oledbcommand(sql2, connection)
cmd2.executenoquery()
catch ex as exception
msgbox(ex.tostring())
(where these cmd1,cmd2 are defined in controlModule.)
so,what should I do ?
Any help is appreciated. Thank You
I think closing the connection fixes the issue, best by using the Using-statement:
try
Using con As OleDbConnection = GetConnection() ' or New OlebConnection(...)
Using cmd = con.CreateCommand()
cmd.CommandText = "INSERT INTO table1(something)VALUES(#something)"
cmd.Parameters.AddWithValue("#something", something)
con.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Using con As OleDbConnection = GetConnection()
Using cmd = con.CreateCommand()
cmd.CommandText = "INSERT INTO table2(something)VALUES(#something)"
cmd.Parameters.AddWithValue("#something", something)
con.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As Exception
msgbox(ex.tostring())
End Try
This is a concurrency issue. Because some other part of your code or MS Access itself accesses the database at the same time.
The fact is that you're not closing the connection after it's use. So the 2nd call should fail with that exception. Instead, you should wrap your disposables - e.g. the OleDbConnection, commands, etc. - in a using statement. That way, the connection will be closed, even if an exception occur:
Using con As New OleDbConnection, cmd1 As OleDbCommand = con.CreateCommand, cmd2 As OleDbCommand = con.CreateCommand()
cmd1.CommandText = "INSERT INTO table1(something)VALUES(something)"
cmd1.ExecuteNonQuery()
cmd2.CommandText = "INSERT INTO table2(something)VALUES(something)"
cmd2.ExecuteNonQuery()
End Using
When I try to connect to AcessDB, I get the error
"Keyword is not supported: provider".
When I try to change provider, I get another error. When I delete provider tag, I also get an error.
Dim Exists As Boolean = False
Dim ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Name\Documents\Visual Studio 2010\Projects\datagrid\datagrid\pokus.accdb;Persist Security Info=False;"
Dim connection As New SqlConnection(ConnectionString)
Try
connection.Open()
Dim command As SqlCommand = connection.CreateCommand
command.CommandText = "SELECT * FROM studenti"
Dim reader As SqlDataReader = command.ExecuteReader
If reader.HasRows Then
Exists = True
Else
Exists = False
End If
reader.Close()
command.Dispose()
Catch ex As Exception
Console.Write(ex.Message)
Finally
connection.Close()
End Try
SqlConnection, SqlCommand, etc. are SQL Server-specific classes. You can't use them to connect to MS Access.
The documentation for SqlConnection makes this very clear:
Represents an open connection to a SQL Server database.
Consider using the OldDbConnection and related classes instead.
I always write wrapper classes for database connections, and I always recommend the same.
Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Name\Documents\Visual Studio 2010\Projects\datagrid\datagrid\pokus.accdb;Persist Security Info=False;"
Dim Conn As New OleDbConnection
Conn.ConnectionString = sConnectionString
Conn.Open()
Dim sQuery As String = "SELECT * FROM studenti"
Dim da As New OleDbDataAdapter(sQuery, Conn)
Dim dt As New DataTable
da.Fill(dt)
Conn.Close()
I am facing a problem that when i make the setup file of my application developed in vb.net the database file will not be attached to it, as a solution i wish to create the database # first run of the application through the following code
Dim myCommand As String
Dim myConnectionString As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=soft_billing;Integrated Security=True;Pooling=False")
myCommand = "CREATE database soft_billing"
Dim cmd As SqlCommand = New SqlCommand(myCommand, myConnectionString)
Try
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Catch
MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
End Try
it will not generated the required data base,
help me please, thanks in advane
Dim myCommand As String
Dim myConnectionString As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Pooling=False")
myCommand = "CREATE database soft_billing"
Dim cmd As SqlCommand = New SqlCommand(myCommand, myConnectionString)
Try
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Catch
MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
End Try
trouble connecting to postgresql database using odbc connector(x64) on vb.net console application(x64), the error,
http://www.sumarlidason.com/tmp/120312/odbc_capture1.png
Dim ConnectionString = "Driver={PostgreSQL UNICODE};Server=myPGSrv;Port=5432;Database=dbDefault;Uid=postgres;Pwd=pw;"
'Dim ConnectionString = "ODBC;dsn=PostgreSQL35W"
conn = New OdbcConnection(ConnectionString)
'Open connection to an instance of the PostgreSQL database.
Try
conn.Open()
Catch Ex As Exception
MsgBox(Ex.Message)
End Try
Dim commonOdbcCommand = New OdbcCommand
commonOdbcCommand.Connection = conn
conn.Close()
Also, I configured the database in the control panel, see here..
http://sumarlidason.com/tmp/120312/odbc_capture.png
correct connection string:
Dim conn As New OdbcConnection("DSN=PostgreSQL35W")