Cannot connect to oracle server from c# - 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;";

Related

WinFormApp won't connect to the database

No clue why this won't work. It has worked before.
I do have the connection string in the app.config as well. I get the MySqlException error saying unable to connect to any database.
I made sure the firewall wasn't stopping it and I opened the ports on my router. All the references are in place too. This should work.
string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
try
{
using (MySqlConnection Conn = new MySqlConnection(connString))
Conn.Open();
MessageBox.Show("DB Connected");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
try this code
string connetionString = null;
MySqlConnection cnn ;
connetionString = "server=localhost;database=testDB;uid=root;pwd=abc123;";
cnn = new MySqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show ("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
MySQL Databases cannot be used in WinFormApp due to security risks on a remote server. MS SQL DB's are supported. I didn't know that and I see a lot of questions about this so if anyone can't connect this may be why.

Issue in connecting to MS Access(accdb) database

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..

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

Unable to connect to any of the specified MySQL hosts

I know that it is an easy problem but I can't find the error. I want to save data in my database. I create a database named "Examen" with Microsoft SQL Server 2008 then in my app in visual studio I make the connection string like this :
string connectionstring = #"Data Source=.\sqlexpress;Initial Catalog=Examen;Integrated Security=True";
Then I use this code to insert data into a "Test" table:
MySqlConnection connection = new MySqlConnection(connectionstring);
MySqlCommand cmd;
connection.Open();
try
{
cmd = connection.CreateCommand();
cmd.CommandText = "Insert into Examen.Test (nom,prenom) values (" + txbnom.Text + "," + txbprenom.Text + ") ";
cmd.ExecuteNonQuery();
MessageBox.Show("ok");
}
catch (Exception)
{
throw;
}
finally
{
if(connection.State == ConnectionState.Open)
{
connection.Close();
}
}
When running this code i had an error when openning the connection
Unable to connect to any of the specified MySQL hosts.
You are mixing MySQL and MSSQL.
Are you sure you want to connect to a MySQL server? Use http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx if you would like to connect to MSSQL.
Also you should make yourself familiar with SQL injection

Unable to work with SQL Server CE 4.0

I try to work with a SQL Server CE 4.0 database with my simple console app but it is not working on one of my machine.
OS: Windows Server 2003 Enterprise x64 Edition with Service Pack 2
I have also installed Microsoft SQL Server Compact 4.0 and used its dll (System.Data.SqlServerCe.dll) but every time I got following error
Unable to load the native components of SQL Server C
ADO.NET provider of version 8080. Install the correc
act. Refer to KB article 974247 for more details.
Here is my code
try
{
Console.WriteLine("Started");
var sqlConnectionString = "Data Source=c:\\tde\\22\\22.tde;password='sanjayraj';mode=Read Write;max database size=4091;temp file max size=4091";
_connection = new SqlCeConnection(sqlConnectionString);
_connection.Open();
var sqlStatement = "SELECT * FROM InfoStores WHERE Location = 'Source'";
IDbCommand newCommand = new SqlCeCommand
{
CommandText = sqlStatement,
Connection = _connection,
CommandType = CommandType.Text
};
using (IDataReader reader = newCommand.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error Occured");
Console.WriteLine(ex.Message);
Console.ReadKey();
}
Console.WriteLine("Finished");
Console.ReadKey();
You a referenceing the wrong version of System.data.SqlServerCe.dll, use the one in the C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Desktop folder

Categories

Resources