how to connect oracle database with c# - c#

I am trying to get connection with Oracle database but there is an error that shows that data source not found! Please can anyone help?
string ConnectionString= "Data Source=voldemort-pc;User ID=student;Password=***********;Unicode=True";
OdbcConnection conn = new OdbcConnection(ConnectionString);
try
{
conn.Open();
MessageBox.Show("success!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Related

Connecting MySQL database with Visual Studio

I made a MySQL server database file from server explorer using the following code to connect the MySQL database:
private void DataAdd_Load(object sender, EventArgs e)
{
try
{
var conn = new MySqlConnection();
conn.ConnectionString =
"Data Source=(LocalDB)\\MSSQLLocalDB;" +
"User Instance=true;" +
"Integrated Security=false;" +
"AttachDbFilename=C:\\Path\\filename.MDF;";
conn.Open();
MessageBox.Show("Connected to database");
}
catch (Exception e1)
{
MessageBox.Show("Connection failed");
}
}
But the connection always fails.
The error that I found while debugging:
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll ("The user instance login flag is not allowed when connecting to a user instance of SQL Server. The connection will be closed.")
To connect to MySQL, you need MySqlConnection and a proper MySQL connection string:
private void DataAdd_Load(object sender, EventArgs e)
{
try
{
var conn = new MySqlConnection(#"Server=192.168.1.10;Database=myDB;Uid=myUsername;Pwd=myPassword;");
conn.Open();
MessageBox.Show("Connected to database");
}
catch (Exception e1)
{
MessageBox.Show("Connection failed");
}
}
You will need to use MySQLConnection as answered here:
ASP.NET use SqlConnection connect MySQL
The MySQL connection library might not be included in your solution, so you will need to download it. And change var conn = new SqlConnection(); to:
var conn = new MySqlConnection();

“error isam instalable cant be found”

I'm trying to connect my windows form app with my embedded database (.dbf) and I keep getting this message no matter what I do to the connection string:
error isam instalable cant be found
Here is the code I'm using to test the whole thing:
private void bGuardar_Click(object sender, EventArgs e)
{
try
{
string cadena = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =D:\\; Extended Properties = dBASE IV; UserID =; Password =;";
OleDbConnection con = new OleDbConnection();
con.ConnectionString = cadena;
con.Open();
MessageBox.Show("conected");
con.Close();
}
catch (OleDbException exp)
{
MessageBox.Show("Error: " + exp.Message);
}
}
I think the problem is in the connection string.

How do I connect to a SQL Server database server that IS NOT on localhost?

The database I'm trying to connect to is on an AWS server and I'm trying to write a C# script that will pull data from the database. The only tutorials I can find are using localhost. Here's some code I tried:
static void Main()
{
SqlConnection myConnection = new SqlConnection(
"user id=MyUsername;" +
"password=MyPassword;" +
"server=MyServerName.ctf1qojvktpk.us-west-2.rds.amazonaws.com:3306;" +
"Trusted_Connection=yes;" +
"database=MyDBName; " +
"connection timeout=30");
try
{
myConnection.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
It looks like it's a mySql database. You need to install the MySQL Connector for .NET.
Example connectionstring
<add name="MyConnection" connectionString="server=myamazonserver.eu-central-1.rds.amazonaws.com;user id=rootusername;password=mypassword;database=mydatabasename; Convert Zero DateTime=True; Allow User Variables=True" providerName="MySql.Data.MySqlClient" />
Or in plain code
static void Main()
{
SqlConnection myConnection =
new SqlConnection(
"server=myamazonserver.eu-central-1.rds.amazonaws.com;user id=rootusername;password=mypassword;database=mydatabasename; Convert Zero DateTime=True; Allow User Variables=True" providerName="MySql.Data.MySqlClient"
);
try
{
myConnection.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

Browsing datadirectory for SQL Server database does not work

I have hardcoded the data directory for the database. Since I want to avoid it, I have decided to use the FolderBrowserDialog and store the dialog string into the application settings.
This is the hardcoded code snippet, which can open the SQL connection:
AppDomain.CurrentDomain.SetData("DataDirectory", #"C:\Users\Osman\Documents\Visual Studio 2013\Projects\CompanyWPF\CompanyWPF\");
try
{
using (SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Produkt.mdf;Integrated Security=True"))
{
con.Open();
}
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
But when I'm using the stored string from the application settings:
AppDomain.CurrentDomain.SetData("DataDirectory",#"" + Properties.Settings.Default.ConnectionString);
try
{
using (SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=#|DataDirectory|\Produkt.mdf;Integrated Security=True") )
{
con.Open();
}
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
then the following error occurs:
Invalid value for key 'attachdbfilename'.
I have set a breakpoint, and the stored string from the application setting has the value:
C:\\Users\\Osman\\Documents\\Visual Studio 2013\\Projects\\CompanyWPF\\CompanyWPF\\
I do not sure what you are going to do but i think here is the problem
using (SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=#|DataDirectory|\Produkt.mdf;Integrated Security=True") )
Use This Instead
using (SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Produkt.mdf;Integrated Security=True") )
Note : Remove # From this "#|DataDirectory| "

oracle 9i: ora-12705 invalid or unknown nls parameter value specified

I have an Oracle 9i DB installed at remote IBM AIX server.
I want to connect to it using C# app (.Net)
Presently, I am able to connect to it using SQL Developer and SQLPlus from my machine.
But when I try to connect from Visual Studio App, using System.Data.OracleClient.
private static string GetConnectionString()
{
return "Data Source=<server address>;User ID=<username>;Password=<password>;";
}
// This will open the connection and query the database
private static void ConnectAndQuery()
{
string connectionString = GetConnectionString();
using (OracleConnection connection = new OracleConnection())
{
try
{
connection.ConnectionString = connectionString;
connection.Open();
Console.WriteLine("State: {0}", connection.State);
Console.WriteLine("ConnectionString: {0}",
connection.ConnectionString);
OracleCommand command = connection.CreateCommand();
string sql = "SELECT * FROM demo";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string myField = (string)reader["f1"];
Console.WriteLine(myField);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
connection.Close();
}
finally
{
connection.Close();
}
}
}
I get the following error:
ORA-12705 invalid or unknown NLS parameter value specified
I have checked registry values for NLS it is already set to AMERICAN_AMERICA.WE8MSWIN1252
Not sure how to handle this. Please suggest
Some possible causes for this problem are discussed here on dba-oracle.com.

Categories