I have the following ridiculously simple code:
static void Main(string[] args)
{
using (OracleConnection conn = new OracleConnection("Data Source=tnsname;User Id=zzzz;Password=xxxx"))
{
using (OracleCommand cmd = new OracleCommand("SELECT * from CONTRACT"))
{
conn.Open();
IDataReader reader = cmd.ExecuteReader();
}
}
}
}
Obviously I've changed the connection string, but if the connection string is wrong, the conn.Open() call fails, so I know the connection string is correct, at least as far as the data source, User Id, and Password.
When it gets to the cmd.ExecuteReader() call, however, I get an InvalidOperationException with the message, Invalid operation. The connection is closed.
I've done a lot of SQL Server stuff from C#, but this is the first time I've used OracleClient. Can't see anything obviously wrong, other than the fact that it's deprecated, but I'd figure it would still function. I'm not trying to write any production code, I'm just trying to do a little one-off test.
You haven't associated your connection object with command.
cmd.Connection = conn;
or pass it in Command constructor like:
using (OracleCommand cmd = new OracleCommand("SELECT * from CONTRACT", conn))
You haven't assigned the Connection instance to the OracleCommand instance
static void Main(string[] args)
{
using (OracleConnection conn = new OracleConnection("Data Source=tnsname;User Id=zzzz;Password=xxxx"))
{
using (OracleCommand cmd = new OracleCommand("SELECT * from CONTRACT", conn))
{
conn.Open();
using(IDataReader reader = cmd.ExecuteReader())
{
.....
}
}
}
}
Just add the conn instance to the constructor of the command.
By the way, also the reader should be enclosed in a using statement
Related
In my business logic, I use multiple oracle query's multiple times. What is the best way to open and close the oracle connection?
private void update()
{
OracleConnection con = new OracleConnection("Connection Statement");
OracleCommand command = new OracleCommand("Select Statement");
con.Open();
OracleDataReader reader = command.ExecuteReader();
reader.Close();
con.Close();
// A for loop
con.Open();
command = new OracleCommand("Update statement");
command.ExecuteNonQuery();
con.Close();
con.Open();
command = new OracleCommand("Second Update statement");
command.ExecuteNonQuery();
con.Close();
}
My code looks like this. Should I open and close my oracle connection for every command or open before the first command and close after the last command.
P.S. This update function is called over 100 times in my application.
As the connection is local to the method create it in a using block and then use it as many times as you need to in that block. The block can contain loops or whatever, there is no rule that states you have to discard the connection after you use it once.
However connection sharing is discouraged, so do not create a class level instance or static instance for shared use.
private void update()
{
using(OracleConnection con = new OracleConnection("Connection Statement"))
{
con.Open();
using(var command = new OracleCommand("Select Statement", con))
using(OracleDataReader reader = command.ExecuteReader()}
{
}
// A for loop
using(var command = new OracleCommand("Update statement", con))
{
command.ExecuteNonQuery();
}
using(var command = new OracleCommand("Second Update statement", con))
{
command.ExecuteNonQuery();
}
}
}
We are getting Timeout expired exception on SqlConnection.Open().
Below is the code :
public int ExecuteNonQuery(SqlParameter[] param, string strSPName)
{
using (SqlConnection conn = new SqlConnection(_connStr))
{
int i = 0;
using (SqlCommand cmd = new SqlCommand(strSPName, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(param);
conn.Open();
i = cmd.ExecuteNonQuery();
}
return i;
}
}
Does the return keyword inside the using statement leaving the connection to opened and hence this issue?
Does the return keyword inside the using statement leaving the connection to opened and hence this issue?
No. The using statement is effectively a try/finally block statement a Dispose call in the finally part - so your connection will still be disposed at the end for the method.
I suspect that either you're just calling this from too many threads at the same time and exhausting your pool that way, or you're opening a connection elsewhere without closing it.
Note that you can make your code simpler by getting rid of the i local variable:
public int ExecuteNonQuery(SqlParameter[] param, string strSPName)
{
using (SqlConnection conn = new SqlConnection(_connStr))
{
using (SqlCommand cmd = new SqlCommand(strSPName, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(param);
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
Again, the command and connection will still be disposed appropriately.
Is this the following code healthy? Or I don't need to use the using keyword as the SqlDataAdapter will handle closing the connection?
public static DataSet Fetch(string sp, SqlParameter [] prm)
{
using (SqlConnection con = new SqlConnection(ConStr))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sp;
cmd.Parameters.AddRange(prm);
using (SqlDataAdapter dta = new SqlDataAdapter(cmd))
{
DataSet dst = new DataSet();
dta.Fill(dst);
return dst;
}
}
}
}
#MarkGravell I need a suggestions here, I am really looking to use DataReader, but I was looking all the time to use the using keyword to ensure closing the connections. Where with DataReader we can not use it because it will close the connection if we want to return the DataReader back to some method.
So do you think the following technique is fine with DataReader and the using keyword:
public static SqlDataReader Fetch(string sp, SqlParameter [] prm)
{
SqlCommand cmd = new SqlConnection(ConStr).CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sp;
cmd.Parameters.AddRange(prm);
cmd.Connection.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
using (SqlDataReader dtrPrize = Sql.Fetch("SelectPrize", new SqlParameter[] { new SqlParameter("id", id) }))
{
dtrPrize.Read();
Prize prize = new Prize();
prize.id = (int)dtrPrize[dtrPrize.GetOrdinal("id")];
prize.artitle = (string)dtrPrize[dtrPrize.GetOrdinal("artitle")];
prize.entitle = (string)dtrPrize[dtrPrize.GetOrdinal("entitle")];
prize.ardetail = (string)dtrPrize[dtrPrize.GetOrdinal("ardetail")];
prize.endetail = (string)dtrPrize[dtrPrize.GetOrdinal("endetail")];
prize.image = (string)dtrPrize[dtrPrize.GetOrdinal("image")];
prize.theme = (string)dtrPrize[dtrPrize.GetOrdinal("theme")];
prize.price = (int)dtrPrize[dtrPrize.GetOrdinal("price")];
prize.audience = (int)dtrPrize[dtrPrize.GetOrdinal("audience")];
prize.type = (byte)dtrPrize[dtrPrize.GetOrdinal("type")];
prize.status = (byte)dtrPrize[dtrPrize.GetOrdinal("status")];
prize.voucher = (string)dtrPrize[dtrPrize.GetOrdinal("voucher")];
prize.supplierid = (int)dtrPrize[dtrPrize.GetOrdinal("supplierid")];
prize.created = (DateTime)dtrPrize[dtrPrize.GetOrdinal("created")];
prize.updated = (DateTime)dtrPrize[dtrPrize.GetOrdinal("updated")];
return prize;
}
Healthy-ish; personally I'd say the unhealthy bit is the bit where it makes use of DataSet and DataAdapter, but that is perhaps just my personal bias.
Yes, you should dispose the adapter etc here (which is what the using does for you, obviously).
As a trivial pointless tidy, you can stack the usings - just makes it a little less verbose:
using (SqlConnection con = new SqlConnection(ConStr))
using (SqlCommand cmd = con.CreateCommand())
{
It will be enough to leave just the first using (the one on the Connection) because disposing the connection will dispose everything you need disposed.
However, there is no harm disposing everything, just a bit more code.
private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}",
reader[0], reader[1]));
}
// Call Close when done reading.
reader.Close();
}
}
How can I enhance the method above to accept any queryString? The problem is in the while. There's a fixed # of columns I can read. I want to be able to read any number of columns so that I can populate and return a DataSet. How can I do it?
You can do something along these lines:
private static void ReadOrderData(string connectionString,
string query, Action<SqlDataReader> action)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand(query, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
action(reader);
}
// Call Close when done reading.
reader.Close();
}
}
You're really barking up the wrong tree. You shouldn't be using "methods that accept query strings". You should raise the level of abstraction by using Entity Framework or the like.
You will then not need to use the above code because it will not exist. Those who would have called that code will do something like this:
var orders = from o in ordersDAL.Orders
select new {o.OrderID, o.CustomerID};
foreach (var order in orders)
{
Console.WriteLine("{0}, {1}", order.OrderID, order.CustomerID);
}
Your code is badly designed in any case. Why in the world would you combine the fetching of the data with the use of it? That while loop should not be in that same method.
I would use something like the answer from obrok, but I would add the ability to use parameters.
Also, the SqlCommand and SqlDataReader both need to be within a using block:
private static void ReadOrderData(string connectionString,
string query, Action<SqlDataReader> action)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command =
new SqlCommand(query, connection)) {
connection.Open();
using (SqlDataReader reader = command.ExecuteReader()) {
// Call Read before accessing data.
while (reader.Read())
{
action(reader);
}
// No need to call Close when done reading.
// reader.Close();
} // End SqlDataReader
} // End SqlCommand
}
}
Use SqlDataAdapter:
private static DataSet ReadData(string connectionString, string queryString)
{
DataSet dataSet;
using (SqlConnection connection =
new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command =
new SqlCommand(queryString, connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(dataSet);
}
return dataSet;
}
Or something like this.
How I'm doing this:
(basically idea is to use DataSet along with IDataAdapter)
DataSet ds = null;
List<SqlParameter> spParams = ...
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand(spName, connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Clear();
command.Parameters.AddRange(spParams);
connection.Open();
IDbDataAdapter da = new SqlDataAdapter();
da.SelectCommand = command;
ds = new DataSet("rawData");
da.Fill(ds);
ds.Tables[0].TableName = "row";
foreach (DataColumn c in ds.Tables[0].Columns)
{
c.ColumnMapping = MappingType.Attribute;
}
}
}
// here is you have DataSet flled in by data so could delegate processing up to the particular DAL client
You said you tried EF, but did you try Dapper ? looks like a simple enough ORM that should work with your database (it's raw SQL), and will avoid you most of this mapping code. Dapper is used in StackOverflow so it cannot be too bad :)
Same suggestion as 'WorkerThread' but change the Method Signature to:
private static DataSet ReadOrderData(string connectionString, string queryString)
{
// do work
}
Drop the following line from 'WorkerThread' example:
string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;";
Once you have made these two changes to 'WorkerThread's' method it should be perfect for what you need.
Look at the DataTable.Load method. Or, for a finer level of control, check out the properties and methods on IDataReader, such as FieldCount, GetFieldType, and GetName.
Or what's the recommended way to read and write at the same time? Having two connections open is the only way? This is on a plain Windows Forms C# (.NET 3.5) app
using (SqlConnection conn = new SqlConnection(connStr)) {
SqlCommand cmdRead = new SqlCommand("select stools from foo", conn);
SqlDataReader rdr = cmdRead.ExecuteReader();
SqlCommand cmdWrite = new SqlCommand("insert into bar values (#beer)", conn);
SqlParameter beer = new SqlParameter("#beer", SqlDbType.Int);
cmdWrite.Parameters.Add(beer);
while(rdr.Read()) {
int stools = rdr.GetInt32(0);
cmdWrite.Parameters["#beer"].value = stools;
//Next line fails for an open data reader associated to the "command"
cmdWrite.ExecuteNonQuery();
}
rdr.Close()
}
This, OTOH, works, but looks ugly to me (besides of opening an extra connection)
using (SqlConnection connR = new SqlConnection(connStr)) {
using (SqlConnection connW = new SqlConnection(connStr)) {
SqlCommand cmdRead = new SqlCommand("select stools from foo", connR);
SqlDataReader rdr = cmdRead.ExecuteReader();
SqlCommand cmdWrite = new
SqlCommand("insert into bar values (#beer)", connW);
SqlParameter beer = new SqlParameter("#beer", SqlDbType.Int);
cmdWrite.Parameters.Add(beer);
while(rdr.Read()) {
int stools = rdr.GetInt32(0);
cmdWrite.Parameters["#beer"].value = stools;
cmdWrite.ExecuteNonQuery();
}
rdr.Close()
}
}
In this simple case, read all the stools, store them in a list, close the reader and then write them will work (as long as there aren't many stools in the database), but in more complex cases it starts being unwieldy and memory hungry, so that's also not desirable.
With SQL 2005 and later, you can use Multiple Active Result Sets (MARS):
http://msdn.microsoft.com/en-us/library/ms345109%28SQL.90%29.aspx
http://msdn.microsoft.com/en-us/library/ms131686.aspx
For this to work, you need to enable it in your connection string.
Why not having two methods: one for reading and one for writing, each using its own connection drawn from the connection pool.
Reading
using (SqlConnection conn = new SqlConnection(connStr))
using (SqlCommand cmdRead = new SqlCommand("select stools from foo", conn))
{
conn.Open();
using (SqlDataReader rdr = cmdRead.ExecuteReader())
{
while(rdr.Read())
{
int stools = rdr.GetInt32(0);
}
}
}
Writing
using (SqlConnection conn = new SqlConnection(connStr))
using (SqlCommand cmdWrite = new SqlCommand("insert into bar values (#beer)", conn))
{
conn.Open();
SqlParameter beer = new SqlParameter("#beer", SqlDbType.Int);
cmdWrite.Parameters.Add(beer);
cmdWrite.Parameters["#beer"].value = stools;
cmdWrite.ExecuteNonQuery();
}
And then just call them on different threads. This makes methods clearer and more specific.
You can't write while the DataReader is open, creating a second connection is needed in this situation, and IMHO it's not a design issue.
Well the obvious question is why are you doing this in code at all?
You're selecting from one table to write into another - that screams insert query to me.
Doubtless the real world example is more complex in which case you need two connections because datareaders work by holding the connection open 'til you're done and that's more or less that.