I am having trouble connecting to an Oracle database using Oracle.ManagedDataAccess and Windows Native Authentication.
I am developing on a workstation from which I am able to connect to the same database using NTS via the PL/SQL Developer, and via C# code using non-managed Oracle.DataAccess. This leads me to I believe that there is nothing fundamentally wrong with either the database, workstation or the account. Now I am trying to evaluate if it's possible to switch to Oracle.ManagedDataAccess in our solution.
I've tried the following so far:
Created a new blank project OracleManagedAccessTest
Under references added the NuGet package Oracle.ManagedAccessTest v19.3.1 (the latest at the time of writing)
Update the <dataSource> tag in the App.config to match the entry in the tnsnames.ora (to make the C# project self-contained and not relying on external component)
Added <setting name="sqlnet.authentication_services" value="NTS"/> tag under <oracle.manageddataaccess.client><version number="*"><settings> in the App.config
I am using the following minimal code to test the connection:
try
{
using (OracleConnection conn = new OracleConnection("Data Source=EDBDEVD;User Id=/;"))
{
conn.Open();
}
}
catch (OracleException ex)
{
Console.WriteLine("Exception Message: " + ex.Message);
}
The attempt to connect fails with the following error: "ORA-01017: invalid username/password; logon denied"
This looks almost like too simple of a problem, yet I can't seem to resolve it, or find a workable solution online. Any help would be greatly appreciated.
Try this one:
string oradb = "Data Source=ORCL;User Id=hr;Password=hr;";
OracleConnection conn = new OracleConnection(oradb); // C#
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "select department_name from departments where department_id = 10"; cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
dr.Read();
label1.Text = dr.GetString(0);
see also: https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/GettingStartedNETVersion/GettingStartedNETVersion.htm
Related
I'm develop a new project for Medical Laboratory by using visual studio C# WinForms for user interaction and MYSQL for database. After my successful build its running successfully in my windows machine. But the problem is when I install my project on another windows machine, the front end of UI running well but the database throw an error to me. The error is Authentication to host 'localhost' for user 'root' using method 'caching_sha2_password' failed with message: Unknown database 'login'. I think the error was I need to add MYSQL reference in my project. but I'm absolutely don't know how to do it. I'm really sorry to all coz I'm noob in C# and my English.
and literally thanks to all.
public partial class registration : Form
{
string connectionstring = "server = localhost; user id = root; database = login; password =
qwerty;";
MySqlConnection connection = new MySqlConnection(connectionstring);
MySqlCommand cmd;
connection.Open();
try
{
cmd = connection.CreateCommand();
cmd.CommandText = "ALTER TABLE register ADD UNIQUE INDEX(rgstrid);";
cmd.CommandText = "INSERT IGNORE INTO register(username, password,confirm) VALUES(#username,#password,#confirm)";
cmd.Parameters.Add("#username", MySqlDbType.VarChar).Value = rgstrusrnmtxtbx.Text;
cmd.Parameters.Add("#password", MySqlDbType.VarChar).Value = rgstrpswdtxtbx.Text;
cmd.Parameters.Add("#confirm", MySqlDbType.VarChar).Value = rgstrcnfrmtxtbx.Text;
DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(table);
if (cmd.ExecuteNonQuery() == 1)
{
MessageBox.Show("Your Account resgistred Successfully", "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Account saved Successfully","Success",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception)
{
throw;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
You probably need to include a reference to a library to talk to MySql, like the MySql.Data package. Adding it as a nuget reference should ensure the required files are installed with the rest of the application files.
Your installer also needs to install some software. The database engine itself if you are running a local database, but also a .net connector for the ado.net driver.
At least this is how we do it. You might be able to avoid some components depending how you are accessing the database.
When installing the database you should specify the root password. For example by giving the rootpasswd=qwerty parameter to the MySQLInstallerConsole.exe program.
Your install script also needs to setup the database, i.e. either run sql files to create any tables and fill it with data, or copy the database files themselves, or run some program to create the tables.
I'm having trouble with my code to access a MySQL Database. Everytime I try to open my connection, a System.TypeInitializationException is thrown by MySql.Data.MySqlClient.Replication.ReplicationManager. Here is my code:
DataTable results = new DataTable("Results");
using (MySqlConnection connection = new MySqlConnection("SERVER=127.0.0.1;DATABASE=foo;UID=bar;PASSWORD=foobar;"))
{
using (MySqlCommand command = new MySqlCommand(queryString, connection))
{
command.Connection.Open(); //throws System.TypeInitializationException
command.ExecuteNonQuery();
using (MySqlDataReader reader = command.ExecuteReader())
results.Load(reader);
}
}
Edit: I guess MySQL Driver was corrupt. After an upgrade from Windows 7 to Windows 10 everything worked fine.
I had the same problem but when i installed the MySql.Data.dll using Nuget then the problem is solved
Install MySql.Dll file from Nuget,
not from MySQL Website.
Your connection string format is wrong. Try this :
static string cs = #"server=localhost;user id=bar;password=foobar;database=foo;";
DataTable results = new DataTable("Results");
using (MySqlConnection connection = new MySqlConnection(cs))
{
using (MySqlCommand command = new MySqlCommand(queryString, connection))
{
command.Connection.Open(); //throws System.TypeInitializationException
command.ExecuteNonQuery();
using (MySqlDataReader reader = command.ExecuteReader())
results.Load(reader);
}
}
In my case, it raised error only in production. It was due to a wrong assumption in the MySql.Data package. It for some reason seems to demand to be in the same folder as the orchestrating dll (it was referencing another dll which was referencing MySql). I resolved my issue by removing MySql.Data and instead installing MySqlConnector package as suggested in here.
I have a C#, .Net 4.5.2 project, built in VS2012, which uses Entity Framework to connect to a SQL database. I now need to access a separate Oracle database, and have been trying to use SQL Anywhere 16 to make the connection. I know that the SQL Anywhere connection works because I have a test project which successfully uses it. The problem is that the connection.Open() method errors with this message:
Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool.
I suspect that Entity Framework doesn't like the additional connection, but enabling MSDTC is not an option as I only have FTP access to the server where the code will live. Is anyone able to suggest an alternative way to get this working, bearing in mind I know very little about Entity Framework?
Additional
There aren't any transactions set up as far as I can see (I only wrote the stuff for connecting to the Oracle database), nor is TransactionScope being used. Here's an edited version of my code:
var connectionString = new SAConnectionStringBuilder();
connectionString.Host = "***.***.***.***";
connectionString.DatabaseName = "*****";
connectionString.UserID = "*****";
connectionString.Password = "*****";
connectionString.ServerName = "*****";
using (SAConnection conn = new SAConnection(connectionString))
{
using (SACommand cmd = conn.CreateCommand())
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
Solution
I did some more searching, and after a bit of trial and error, managed to solve my problem. It seems the SQL Anywhere connection was automatically being put into a transaction. When this was then combined with the transactions being used by the entity framework connection, it was raised to a distributed transaction.
I have found that by putting the connection inside a transaction scope, and using the TransactionScopeOption.Suppress flag, it is no longer being put into a transaction, and therefore does not require MSDTC to be enabled. My code now looks like this:
var connectionString = new SAConnectionStringBuilder();
connectionString.Host = "***.***.***.***";
connectionString.DatabaseName = "*****";
connectionString.UserID = "*****";
connectionString.Password = "*****";
connectionString.ServerName = "*****";
using (TransactionScope scope1 = new TransactionScope(TransactionScope.Suppress))
{
using (SAConnection conn = new SAConnection(connectionString))
{
using (SACommand cmd = conn.CreateCommand())
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
scope1.Complete();
}
Thanks to everyone who responded
I have just stared to learn C# 2 Weeks ago so I dont know much but right now I just want to make my first program I don't really care about the security flaw within ATM as I will fix these with time when I know a better solution.
So I got this error:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Additional information: A severe internal connection error has occurred.
And I have been sitting with this error for 2 days now modifying my code and searching the internet for solutions with no luck. So I turn here as I see there are some experts here.
Code with error:
using (SqlConnection con = new SqlConnection("server=SERVERIP,3306;Integrated Security=True;database=data;uid=USER;password=PASS"))
{
con.Open(); //ERROR HERE
using (SqlCommand cmd = new SqlCommand("insert into info(Datum,Timmar,Rast) Values(#Datum,#Timmar,#Rast)", con))
{
cmd.Parameters.AddWithValue("#Datum", textBox1.Text);
cmd.Parameters.AddWithValue("#Timmar", textBox2.Text);
cmd.Parameters.AddWithValue("#Rast", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Sparat!");
}
}
So basically what I am trying to do with this program is so that the data that the user types into the textboxes gets saved in the database.
When I type word with full capital letters I mean that it's something else there eg. Ip and password etc.
You are using the wrong connection class. You are trying to talk to a MySQL database as if it were a SQL Server database.
You need to use MySqlConnection, not SqlConnection. MySqlConnection is in MySQL Connector/NET, which can also be installed via NuGet.
Your code should look like the following instead.
using (MySqlConnection con = new MySqlConnection("server=SERVERIP;port=3306;database=data;uid=USER;password=PASS"))
{
con.Open(); // Hopefully no error here any more
using (MySqlCommand cmd = new MySqlCommand("insert into info(Datum,Timmar,Rast) Values(#Datum,#Timmar,#Rast)", con))
{
cmd.Parameters.AddWithValue("#Datum", textBox1.Text);
cmd.Parameters.AddWithValue("#Timmar", textBox2.Text);
cmd.Parameters.AddWithValue("#Rast", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Sparat!");
}
}
You have Integrated Security=true AND uid=USER;password=PASS
If you are providing username and password you shouldn't have Integrated Security=true
I am new to SmartDeviceProject. So When i try to connect MySQL to my project, MissingManifestResourceException through in Connection.open in below code?.
string connectionString = "server=192.168.1.100;database=mcubic;User Name=mcubic;Password=mcs#2011$;";
string query = "select b.Outlet_Master_Name from mcs_user_outlet a,outlet_master b where a.Mcs_User_Outlet_User_Id=3 and a.Mcs_User_Outlet_Outlet_Id = b.Outlet_Master_Id";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
comboBox1.Items.Add(Reader[0].ToString());
}
connection.Close();
How do i solve this,. I am imported MySql.Data.CF.dll for this project.
Via Krish Kapadia from MSDN forums:
Solution :
MySql's version 5.2.7.0 is the stable version. I use dll of this version. Other versions have many problems. so first I download dll of that version from mysql site.
here is the link :
http://dev.mysql.com/downloads/connector/net/5.2.html
I have inserted one entry in 'mysql.user' table in which hostname will be '%' (means any user can connect to mysql). If you don't want to insert entry with hostname then you have to insert for all ip addresses who should be allowed to connect to mysql.
After inserting entry in 'mysql.user', I restarted MySQL service.
And then trying to connect to mysql and Connected....
source:
http://social.msdn.microsoft.com/Forums/eu/netfxcompact/thread/66f6386a-9963-4c2f-8d39-1c507a26a6c7