By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

How to change connection string for database class?

P: 26
Dear all

i wrote one class to deal with sql database to insert and read data from that class only...and based on that one of the parmeters or the class is the sqlconnection

SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename= {0};Integrated Security=True;Connect Timeout=30;User Instance=True");

so the connection string is defined indepented of the class objects of the class (i mean class objects used by the other class)

but I need to change the database file or I want to change the connection string

so how can I change the connection string for the whole class ...

What I did

i define the connection in the beginning of the class

SqlConnection connection = new SqlConnection();

then I wrote a method

public void conntstr(string r)
connection = new SqlConnection(r);

and I passed the connection string to it...however it doesn't worte?

what can I do
Mar 6 '09 #1
Share this Question
Share on Google+
6 Replies

P: 26
ok I tried the following solution however i got error

i tried the following solution

Expand|Select|Wrap|Line Numbers
  2. class sqldatabase
  3. {
  4. public static String connectionString;
  5. SqlConnection connection = new SqlConnection(connectionString);
  7. }
in my application I made the following
Expand|Select|Wrap|Line Numbers
  1. if (open.ShowDialog() == DialogResult.OK)
  2. {
  3.                 connectionstr = string.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename= {0};Integrated Security=True;Connect Timeout=30;User Instance=True", open.FileName);
  4. }                                 
  5. sqldatabase.connectionString = connectionstr;
however I got the following error when I start to access my database

The ConnectionString property has not been initialized.

any ideas?
Mar 6 '09 #2

Expert 5K+
P: 7,872
Is your connection string following the formats listed here:

It looks like you are missing the database name when attaching a db file?

It also looks like if you say cancel in your open file dialog, the connectionstring is still assigned to the object
Mar 6 '09 #3

Expert 100+
P: 229
First problem is that you shouldn't be creating a database connection just like that, "in the middle of nowhere".

Create a private string field which contains the connection string only, and then create a connection only when you need to do a transaction.

Expand|Select|Wrap|Line Numbers
  1. // add this to your class, but not as a static field
  2. private string connectionString = null;
  4. if (open.ShowDialog() == DialogResult.OK)
  5. {
  6.     // create the string using the specified name
  7.     connectionString = CreateConnectionString(open.FileName);
  8. }
Then, just before sending a SQL query, create your connection:

Expand|Select|Wrap|Line Numbers
  1. using (SqlConnection connection = new SQLConnection(connectionString)
  2. {
  3.     // do something with database
  4. }
This way you are sure that SqlConnection will be closed and disposed after use. It is also obvious that you have to define your connection string before creating a connection.

The second problem is, the way it is implemented now, you expect from a caller to set that static field before creating a new instance of your 'sqldatabase' class. That is not a common practice in OOP, and you should always try to follow common programming patterns.

Third suggestion would be to try to follow C# naming conventions (you can google it, here is one example). Classes are never named in lower case (i.e. sqldatabase should be SqlDatabase), to distinguish them from variables and class fields.
Mar 6 '09 #4

P: 26
but I create the connection string in one class and the methods which deal with the sql database on other class...ur solution available if both on the same class..
Mar 6 '09 #5

Expert 100+
P: 229
but I create the connection string in one class and the methods which deal with the sql database on other class
Nevertheless, you need to pass the connection string to SqlConnection to create it. I didn't say that the second part of the code needs to be in the same class as the first part, but you need to have the connection string ready before creating SqlConnection (that's what the error is telling you, after all).

Your class doesn't enforce that in any way - you are creating an SqlConnection from a static string, without knowing if it was initialized first. If you wrote it like this:

Expand|Select|Wrap|Line Numbers
  1. class SqlDatabase
  2. {
  3.     /// <summary>
  4.     /// Contains the connection string.
  6.     /// IN CONSTRUCTOR.
  7.     /// </summary>
  8.     private readonly string _connectionString;
  10.     public SqlDatabase(string connectionString)
  11.     {
  12.         _connectionString = connectionString;
  13.     }
  15.     public void DoSomeQueries()
  16.     {
  17.         using (SqlConnection connection = new SqlConnection(_connectionString))
  18.         {
  19.             // do something
  20.         }
  21.     }
  22. }
Now it is obvious that you can only create SqlDatabase object like this:

Expand|Select|Wrap|Line Numbers
  1. SqlDatabase sqlDatabase = new SqlDatabase(connectionString);
Mar 6 '09 #6

P: 26
thanks very it works
Mar 9 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.