Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 30th, 2006, 05:01 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default where to put my connectionstring

Hi,

I just began te learn C# with a background off VB.NET so I'm an absolute beginner. So, here I am with my first problem : where do I have to put my connectionstring so that I can use an every form of my application. If I place the connectionstring on one form I have to do it again for another. What is the best way to deal with this problem ?

Thanks in advance,

Pascal.

Reply With Quote
  #2 (permalink)  
Old November 30th, 2006, 10:11 PM
Authorized User
 
Join Date: Sep 2006
Location: , , India.
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi , Create a new module and place the variables in that module.Then U can access the variables in any form in the project.




Reply With Quote
  #3 (permalink)  
Old November 30th, 2006, 10:14 PM
Authorized User
 
Join Date: Sep 2006
Location: , , India.
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 Hi ,I thought u are asking for vb.net.
For c# U can create a class and declare a static string variable and assign connection string . Then u can acess this variable from any form using
classname.varname syntax




Reply With Quote
  #4 (permalink)  
Old November 30th, 2006, 10:31 PM
Authorized User
 
Join Date: Sep 2003
Location: Park City, UT, .
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Pascali70,

I'm going to make a few assumptions:

  a. You are using Visual Studio 2005.
  b. You are developing a windows forms application.

You have many good options, but two that I frequently use, are as follows. NOTE that neither of these are acceptable for storing passwords.

1. When Visual Studio builds the framework for a Windows Forms application, it automatically builds a class called Program. Look for it in your solution explorer (Ctrl + Alt + L). If I'm not mistaken, the class is a static class (similar to a shared class in VB, again if I remember correctly).

Add a global variable, or constant containing your connection string. For instance:

Code:
  static class Program
  {
    //------------------------------
    // Global Variables & Constants
    //------------------------------

    public const string DEFAULT_CONNECTION_STRING = @"Data Source=.\sqlexpress;Database=Northwind;Integrated Security=SSPI;";

    //------------------------------
    // Public Methods
    //------------------------------

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
    }
  }
2. Another simple way to store and access a connection string is to store it as a resource. Again, look in your Solution Explorer (Ctrl + Alt + L), but this time, look for something called "Properties." Double click on it. The window that opens will have a number of tabs on the left side. Click on the "Resources" tab. This will display all the resources that the project is storing.

There are two buttons at the top left of the window. Drop down the first button and select "Strings." At this point, you will be looking at all the string resources stored with the project. You can enter a Name-Value pair, such as:

Code:
  Name                 Value
  -----                --------------
  MyConnectionString   Data Source=.\sqlexpress; Database=Northwind; Integrated Security=SSPI;
Then you can access the connection strings from some other class within your application as is illustrated from the constructor of a class called Form1.

Code:
  public partial class Form1 : Form
  {
    //------------------------------
    // Member Variables
    //------------------------------

    protected string m_connectionString = "";
    protected string m_otherConnectionString = "";

    //------------------------------
    // Constructors
    //------------------------------

    public Form1()
    {
      InitializeComponent();

      m_connectionString = Program.DEFAULT_CONNECTION_STRING;
      m_otherConnectionString = Properties.Resources.MyConnectionString;
    }
  }

Cheers.

- Roger Nedel
Reply With Quote
  #5 (permalink)  
Old December 1st, 2006, 12:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

And just for completeness sake, don't forget the App.config or Web.config file. No need to recompile your app when you add or change strings in configuration files.

In an App.config file in a Windows app, or a Web.config file in a web app, add:

<configuration>
    <appSettings>
        <add key="YourDatabase"
             value="data source=YourServer;initial catalog=YourDatabase;user id=sa;password=password" />
    </appSettings>
</configuration>

Use in code like:

OleDbConnection cnn = new OleDbConnection(ConfigurationSettings.AppSettings["YourDatabase"]);
cnn.Open();

HTH,

Bob

Reply With Quote
  #6 (permalink)  
Old December 1st, 2006, 11:38 AM
Authorized User
 
Join Date: Nov 2006
Location: New Delhi, Delhi, India.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Experts
Is it possible to create a module in C#.If Yes then How.
thanks
krashishroa@yahoo.co.in

Thanks and Regards
Kumar Ashish
9350789189
Reply With Quote
  #7 (permalink)  
Old December 1st, 2006, 11:41 AM
Authorized User
 
Join Date: Nov 2006
Location: New Delhi, Delhi, India.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by SriRamaKrishna
 Hi , Create a new module and place the variables in that module.Then U can access the variables in any form in the project.

Hi
Is it possible to create module in C#.If yes then Reply me.
Thanks
Ashish
krashishroa@yahoo.co.in


Thanks and Regards
Kumar Ashish
9350789189
Reply With Quote
  #8 (permalink)  
Old December 1st, 2006, 05:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

No. But using static members in a C# class gives you the same behavior as using a standard module in VB.

Bob


Reply With Quote
  #9 (permalink)  
Old December 4th, 2006, 01:08 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks.

Reply With Quote
  #10 (permalink)  
Old June 20th, 2007, 01:42 PM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
the better and easiest approach is to add the constring in Web.config
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ado.connectionstring ankttv Access VBA 4 November 7th, 2008 04:54 PM
Tracing connectionstring miguel.ossa ASP.NET 2.0 Basics 4 January 8th, 2008 04:24 PM
the ConnectionString Property... coolb C# 3 September 26th, 2006 02:05 AM
ConnectionString not initialized samiboy ASP.NET 2.0 Basics 2 April 30th, 2006 06:18 AM
The ConnectionString property...! rajesh0363 All Other Wrox Books 2 July 26th, 2005 01:55 PM



All times are GMT -4. The time now is 03:57 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.