Issue in connecting to MS Access(accdb) database - c#

I need to connect to MS Access database(*.accdb) using OdbcConnection class in my C# application.
Below is the code that I tried:
string connetionString = "Driver={Microsoft Access Driver (*.accdb)};DBQ=C:\\Temp\\Database4.accdb";
OdbcConnection myConnection = new OdbcConnection(connetionString);
try
{
myConnection.Open();
myConnection.Close();
}
catch (Exception ex)
{
}
But it gets the below exception on the myConnection.Open() call.
"ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not
found and no default driver specified"
But the above code is working for .mdb file if I use the connection string like below:
string connetionString = "Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\Temp\\Database4.mdb";
But my requirement is to connect to a .accdb file.
Any idea of what is wrong with my code or connection string format?

I got it working using the following the code:
const string connetionString = #"Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\Temp\Database4.accdb";
OdbcConnection myConnection = new OdbcConnection(connetionString);
myConnection.Open();
myConnection.Close();
Console.WriteLine("Done.");
Console.ReadLine();
But it will work only on x86 configuration and not with x64 configuration. For x64 configuration, we have to go for OleDbConnection class.
Thank you all..

Related

Issue connecting to gearhost database

I'm trying to connect with C# code to a SQL Server database hosted on gear host using the System.Data.SqlClient library, but it doesn't work, I get an error "Server does not exist or connection refused."
Maybe I need do install some driver? Or is there anything wrong with my code?
string connetionString = "Data Source=mssql.gear.host:1433;Initial Catalog=databasename;User ID=userid;Password=mypassword";
SqlConnection cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
Console.WriteLine("Connection Open!");
cnn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
when removing the port it works ,obviously the database in gear host doesn't use a port

How to connect to a xampp server to a c# gui app?

So i was trying to connect a c# gui app to a xampp sql server here's the code:
try
{
string connectionString = "Server = localhost:8080; database = blog; Uid=root;Pwd";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("insert into logins(username,password) values('name','password')", conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this is the code username is root and no password
but when the program starts it raises an exception saying:
Format of the initialization string does not conform to specification starting at index 52.
Whats causing this problem and how can i solve it?
Thanks in advance
SQLConnection class is used to connect to sqlserver and to Connect Mysql MySqlConnection will be used
you need to add MySql Library in your C# project.
using MySql.Data.MySqlClient;
Config
string myConnectionString = "server=localhost;database=testDB;uid=root;pwd=pwd;";
Use
string connetionString =""; //get from config;
MySqlConnection cnn ;
cnn = new MySqlConnection(connetionString);

Execute ObdcCommand for NetezzaSQL with C#

I am trying to execute a command with OdbcCommand in c# but it seems i can't even open the connection. This is the data I got to create the Connection String
Server: APPRDNETEZZA (192.168.0.64)
Web Server: beamprdwb3
ODBC DSN Name (64bit): NZ_FUTUREBRANDS
User: MSTR_ADMIN
Port: 5480
This is my code
string connetionString = null;
OdbcConnection cnn;
OdbcCommand cmd;
string sql = null;
connetionString = "Driver={NetezzaSQL}; servername=APPRDNETEZZA ;
database=NZ_FUTUREBRANDS; port=5480; username=MSTR_ADMIN;
password=mstr17Uz1%4;";
sql = "CREATE EXTERNAL TABLE X_STORE_GROUP_GUID_12345(" +
"STORE_ID VARCHAR(10)," +
"STORE_NAME VARCHAR(50)," +
"USERID Varchar(255)," +
"import_guid Varchar(255), )" +
"USING(" +"DATAOBJECT('/apnas01/vol2.nfs.Data/ap_prod/data/store_upload/scripts/12345_guid_12345')" + "logDir '/apps/ap_prod/log'" + " delimiter ','" + " "; ";
cnn = new OdbcConnection(connetionString);
try
{
cnn.Open();
Console.WriteLine("Connection Opened ");
cmd = new OdbcCommand(sql, cnn);
cmd.ExecuteNonQuery();
cmd.Dispose();
cnn.Close();
result = "Executed sucessfully";
}
catch (Exception ex)
{
result = "Error" + ex.InnerException.ToString();
}
The error happens when it tries to open the connection. While debugging it gave me this inner exception
[ODBC Driver Manager] Data source name not found and no default driver
specified
I have the Odbc driver installed in my machine. I dont know it this is happening cuz the connection string is in bad format. I have spent many hours trying to figure it out why doesnt work. Appreciate any kind of help. Thanks
Here is an example Netezza ODBC connection string:
https://www.connectionstrings.com/netezzasql-odbc-driver/
Driver={NetezzaSQL};servername=myServerAddress;port=myPortNumber;
database=myDataBase;username=myUsername;password=myPassword;
Your connection string basically looks the same:
connetionString = "Driver={NetezzaSQL}; servername=APPRDNETEZZA ;
database=NZ_FUTUREBRANDS; port=5480; username=MSTR_ADMIN;
password=xyz;";
SUGGESTIONS:
Make the entire connection string (everything inside the quotes) ONE LINE. Don't break the string into separate lines.
Or use "+" (like you did with your SQL string).
Copy the driver (nsqlodbc.dll?) into the same directory as your .exe assembly (path issue?)
Try both the 32-bit and 64-bit drivers (CPU platform issue?)
Definitely post back what you find!

Cannot connect to oracle server from c#

I have oracle 10g Express Edition installed and I'm trying to connect to it from C#.
I've created a ODBC Data Source called MyOracle from ODBC Data Source Administrator.
I'm using Windows XP and Visual Studio 2008 Express Edition.
Here's my code:
string connString = "dsn=MyOracle;User=user;Password=pass";
OdbcConnection connection = null;
OdbcCommand command = null;
OdbcDataReader reader = null;
try
{
connection = new OdbcConnection(connString);
connection.Open();
connection.Close();
connection.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(string.Format("StackTrace:\n\n{0}\n",ex.StackTrace));
Console.WriteLine(string.Format("Message:\n\n{0}\n", ex.Message));
}
The console output tells me that the problem is at connection.Open();
Here's the output:
Any ideas?
Try to change your connection string to the following:
string connString = "DSN=MyOracle;UID=user;PWD=pass;Integrated Security=no;";

Can not Open ODBC connection for MSAceess in Windows7 64bit

Can not connect MSAcess through ODBC.
OdbcConnection.Open thrown an exception like
"ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
code portion is follows:
string connetionString = null;
OdbcConnection cnn;
connetionString = (#"Driver={Microsoft Access Driver (*.mdb,*.accdb)};Dbq=E:\aa.mdb;Exclusive=1;Uid=Admin;Pwd=;");
cnn = new OdbcConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
Your connection string is wrong, this works for me:
connetionString = (#"Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=E:\aa.mdb;Uid=Admin;Pwd=;");
See http://connectionstrings.com

Categories

Resources