Urgent C# Global Connection Problem
I have problem in C#.I want to make a global connection for entire windows application in C#.net.
I have this problem becoz there is no concept of adding modules in C#.net ,unlike vb.net which has a functionality of adding modules.
So what can i do in C3.net.
Re: Urgent C# Global Connection Problem
A module in VB.NET is purely a concept. When compiled it is implemented as a NotInheritable class with all its members declared as Shared. You can just as easily create a data access class in your C# apps that provides the same functionality. The C# equivalent of NotInheritable is sealed, which you may or may not choose to include in the declaration, and static is the equivalent of Shared. It is generally frowned upon to create a class that does not encapsulate some conceptual object, but if you think about the implementation carefully it shouldn't be too hard to create a data access class that adheres to OO guidelines.
Re: Urgent C# Global Connection Problem
Hi
i have one more question ,if instead i make my functions etc as static in my datalayer , what if i create object of this layer as static in my business layer and then access all variables or functions as i don't want to make object of this datalayer again and again in every form coz it is to be accessed globally in my project.
plz help me whether it will affect any working or any drawback or problem or any other alternate good solution .
Thanks
Pankaj
Re: Urgent C# Global Connection Problem
You don't need to create an instance of a class to call static members. They are members of the class, not of any particular instance. An example is MessageBox.Show. You don't have to create an instance of the MessageBox class every time you want to call it, but rather just call it using the class name.
Re: Urgent C# Global Connection Problem
u can create a class and declare the connection as static.
public static Sqlconnection scon
Re: Urgent C# Global Connection Problem
Quote:
Originally Posted by PankajGupta
Hi
i have one more question ,if instead i make my functions etc as static in my datalayer , what if i create object of this layer as static in my business layer and then access all variables or functions as i don't want to make object of this datalayer again and again in every form coz it is to be accessed globally in my project.
plz help me whether it will affect any working or any drawback or problem or any other alternate good solution .
Thanks
Pankaj
Hmmm... I think I missed the point of this post. Yes, you can create a single instance of your data layer and assign it to a static variable so that it can be accesses from everywhere in your application. Global variables should be handled with care and only used when necessary, but after about two seconds careful consideration I would say that this is a legitimate use. It may actually be preferable, though, to make your data layer a Singleton class, and give it a static method that returns that single instance.
Re: Urgent C# Global Connection Problem
Hi
create a single instance of my data layer and assign it to a static variable so that it can be accesses from everywhere in my application(For that agang i have to use a class and declare that membet as static etc).
but your point (It may actually be preferable, though, to make your data layer a Singleton class, and give it a static method that returns that single instance).I agree with that also , but could you plz tell me the difference between these two situation.
and one more question that whatif i made constructor of my data layer as static and then just call any static method via classname.methodname.
Here also i get rid of that situation of creating any instance from any method or assigning it to any other static member .I am also giving my class structure below..
public sealed class DataService
{
public static OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter();//Global Adapter
public static DataSet MyDataset = new DataSet();//Globaldataset
public static OleDbConnection MyConnection = new OleDbConnection();
public static OleDbDataReader MyDataReader ;
private static bool IsConnected ;
public static string MyConnectionString;
private static int NavigationCounter = 1;//This variable is user for navigation Control only
public static ArrayList ArrGlobal = new ArrayList();//This is used to fill Current values of current record
public static Form CurrentForm = new Form ();//This Variable represent Current form
public static string TableName ;//For Holding Current TableName
public static OleDbCommand MyCommand=new OleDbCommand();
//Constructor-----------------------
static DataService()
{
try
{
MyConnection.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Masu;Data Source=TRAFFIC";
MyConnection.Open();
IsConnected = true;
MyConnectionString = MyConnection.ConnectionString;
MyCommand.Connection = MyConnection;
}
catch (System.Exception ex)
{
MessageBox.Show (ex.Message);
IsConnected = false;
}
}
//-------------------------------
public static DataSet FillDataSet(string Sql,string StrTableName)
{
DataSet myLocaldataset=new DataSet() ;
try
{
MyCommand.CommandText =Sql;
MyDataAdapter.SelectCommand=MyCommand;
MyDataAdapter.Fill(myLocaldataset,StrTableName) ;
MyDataset=myLocaldataset;
TableName=StrTableName;
return myLocaldataset;
}
catch ( System.Exception ex)
{
MessageBox.Show (ex.Message);
return myLocaldataset;
}
}
//-----------------------------------
Re: Urgent C# Global Connection Problem
The difference between creating an instance of your data layer object and assigning it to a static variable and creating a singleton class is that with a singleton class you cannot create more than one instance, whereas in the first scenario you could create more instances and assign them to other variables. The idea of a class with all static members is going back to what I was talking about in the first place. It is the equivalent of a VB.NET module.
Re: Urgent C# Global Connection Problem
Re: Urgent C# Global Connection Problem
Don't forget to resolve your thread from the Thread Tools menu.