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

Javeed M ShaikhRank #839 · 2.7K reputationPosted Oct 3, 2011, 7:29 AM EDT
Joshua29 reputationPosted Oct 3, 2011, 7:40 AM EDT
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 ShaikhRank #839 · 2.7K reputationPosted Oct 3, 2011, 7:59 AM EDT
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.
Joshua29 reputationPosted Oct 3, 2011, 9:53 AM EDT
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
Amit ChoudharyRank #145 · 15.8K reputationPosted Oct 3, 2011, 11:01 AM EDT
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:
public class Singleton where T : new() { private LazySingleton() { } public static T Instance { get { return SingletonCreator._instance.Value; } } class SingletonCreator { static SingletonCreator() { } internal static readonly Lazy _instance = new Lazy(() => new T()); } }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 public class DAL { static SqlConnection connection; public DAL() { connection = new SqlConnection(); connection.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["myConnectionString"]; } public DataTable GetDataTableByAdapter(string query) { connection.Open(); DataTable dt = new DataTable(); new SqlDataAdapter(query, connection).Fill(dt); connection.Close(); return dt; } } In this class we have a method and that method will open the connection use it and will close it before returning the final values. How to use both of the above classes to utilize the single object multiple times. class Program { static void Main(string[] args) { DAL dal = SingletonDAL>.Instance; string query = "Select * from Production.Product"; DataTable dt = dal.GetDataTableByAdapter(query); System.Console.Read(); } }I hope this is something you wanna know about the connection management. If i'm not wrong.. and surprisingly i've found some great
thing while i was creating this sample for you. Will soon publish in the article section and on my blog ;)
Happy Coding!!
Amit ChoudharyRank #145 · 15.8K reputationPosted Oct 3, 2011, 11:05 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Oct 3, 2011, 11:35 AM EDT
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.
Joshua29 reputationPosted Oct 3, 2011, 10:25 PM EDT
Thanks for the ideas. I will try it.
Joshua
Dhaval PatelRank #1,023 · 1.9K reputationPosted Oct 18, 2011, 4:05 AM EDT
Pravin MoreRank #1,419 · 1.2K reputationPosted Oct 18, 2011, 5:19 AM EDT
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.
Amit ChoudharyRank #145 · 15.8K reputationPosted Oct 18, 2011, 8:20 PM EDT