I'm trying to open a connection to my university's database that runs on Oracle 11g 11.2.0.1.0 (Oracle.ManagedDaraAccess v18.3.0 is installed on visual studio) with c#.
When I attempt to connect, it throws:
Exception thrown: 'Oracle.ManagedDataAccess.Client.OracleException' in Oracle.ManagedDataAccess.dll"
during the con.open() function at me with no further explanation.
Is there any way to see where the error occurs?
OracleConnection con = new OracleConnection();
OracleConnectionStringBuilder ocsb = new OracleConnectionStringBuilder();
ocsb.Password = pass;
ocsb.UserID = user;
ocsb.DataSource = "<address>:<port>/orcl";
con.ConnectionString = ocsb.ConnectionString;
con.Open();
Add a try catch block around the open() call and resolve the ORA error that shows up in the OracleException
Related
Keeps throwing the exception:
An unhandled exception of type 'System.InvalidOperationException'
occurred in MySql.Data.dll
here is the code:
Public Sub showWorkerFullnames()
getQuery = "SELECT worker.worker_fullname FROM worker"
getCommand = New MySqlCommand(getQuery, MySQLConnection)
getReader = getCommand.ExecuteReader
cbWorkerFullnames.Items.Clear()
While getReader.Read
cbWorkerFullnames.Items.Add(getReader.Item("worker_fullname").ToString)
End While
This page shows a working example.
Creating a connection:
string connStr =
"server=localhost;user=root;database=world;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);
Use that connection object when creating the command:
MySqlCommand cmd = new MySqlCommand(sql, conn);
Open the connection before using it. The linked example opens the connection right after creating it. It's hair-splitting, but I would make opening the connection the very last thing before using it.
conn.Open();
And don't forget to close the connection when you're done.
I have added a SQL Server .mdf database file to my C# application, but when I try to connect with this code, the program causes a connection error.
CODE:
DataSet data;
string con = "Data Source=dbinterno.mdf;";
string queryString = "Select * FROM Dati";
try
{
using (SqlConnection connection = new SqlConnection(con))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(queryString, connection);
command.ExecuteNonQuery();
data = new DataSet();
adapter.Fill(data);
MessageBox.Show(data.ToString());
connection.Close();
}
}
catch
{
MessageBox.Show("\n Problemi di connessione al database");
}
The error is:
ERROR IMAGE
Here are a couple observations:
Your connection string will need to be modified. Try using
string con = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;";
using Windows Authentication or this:
string con = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;"; using standard security, Source: connectionstrings.com. This should be managed some other way than in code as well. Desktop applications can be de-compiled, and if the password changes, you would need a rebuild. In a ASP.NET application, Microsoft advises to use a web.config file or in the windows registry using a custom subkey.
You will want to use ExecuteReader() for a SELECT statement as ExecuteNonQuery() will not return a result set. See this answer that describes the differences in the types of SQL Server methods
you don't need connection.Close();, the using statement will handle that.
static void Main(string[] args)
{
OleDbConnection conn = new OleDbConnection("Provider=SAOLEDB.10;LINKS=tcpip(IP=192.168.1.100,Port=2638);ENG=dental;Persist Security Info = True; User ID = dba; PWD = sql");
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT tran_num FROM transactions WHERE tran_date > '2015-10-01'", conn);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{0}", reader.GetValue(0).ToString());
}
reader.Close();
conn.Close();
if (Debugger.IsAttached)
{
Console.ReadLine();
}
}
As the title suggests, I'm getting an error when attempting to connect to this server. The driver (Provider) works fine for a local database using the same server setup, trying to adjust the code to connect remotely to the same setup at a different location. This is an old SQL Anywhere 10 database and I've tried hard to find answers for this with no success so far.
Edit: I changed the string a little to reflect some suggestions, now just getting a new error: Additional information: Connection error: TCPIP requires a server name
Update: The syntax changes to LINKS seems to have progressed my attempts but now I'm having issues with the syntax it's looking for when searching for a server at that IP. Receiving an error: Additional information: Database server not found
This is the connection string in registry:
DBN=DENTSERV;DSN=DENTAL;UID=DBA;PWD=SQL
I've tried both ServerName= and ENG= using the DSN and DBN, also while having them in ' ' but still receiving that same error. Any thoughts?
I get that error when I try to debug my program. Searched for this error but could not really figure it out on my own. Also when I try to connect my database I get this error "error: 26 - Error Locating Server/Instance Specified".
public DataTable ReadData(string st_proc, SqlParameter[] param)
{
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = st_proc;
command.Connection = conn;
if (param != null)
{
command.Parameters.AddRange(param);
}
SqlDataAdapter db = new SqlDataAdapter(command);
DataTable dt = new DataTable();
db.Fill(dt);
return dt;
}
Looks like you have got the connection string from some else's machine. Here Data Source=(Localdb)\mssqllocaldb; means it is pointing to "[Sql Server Express LocalDB 2014][1]".
Make sure you have it installed on you machine. You can also try connecting to DataSource=(Localdb)\v11.0; which is "Sql server express 2012" and use it in your connection string. See if this works.
I'm using an Oracle database(10g) which contains a stored procedure called Foo.
Foo takes 2 datetime as IN parameters and 1 cursor as OUT parameter. I've been trying to make the connection and the query to the database with the OleDbCommand, but since Foo needs a cursor I have no choice but to use a OracleCommand(right?).
When I try to connection to the database I get the following error : "OCIenvCreate has failed return code -1". I've gived the correct permissions to ASPNET user for the oci.dll file so that it can read and execute it. Unfortunately, I still get the same error and I'm lost.
Here is what cause the error
OracleConnectionStringBuilder conBuilder = new OracleConnectionStringBuilder();
conBuilder.DataSource = dataSrc;
conBuilder.UserID = user;
conBuilder.Password = password;
OracleConnection con = new OracleConnection(conBuilder.ConnectionString);
OracleCommand cmd = new OracleCommand("foo", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("this_is_a_cursor", OracleType.Cursor).Direction = ParameterDirection.Output;
con.Open(); // Cause the error at runtime
OracleDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
con.Close();
I've noticed that some other people are having the same problem.
I'm running the application from a Windows Server 2003 Entreprise Edition(I hope this can help).
Thank you.
I'm not quite sure what the exact problem was, but I managed to connect to my database with another provider in the connection string.