Closing database connection in asp.net - asp.net

Can we close all known/unknown connections to database with the code?
I'm using Access database and my application gives the following error:
"Could not use ''; file already in use. "
I don't know which connection is opened and no closed, so is there a way to close all application's opened connections?

When working with disposable objects you should use using so they will get disposed, and in this case even closed, when leaving the using block. Your code should look something like:
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
// Do work here; connection closed on following line.
}
Read about OleDbConnection.
UPDATE: I missed that you were accessing an access database, so updated the code to use OleDbConnection instead.

Related

Can you insert a new database connection in an existing database connection in OLEDB?

I am creating a program in which I am trying to have a new database connection in an existing database connection in OLEDB using ASP.net. Is that even possible?
"Connection within connection" is likely not what you have in mind. But there is no issue with having both OleDb and SQLServer connections operating together in one ASP.NET application, you can add these 2 or many more connections to one app.
Answer is yes. Please make sure you close the connections after using by using statement or close() explicitly. More here.
using (OleDbConnection connection1 = new OleDbConnection(connectionString1))
using (OleDbConnection connection2 = new OleDbConnection(connectionString2))
{
connection1.Open();
connection2.Open();
// Do something
}

Connecting to sqlite file using Vaadin

My problem is that I can't connect to my sqlite database file using Vaadin. I always get a requested resource is not available error. What's wrong with my code?
private Container buildContainer() throws SQLException {
SimpleJDBCConnectionPool connectionPool = new SimpleJDBCConnectionPool(
"org.sqlite.JDBC", "jdbc:sqlite:e:/teszt.db", "", "");
SQLContainer container = new SQLContainer(new FreeformQuery(
"SELECT users.name, departments.name"
+ "FROM users"
+ "INNER JOIN departments ON users.department = departments.id",
connectionPool));
return container;
}
When troubleshooting some connection issue buried in layers of abstraction, try to get right to the simplest possible version of what you are doing.
First, connect to the database without Vaadin to verify that the db server is running and the connection code is wrong.
If that works fine, then the configuration problem is on the Vaadin side. Try connecting without the special sql container. Just open a connection and place some text on the screen to proce success. Also try connecting without a connection pool.

How to write unit case for remote database connection in asp web application

I am new to unit testing for web applications
I have a function which creates a connection to a remote mysql database and perform some operations on it .
I want to have a test case which tests the connection is closed or not after the operations on database.
for example
fun1()
{
ODBCConnection con = new ODBCConnection(connString);
con.open();
}
in the above function, the connection is not closed?
how do i check this? can any one help?
In .Net, it's generally best to open your connections immediately before you use them. So rather than building (and testing) a function that connects to the database, you build and test a function that returns the correct connectionstring. You also have a reference database for your testing environment, and so you build your data access methods and create their own connection and test them against your reference database, that the right results come back.
Okay, based on your comment I can help you. Since you will be opening and closing the connection in the same function (as you should), you can do this:
public void fun1()
{
using (ODBCConnection con = new ODBCConnection(connString))
{
con.open();
//use the connection here
}
//connection is closed here because of the using block, even if an exception is thrown
}
There is no need to check if the connection closes in the code above. It will be closed in a timely manner by the using block, and that's guaranteed as much as anything can be in software. Just make sure you use that pattern everywhere you use connections.
In unit testing, the "units" to be tested are methods/functions. You test that the function performs as you expect it to, and nothing more. If you want to test specifically if a connection is closed, than the way to do it is to write a function to close the connection, and test that.

Can't rename SQLite database file after using it

I'm trying to rename my SQLite database file after I'm done using it, however the file still appears to be opened by SQLite even after all my connections are closed.
Here is an example of what I'm doing:
using (DbConnection conn = new SQLiteConnection("Data Source=test.db"))
{
conn.Open();
DbCommand command = conn.CreateCommand();
command.CommandText = "select id from test";
command.ExecuteScalar();
}
File.Move("test.db", "test.db.test");
The Move call throws an IOException. This is the only connection that I have to this database file. Once the application ends I can move the file manually without a problem. I've tried various things such as explicitly setting Pooling=false in the connection string, manually calling Close before the connection is disposed, explicitly starting and committing a transaction, but none of this seems to help. Is there a way to force SQLite to close/release the database file without exiting the application?
I presume you are using SQLite.Net? I have just tried your code and works without problem with SQLite.Net 1.0.65, in VS 2005 on Vista.
DBCommand is IDisposable. Maybe you need to call Dispose on it or create it in a using statement.

Best way to establish a connection for use with an ADO.NET command object

I'm designing a web service in ASP.NET and VS2008, and am using typed datasets for retrieving table data. These work well and establish their own connections through their associated TableAdapter objects. Edit: I'm using VB, BTW!
I am now attempting to run a custom SQL string using a DataAdapter and a Command object, however I need to reference a Connection object in order for the Command to work. What is the best way to handle this? Should I:
a) Create a global connection object using Global.asax, retrieving the connection string from web.config? (I've been trying that one already, with not much success)
b) Create a class-level connection object using the InitialiseComponent method, also retrieving the ConnectionString from web.config?
c) Retrieve a Connection from one of the TableAdapters that I've already created in my typed DataSets?
d) Something else I haven't thought of yet?
BTW I've been finding it very difficult to extract a ConnectionString from web.config, so any help with that would be appreciated also!
I'm not entirely inexperienced with ASP.NET, but my last big project used VS2003, and I want to make sure that I'm using the current tools correctly.
To extract the connection string, use
WebConfigurationManager.ConnectionStrings["name"].ConnectionString
It's best to open and close the connections as close as possible to their use. ADO.NET will do connection pooling so that this won't be expensive:
var connectionString =
WebConfigurationManager.ConnectionStrings["name"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("query", conn))
{
conn.Open();
// Use the command
}
}
For Connection and data access problems I will advise you to go with some kind of Data Helpers like Microsoft Data Access Application Block
Here you can find small tutorial about how to use it.
For getting connectionstring from web.config use folowing methods
public static string GetConnectionString( string strConstringKey )
{
return ConfigurationManager.ConnectionStrings[strConstringKey];
}
public static bool GetConnectionString(string strConstringKey, ref string strConstring)
{
return (strConstring = ConfigurationManager.ConnectionStrings[strConstringKey] ) == null ? false : true ;
}

Resources