Creating Connections to ODBC Data Sources using codes in vb.net - vb.net

I am trying to connect using vb.net (Visual studio 2013) to an MS Access Database 2007(.accdb) using codes. But something is wrong in my code and i can't figure it out.
the Database name is "localDatabase.accdb"
I didn't put any password on my database
I'm using a 64bit
Thanks in advance!
Here is my code:
Module Module1
Dim conn As New System.Data.Odbc.OdbcConnection
Public Sub ConnectToOdbc()
conn.ConnectionString = Provider=Microsoft.ACE.OLEDB.12.0;Data Source="C:\Users\MyPc\Documents\Visual Studio 2013\Projects\database\localDatabase.accdb"
Try
conn.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to data source")
Finally
conn.Close()
End Try
End Sub
End Module

this is just a hypothesis..
I am using ACE 12.0 now on my app, and it works fine. the different thing is the code try to use this..
Imports System.Data.OleDb
'instead of obdc
con = New OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " & Path.Combine(Application.StartupPath, "YourDatabase.accdb")
'put your .accdb in your app folder
maybe the error is it can't find your .accdb file.

Change this line:
conn.ConnectionString = Provider=Microsoft.ACE.OLEDB.12.0;Data Source="C:\Users\MyPc\Documents\Visual Studio 2013\Projects\database\localDatabase.accdb"
to this:
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MyPc\Documents\Visual Studio 2013\Projects\database\localDatabase.accdb"
ConnectionString as the name tell is a string, so you need to wrap it with double-quotes to be recognized as string by VB compiler.
UPDATE :
Also you need to change the connection object from Odbc to OleDb:
Dim conn As New System.Data.OleDb.OleDbConnection

Related

How to make connection VFP database with VB.NET

I'm creating a system that use foxpro as a database. I keep getting this error error [42S02][microsoft][ODBC visual foxpro driver] not a table when I want to connect VFP database with Visual Studio. When I add data connection in the visual studio, it shows connection success, but when I try to open the table, it shows the error.
This is a VB.Net system that use database foxpro 9. I have use mysql as the database and it work, but when I try to use foxpro database I get an error.
Imports System.Data.Odbc
Imports System.Data.OleDb
Public Class login
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
Dim oConn = CreateObject("adodb.connection")
oConn.ConnectionString = "Provider=vfpoledb;DSN=visual_foxpro"
oConn.Open()
Dim conn = New OleDbConnection()
Dim cmdString As String = "SELECT * FROM `login` WHERE `staffID`= #staffid AND `staffName`= #staffname"
Dim cmd As New OleDbCommand(cmdString, oConn)
cmd.Parameters.Add(New OleDbParameter("staffID", CType(txtStaffID.Text, String)))
cmd.Parameters.Add(New OleDbParameter("staffName", CType(txtStaffID.Text, String)))
Dim adapter As New OleDbDataAdapter(cmd)
Dim table As New DataTable()
adapter.Fill(table)
If table.Rows.Count = 0 Then
MessageBox.Show("Staff ID or Staff Name not available")
Else
MessageBox.Show("Welcome " & txtStaffName.Text)
Dim form As New formLeave
form.PassStaffid = txtStaffID.Text
form.PassStaffName = txtStaffName.Text
form.Show()
Me.Hide()
End If
End Sub
End Class
I expected the system can login using the database.
VFP database versions later than 6.x do not have an official ODBC driver from Microsoft. If you HAVE TO use ODBC, then you can find alternative drivers from sources like Sybase ADS. I use OLEDB instead successfully well.
While your code might work with MySQL, that is not the way you should write it. Also, it is MySQL specific, it wouldn't work in say MS SQL Server or postgreSQL either. You should read the documentation on the backend you are using. In VFP (or MS SQL Server, postgreSQL ...), you don't use back tics as table and field name identifiers. In VFP, if need be, to use name identifiers you could use single, double quotes or square brackets but you would need to enclose with parentheses (and use only for table name in an SQL query). Anyway, the easy way is to simply not to use identifiers at all.
Also, with an ODBC or OLEDB query, you need to use ? as parameter placeholders. Using #staffID wouldn't normally work in MySQL, ... either, but driver makers decided to support them for those backends.
From your messageBox messages, looks like you expect to get a single row for that query (I don't know why you use both staffId and staffName if staffId is primary key). Anyway here is your query in VB.Net:
Imports System.Data.OleDb
Public Class login
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
Dim strConn As String = "Provider=VFPOLEDB;Data source=c:\MyDataFolder\"
Dim strQuery As String = <sql>SELECT *
FROM login
WHERE staffID=? AND staffName=?
</sql>
Using cn As New OleDbConnection(strConn)
Using cmd As New OleDbCommand(strQuery, cn)
cmd.Parameters.Add("#staffid", OleDbType.VarChar).Value = txtStaffID.Text;
cmd.Parameters.Add("#staffname", OleDbType.VarChar).Value = txtStaffName.Text;
cn.Open()
Dim rdr As OleDbDataReader = cmd.ExecuteReader()
If rdr.Read()
MessageBox.Show("Welcome " & txtStaffName.Text)
Dim form As New formLeave
form.PassStaffid = txtStaffID.Text
form.PassStaffName = txtStaffName.Text
form.Show()
Me.Hide()
Else
MessageBox.Show("Staff ID or Staff Name not available")
End If
cn.Close()
End Using
End Using
End Sub
End Class

How to close database connection?

I'm having a problem in project of mine in VB.NET. The problem is whenever I want to save, delete or update data with an Access database I get an error with a message saying that "not allowed to change the connection string property. connection's current state is open".
I have used If con.State = ConnectionState.Open Then con.Close() End If
command using finally in every section where I have called the database.
But still I'm having the same problem. What am I doing wrong?
Use the "USING"-Keyword. Exiting a using block calls .Dispose() on the object which for a SqlConnection will close the connection and any open resources.
Using connection As New SqlConnection(connection)
Dim command As New SqlCommand("Select * From dbo.table1",connection)
command.ExecuteNonQuery()
End Using
EDIT:
Module Module1
Public Sub DbConnection()
Dim connectionString as String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourServerAddress;Initial Catalog=university.mdb;
Integrated Security=SSPI;"
Using connection as New Sqlconnection(connectionstring)
Dim command As New SqlCommand("Select * From dbo.table1",connection)
command.ExecuteNonQuery()
End Using
End Sub
End Module

Connect VB on Visual Studio to MS Access

What would be the code that will connect the Data from MS Access to the Visual Basic in Visual Studio? I am new to Visual Basic.
If you are just looking for the connection you can use this
Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\database\data.mdb")
I place the data.mdb at custom folder name 'database' in the debug folder of the application.
This is more detailed...
Dim con As New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DBName.accdb;Persist Security Info=False;"
Try
con.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try

Linking VB to MS Accsss Database

I am trying to link a VB application with a MS access database.
The only way I seem able to do this is through adding a data source. Is there a way or doing this so you use it as an external and code to the file location rather than actually uploading the database to vb?
Thanks
Imports System.Data.Oledb
Dim con As New OledbConnection("Provider=microsoft.Jet.oledb.4.0DataSource=D:\mydata.mdb;")
Dim cmd As New OledbCommand
Public Sub New()
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT * FROM table1"
End Sub

vb.net Oracle connection using TNS Name?

I have put my tns connection into the .ora file and am now able to conenct to it using SQL plus and can ping it :tnsping myConn.
I've also added the connection to the ODBC Manager and connecting successfully when tetsing conneciton through the ODBC tool.
now i'm having an issue making a connection to it using vb.net
i've tried the following:
oODBCConnection = New Odbc.OdbcConnection(connStr)
oODBCConnection.Open()
where my connStr is:
Data Source=tns.dev;User Id=MyUser;Password=MyPass;
per: http://www.connectionstrings.com/oracle and http://www.shabdar.org/c-sharp/101-connect-to-oracle-using-asp-net-and-csharp.html
what am i doing wrong? it's telling me i need to specify a driver, how do i do it?
Thank you!
the error i'm getting is:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Did you checked the tnsnames.ora file? Is there any entry for tns.dev?
http://www.mike-devlin.com/oracle/tnsnames_ora.htm
That is not an oracle error - it sounds like you did not create a system dsn. This code looks like it expects a DSN by name of tns.dev.
That said, I would not use odbc if I could help it. You might want to look at Oracle Data Provider for .net
http://www.oracle.com/technetwork/topics/dotnet/index-085163.html
ODBCConnection was incorrect.
solution:
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim myConnection As New OracleConnection(connStr)
myConnection.Open()
I´ve been using the following code in Vb.net
Dim conn As New Odbc.OdbcConnection
Dim cmd As New Odbc.OdbcCommand
Dim drResult As Odbc.OdbcDataReader
Dim connString As String
Dim QuerySQL As String
connString = "Driver={Microsoft ODBC for Oracle};CONNECTSTRING=(DESCRIPTION=(ADDRESS= (PROTOCOL=TCP)(HOST=ORACLEDB01)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORACLE_INSTANCE_NAME)));Uid=john;Pwd=mypassword;"
QuerySQL = "select first_name, last_name from employees where id = 28"
conn.ConnectionString = connString
conn.Open()
cmd.Connection = conn
cmd.CommandText = QuerySQL
drResult = cmd.ExecuteReader()
While drResult.Read
TextBox1.Text = TextBox1.Text & drResult("last_name") & ", " & drResult("first_name") & Environment.Newline
End While
drResult.Close()