Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am doing windows project on that i have a requirement that the connection string has change dynamically means when ever user upload a excel file on that time the connection string should be the uploaded file`s connection string
C#
private void txtExtractFile_TextChanged(object sender, EventArgs e)
{
     Compare();
}
 
public void Compare()
{ 
OleDbConnection con; // create connection
OleDbDataAdapter da;
con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\\Projects\\WIP\\Database.mdb");
// open the coinnection
con.Open();
DataTable dt = new DataTable();
da = new OleDbDataAdapter("SELECT * FROM Sysobjects where TABLE_TYPE=TABLE", con);
dt = con.GetSchema("tables");
int found = 0;
string s1 = "Emp";
string s2 = "Student";
string s3 = "Projects";
 

for (int i = 0; i < dt.Rows.Count; i++)
{
 
//if (dt.Rows[i][2].ToString().Contains("Emp") || dt.Rows[i][2].ToString().Contains("Student") || dt.Rows[i][2].ToString().Contains("Projects"))
//{
// found++;
 
//}
//
if (s1 == dt.Rows[i][2].ToString()
|| s2 == dt.Rows[i][2].ToString()
|| s3 == dt.Rows[i][2].ToString())
{
found++;
}
}
if (found == 3)
{
MessageBox.Show("hi");
}
//bool bfound = false;
//string s1 = "emp";
//for (int i = 0; i < dt.Rows.Count; i++)
//{
// s1 = dt.Rows[i][2].ToString();
// bFound = true;
 
//}
con.Close(); //Close the connection 
}
Posted
Updated 20-May-12 20:19pm
v2
Comments
Maciej Los 21-May-12 2:08am    
Where is a problem? What have you done so far?
MAKReddy 21-May-12 2:17am    
Hi Losmac i copeied code see the database path D:\\Projects\\WIP\\Database.mdb");

but this path will be change every time at that time i want to change in code behind also. how it will change dynamically this path.
Dholakiya Ankit 16-Jul-13 0:50am    
solutions 4 is right
CHill60 16-Jul-13 13:47pm    
And yet Solution 2 has the most votes!

Configuration is read only so you can not do it in obvious way like;
C#
ConfigurationManager.ConnectionStrings["student"].ConnectionString = "new value";

This raises System.Configuration.ConfigurationErrorsException[^] exception which saying that "Configuration is read only".

Here is a trick using reflection to reset readOnly attribute of configuration element. See this article for full details Programmatically setting a connectionString property[^]

C#
var settings = ConfigurationManager.ConnectionStrings[ 0 ];
var fi = typeof(ConfigurationElement).GetField(
              "_bReadOnly", 
              BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(settings, false);
settings.ConnectionString = "Data Source=Something";


Also refer:
How to dynamically change connection string in generated dataset class? [^]
Dynamically change connectionString in web.config[^]
 
Share this answer
 
v2
Comments
Mohamed Mitwalli 21-May-12 2:36am    
5+
Prasad_Kulkarni 21-May-12 2:54am    
Thank you Mohamed!
Amar zaidi 21-May-19 10:04am    
you're awesome
since the dataset connection string is read only you must use the code below to be able to modify its default read only connection string :)
C#
Console.WriteLine(Properties.Settings.Default["DataSetCS"]);
Properties.Settings.Default["DataSetCS"] = yourNewConnectionString;
Console.WriteLine(Properties.Settings.Default["DataSetCS"]);
 
Share this answer
 
v2
you can store your connection string in app.config and use this code to change the value of connection strinh
C#
ConfigurationManager.ConnectionStrings["student"].ConnectionString = "val";
 
Share this answer
 
Hellow, just create a separate win form app where u put options like
dbname

server name
uid (if available)
password (if availabe)

and a connect button.
on connect button just open and close the connection, if its get connected then save it to a file like each value in different line.
i.e.
mySqlServer
myDbName
myUid
myPass
and each time when u open or run ur project read the file line by line. \
Thats it ur done with dynamic connection string.
 
Share this answer
 
The simple way is to store connection string in the database. Create a table in your database. On each successfull data file upload, enter an entry in this table with appropriate connection string.

You only need to read that table to read the connection string. This should get you going.
 
Share this answer
 
Comments
tedebus 21-Feb-14 9:22am    
Is it a joke?
How can you connect to database without connection string?! It's a loop!

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