Hi
i'm looking for an example -> where the connection to a db is definde one time and then you can need it for all formulars
my question is
--------------------------------------------------------------------
i make a connection for example:
static String connString = Properties.Settings.Default.auftragsverwaltungConnectionString;
MySqlConnection myconn = new MySqlConnection(connString);
--------------------------------------------------------------------
and now i can use for all formulars
myconn.open
Code ...
myconn.close
thanks for help
joshua
Loading
Amit ChoudharyPosted Oct 19, 2011, 12:20 AM
Pravin MorePosted Oct 18, 2011, 9:19 AM
Try this solution
create 1 method which will return you opened connection like this.....
namespace DBLayer.Util
{
public class Dbconnection
{
public SqlConnection getcon()
{
SqlConnection dbConn = null;
try
{
dbConn = new SqlConnection(Properties.Settings.Default.auftragsverwaltungConnectionString.ToString());
dbConn.Open()
}
catch (Exception e1)
{
}
return dbConn;
}
}
so when you want to open connection just call this method like below....n close connection in every context ..i mean in every method
Dbconnection dbc=new Dbconnection();
dbc.getCon(); //this will open connection
// your code here
con.Close();
i hope this is up to your need.
Thanx,
Pravin.
Dhaval PatelPosted Oct 18, 2011, 8:05 AM
JoshuaPosted Oct 4, 2011, 2:25 AM
Thanks for the ideas. I will try it.
Joshua
Sam HobbsPosted Oct 3, 2011, 3:35 PM
Also, you can create a data source and use the data source. A data source would have just one set of data (columns or fields) so I am not sure if that would help you but if it would then the data source will do most of the details for you. Look for articles in this web site about the Entity framework.
You probably can benefit from muli-tier designs. You can create a Data Access Layer (DAL) separate from the rest of your application. Look for articles about things like that.
Amit ChoudharyPosted Oct 3, 2011, 3:05 PM
Amit ChoudharyPosted Oct 3, 2011, 3:01 PM
Let me show something I'm came with to solve your problem first of all create a singleton pattern so that you can use it to create class that can have only one instance and that instance would be available everytime you use it..
Singleton:
for .Net 3.5 version:
For .Net 4.0:
public class Singleton
{ private static readonly Lazy _instance = new Lazy(() => new T()); public static T Instance { get { return _instance.Value; } } }
Now you will use it thorughout your application if you want to create a singleton object for any class.
Lets see how your DB class will look like that'll manage the connection
JoshuaPosted Oct 3, 2011, 1:53 PM
thank you for the answer
yes this is wat i meen. If you have some formulars, is this not the normal way to solve the problem of connection with database ?
joshua
Javeed M ShaikhPosted Oct 3, 2011, 11:59 AM
Class DB
{
MySqlConnection myconn;
static String connString = Properties.Settings.Default.auftragsverwaltungConnectionString;
public DB()
{
myconn = new MySqlConnection(connString);
}
public OpenConnection()
{
if (myconn is closed or myconn is not null)
{
myconn.Open();
}
}
public CloseConnection()
{
if (myconn is open or myconn is not null)
{
myconn.Close();
myconn.dispose();
}
}
}
this is not an actual code and my have errors, but just to give you an idea i have written this. Let us know.
JoshuaPosted Oct 3, 2011, 11:40 AM
when i create a formular i give the code
static String connString = Properties.Settings.Default.auftragsverwaltungConnectionString;
MySqlConnection myconn = new MySqlConnection(connString)
in every formular.
my question is how can i create the code once an need for all formulars ?
So i only give the command
myconn.open
Code ..
mycone.close
in the particulair forms
thanks joshua
Javeed M ShaikhPosted Oct 3, 2011, 11:29 AM