连接字符串动态更新 [英] Connection String Dynamically updating
问题描述
我正在从Access DB创建多数据库应用程序。使用打开对话框我需要更新访问数据库的连接字符串数据库文件路径。这不起作用并给出错误:
没有可用的错误消息,结果代码:DB_SEC_E_AUTH_FAILED(0x80040E4D)。
但是当我声明时一个具有相同DB路径的字符串,并添加到连接字符串,它完美地工作。但不能达到我的目的。以下是代码:
(本手册声明有效)但不是从Load form Event ..中选取的DUP1变量,即使变量是使用从其他数据库。
I am creating multi DB application from Access DB's. Using a Open dialog box I need to update the connection string DB file path for the access DB. This does not work and gives the error:
No error message available, result code: DB_SEC_E_AUTH_FAILED(0x80040E4D).
But when I declare a string with the same DB path and add to the connection string it works perfectly. But dose not serve my purpose. Following is the code:
(This Manual Declarations works) But not the DUP1 variable picked from Load form Event.., even though variable is updated with path picked from the other DB.
static string ss = @"C:\Users\hp\Desktop\Master_Database.mdb";
static string DUP1;
static string HO1 = "";
static string MES1 = "";
static string dup = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DUP1 + ";Jet OLEDB:Database Password=1010101010 ";
static string sqlcon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hp\Documents\Database1.accdb";
OleDbConnection OleConn = new OleDbConnection(sqlcon);
OleDbConnection db = new OleDbConnection(dup);
OleDbConnection dbmes = new OleDbConnection(mes);
OleDbConnection dbho = new OleDbConnection(HO);
string SQL = "SELECT* from SalesDetails";
string sql1 = "SELECT * from SalesHeader";
private void Form1_Load(object sender, EventArgs e)
{
DataTable s = new DataTable();
OleConn.Open();
OleDbCommand adc = new OleDbCommand("SELECT* from Filepaths", OleConn);
OleDbDataAdapter u = new OleDbDataAdapter();
u.SelectCommand = adc;
u.Fill(s);
OleConn.Close();
DataRow j;
DataRow x;
DataRow t;
j = s.Rows[0];
x = s.Rows[1];
t = s.Rows[2];
DUP1 = j.ItemArray.GetValue(1).ToString(); //C:\Users\hp\Desktop\Master_Database.mdb (same path as Variable "SS")
HO1 = x.ItemArray.GetValue(1).ToString();
MES1 = t.ItemArray.GetValue(1).ToString();
但这不会打开DB(更新后的DUP1变量)!我做错了什么?
But this does not open the DB (Updated DUP1 variable)! What am I doing wrong?
推荐答案
嗨
在DUP1中获取路径后声明这个
Hi Declare this after u get path in DUP1
DataRow t;
j = s.Rows[0];
x = s.Rows[1];
t = s.Rows[2];
DUP1 = j.ItemArray.GetValue(1).ToString();
//here u declare string and create oledbconnection.what u try before u getting valule for DUP1 u try to create oldeconnection
static string dup = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DUP1 + ";Jet OLEDB:Database Password=1010101010 ";
OleDbConnection db = new OleDbConnection(dup);
问候
Aravind
Regards
Aravind
大家好我解决了。
我做了类似的事情..
Hi all I solved it.
I did something like this..
private void Form1_Load(object sender, EventArgs e)
{
<pre>static string dup = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DUP1 + ";Jet OLEDB:Database Password=1010101010 ";
OleDbConnection db = new OleDbConnection(dup);
并将代码插入执行查询的按钮,以便在执行查询时文件路径得到更新。
and inserted the code into the button which executes the query so the File path gets update at the time executing the query.
这篇关于连接字符串动态更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!