Hi All,
How can this be done? When I have a method to open my SQL connection in one class and want to use the connection object again(it is still open) in another class that is in the same namespace, how can this be done? I do not want to open/close, open/close my connections. I want to open it once and at the end close it when everything is completed. I am have a hard time getting this...
thanks,
Loading
Sam HobbsPosted Mar 29, 2010, 4:48 PM
Something that is good programming is TableAdapters. If you use TableAdapters then Visual Studio will generate code that does a lot for you automatically. TableAdapters manage the connection for you. I wrote an article called "Database Table Update in a DataGridView without Writing Code" that will appear in this web site soon, hopefully today. The article won't help much to learn about TableAdapters but it does provide a very basic introduction. If you can learn about TableAdapters then that will make many things easier.
Space GhostPosted Mar 29, 2010, 8:45 AM
I sorted this out with a property of type SQLConnection.
private sql.SqlConnection _Myconn;
public sql.SqlConnection Myconn
{
get { return _Myconn; }
set { _Myconn = value; }
}
Thanks for responses!
Amit ChoudharyPosted Mar 29, 2010, 5:46 AM
Create a class common put it under the same namespace as your application files have.
Create a static method as Hirendra told you for opening a connection.
You can create a same static method for closing the connection too.
But don't forget to close your datareaders if you are using any. and dispose them after use
and you are done.
Please mark as answer if it helps.
Hirendra SisodiyaPosted Mar 29, 2010, 5:12 AM
you can make sql connection as public static like:
public static System.Data.SqlClient.SqlConnection SqlConn;
than you can use this with only class name like Class1.SqlConn
or
you can make property
thanks
Please mark as answere if it helps