Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
SqlConnection con = new SqlConnection(@"DataSource=.\SQLEXPRESS;AttachDbFilename=E:\prashantcs\crm111\CRM111.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
I have the code shown above.I am declaring it again and again for each and every page
The reason why i am asking this is i need to install it different desktops so every time i am changing the path for all the pages so that it takes that particular computers path and name.ill change it but only once if i declare it globally.

Is there any method for declaring it globally.

plz help me..
Posted
Updated 11-Dec-11 20:22pm
v2
Comments
Karthik Harve 12-Dec-11 2:22am    
added pre tags.

if all the desktops or PCs are in Network then you can create a network drive in one of the PCs and copy the mdf file in that shared drive. so that all the systems can access the file from that network path.

see here for more on netwrok drives..

http://support.microsoft.com/kb/308582[^]

http://compnetworking.about.com/od/windowsxpnetworking/ht/mapnetworkdrive.htm[^]

and specify that network path in your connection string.

hope it works..
 
Share this answer
 
Comments
Member 8388026 12-Dec-11 2:36am    
no they are not in one network
Member 8388026 12-Dec-11 2:38am    
they are not in one network but different place
you can add a class and make method like
C#
public class Db
{
	public Db()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static SqlConnection GetConnection()
    {
        SqlConnection cn = new SqlConnection();
       // cn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["cnn"].ToString();
cn.ConnectionString=@"DataSource=.\SQLEXPRESS;AttachDbFilename=E:\prashantcs\crm111\CRM111.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        cn.Open();
        return cn;
    }
}


use this method as
C#
SqlCommand cmd12111 = new SqlCommand();
           cmd12111.CommandText = "insert_File_Request1";
           cmd12111.CommandType = CommandType.StoredProcedure;
           cmd12111.Connection = Db.GetConnection();//Db class method
           cmd12111.Parameters.AddWithValue("ReqControlNo", ReqControlNo);
           cmd12111.Parameters.AddWithValue("ReqDate", ReqDate);
           cmd12111.Parameters.AddWithValue("ReqTime", ReqTime);
           cmd12111.ExecuteNonQuery();
           cmd12111.Connection.Close();


hope it will helps you.
 
Share this answer
 
Comments
Member 8388026 12-Dec-11 2:47am    
if i do so in one aspx then it should get attached to all the other aspx files also.
uspatel 12-Dec-11 3:44am    
You will call this method as you need to make connection with database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900