Can't connect to database (OCIenVCreate has failed) - c#

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.

Related

Getting a database error when trying to view data

Alright,
I have a page that interacts with my SQL data connection. it tries to run a stored procedure, but every time I run it, it locks out of the database showing this error:
Here's the code the fires of the connection and stored procedure `
FileUpload1.SaveAs(Server.MapPath("images//" + FileUpload1.FileName));
string Image = MapPath("images//" + FileUpload1.FileName);
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BOOKS.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand("AddaBook", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Title", SqlDbType.NVarChar).Value = txtTitle.Text;
cmd.Parameters.AddWithValue("#Image", SqlDbType.NVarChar).Value = Image;
cmd.Parameters.AddWithValue("#Author", SqlDbType.NVarChar).Value = txtAuthor.Text;
cmd.Parameters.AddWithValue("#PublishDate", SqlDbType.DateTime).Value = txtPublishDate.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
lblUploadResult.Text = "File uploaded successfully.";
lblUploadResult.ForeColor = System.Drawing.Color.ForestGreen;
}`
Any help is appreciated and always thank you!
Try moving your mdf file out of OneDrive. Sometime when OneDrive is doing an upload, or one of the many other processes it does, it will lock the files - especially from system user.
It is saying that your database is under usage so you can not access it.
Be sure that no other code is accessed in the database without closing the connection.
You may also update the code with the following as some not needed lines are removed.
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BOOKS.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand("AddaBook", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Title", SqlDbType.NVarChar).Value = txtTitle.Text;
cmd.Parameters.AddWithValue("#Image", SqlDbType.NVarChar).Value = Image;
cmd.Parameters.AddWithValue("#Author", SqlDbType.NVarChar).Value = txtAuthor.Text;
cmd.Parameters.AddWithValue("#PublishDate", SqlDbType.DateTime).Value = txtPublishDate.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
lblUploadResult.Text = "File uploaded successfully.";
lblUploadResult.ForeColor = System.Drawing.Color.ForestGreen;
You already have the file open but just don't know it. For example, if you are trying to open a text file called Test.txt (or conversely a .mdf database file which has already been opened), then you will get this error, which is also why you are likely getting the other errors down the line but all related to this first error.
With databases, things can get a bit tricky, because you can connect to a database and at times get an error and be kicked out of the system without closing the connection or process... at which point a currently running but unknown process may be keeping the connection alive. You can look at all open processes on your system or see if you mistakenly have the connection open and then close it before trying to open it through the application.

C# Using multiple MySQL connection or put queries in queue to execute respectively

I have a client/server app and my server stores data in a MySQL database, currently I have made a connection and I do queries without queue or something. I don't think this is a good solution for this, because when a MySQLDataReader opens another one can't be execute at the same time and first one must be closed. I think I have two options, either make a connection by every DataReader or put my queries in a queue to execute them one by one.
I want to know which one is the best or is there any way or something to prevent errors and exception which causes this error
There is already an open DataReader associated with this Connection which must be closed first.
This is how currently I am doing queries. I first get the main connection and do queries. it my causes above error.
string query = "SELECT * FROM users WHERE username = #username";
ServerModel.Database.CheckConnection(); // Get the main connection
MySqlCommand cmd = new MySqlCommand(query, ServerModel.Database);
cmd.Parameters.AddWithValue("#username", username);
UserStatus userStatus;
using (MySqlDataReader dataReader = cmd.ExecuteReader())
{
if (dataReader.Read())
{
...
dataReader.Close();
return userStatus;
}
}
To note that this server may do thousands of queries at moment. think about a chat server.
In this case please don't use the using block, I hope below approach will work fine.
string query = "SELECT * FROM users WHERE username = #username";
ServerModel.Database.CheckConnection(); // Get the main connection
MySqlCommand cmd = new MySqlCommand(query, ServerModel.Database);
cmd.Parameters.AddWithValue("#username", username);
UserStatus userStatus;
MySqlDataReader dataReader = cmd.ExecuteReader()
if (dataReader.Read())
{
...
dataReader.Close();
return userStatus;
}

Open database into application c#

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.

ADO.Net c# 0x80131904 must declare scalar variable/function

I'm trying to use a function via a SQL connection I've done everywhere else in my application (only here it give the error, not the rest of the application). When i searched for what that error code meant the responses i found say it's an error when one can't connect to SQL server? but it doesn't give a solution.
here is my c# code
SqlConnection connection = Database.GetConnection();
DataTable dt = new DataTable("CRC");
SqlCommand cmd = new SqlCommand("SELECT dbo.CalcRentalCharge(#RentalStartDateTime,#RentalEndDateTime,#CarTypeID)", connection);
try
{
connection.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("#RentalStartDateTimetext", SqlDbType.DateTime).Value = RentalStartDateTimeBox.Text;
cmd.Parameters.Add("#RentalEndDateTimetext", SqlDbType.DateTime).Value = RentalEndDateTimeBox.Text;
cmd.Parameters.Add("#CarTypeIDtext", SqlDbType.Int).Value = CarTypeID.Text;
connection.Open();
Decimal rentalChange = (Decimal)cmd.ExecuteScalar();
connection.Close();
MessageBox.Show("The rental change is: " + rentalChange.ToString());
if (dr.HasRows)
{
dt.Load(dr);
dataGridView1.DataSource = dt;
}
}
connection.Close();
Can you help me get this FUNCTION to work?
Don't use cmd.ExecuteReader() before adding parameter to command object.
It gives error,
add parameter to command and then cmd.execureReader()
You have a copy/paste error in your variable name:
In the line
cmd.Parameters.Add("#RentalStartDateTimetext", SqlDbType.DateTime).Value = RentalStartDateTimeBox.Text;
the string
RentalStartDateTimetext
needs to be
RentalStartDateTime
In addition to that, because it will pop up as your next error: Your opening and closing of the connection is wrong. Use a using block for the connection as well and open it directly after the start of the block. You don't need to close it manually, the using-block will do that for you.

Connecting to MYSQL from a c# application

the underlying database on a project has changed from sql 2005 to MySql 5.1
The code has loads of method similar to below. I'm presuming it is just a case of switching the 'con' variable SqlConnection to a MYSql specific connection. Has anyone had any experience with this? I have never touched a mySql db. Any help much appreciated.
private SqlConnection con;
public User LogonUser(string pUserName, string pPassword)
{
con = new SqlConnection();
con.ConnectionString = DatabaseConstants.DB_CONN_STRING;
using (con)
{
con.Open();
var command = new SqlCommand();
command.Connection = con;
command.CommandText = "SELECT id FROM Users WHERE userName = #userName AND password = #password";
command.CommandType = CommandType.Text;
var userName = new SqlParameter("#userName", pUserName);
var password = new SqlParameter("#password", pPassword);
command.Parameters.Add(userName);
command.Parameters.Add(password);
User user;
var dr = command.ExecuteReader();
if (dr != null)
if (dr.HasRows)
{
while (dr.Read())
{
user = new User();
user.id = dr.GetString(0);
return user;
}
}
else
{
throw new Exception("Can not find user, please check your username and password");
}
}
return null;
}
You got it partially correct, but you will need an instance of the MySQL Provider, not the SqlConnection. Also you will have to change any SQL that isn't compatible with MySQL.
Downloadable SQL Connectors are available for various frameworks and platforms - in this case assemblies to reference into your .NET project under the guise of ADO.NET are available from MySql. Can program against them using any .NET language.
Start in C# by referencing the MySql namespace:
using MySql.Data.MySqlClient;
and change over your ADO.NET class names from SqlConnection to MySqlConnection, etc. Google Code examples show cursory usage (similar to other ADO.NET providers), and of course the MySql docs are the best reference.
No, you have to also change this line
var command = new SqlCommand();
to
var command = new con.CreateCommand();
and of course you have to change any specific T-SQL and MSSQL features to MySQL. Date and time function, stored procedure, and parameter binding(? instead of #) are a few things that you need to closely check.

Categories

Resources