如何在c#中动态更改连接字符串 [英] How change connection string dynamically in c#`

查看:110
本文介绍了如何在c#中动态更改连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在做Windows项目,我要求连接字符串动态更改意味着当用户上传excel文件时,连接字符串应该是上传的文件s连接字符串

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

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 
}

推荐答案

配置是只读的,因此您不能以明显的方式执行此操作;

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



这会引发System.Configuration.ConfigurationErrorsException [ ^ ]异常,说配置只读。



这是一招使用反射重置配置元素的readOnly属性。有关详细信息,请参阅此文章以编程方式设置connectionString属性 [ ^ ]




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[^]

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





另请参阅:

如何在生成的数据集类中动态更改连接字符串? [ ^ ]

动态更改web.config中的connectionString [ ^ ]


因为数据集连接字符串是只读的,所以必须使用下面的代码才能修改其默认的只读连接字符串:)

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 :)
Console.WriteLine(Properties.Settings.Default["DataSetCS"]);
Properties.Settings.Default["DataSetCS"] = yourNewConnectionString;
Console.WriteLine(Properties.Settings.Default["DataSetCS"]);


您可以将您的连接字符串存储在app.config中并使用此代码更改连接的值strinh

you can store your connection string in app.config and use this code to change the value of connection strinh
ConfigurationManager.ConnectionStrings["student"].ConnectionString = "val";


这篇关于如何在c#中动态更改连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆