连接字符串中的问题 [英] Problem in Connection String
问题描述
大家好...
我正在编写Windows应用程序,以获取尚未附加到服务器但保留在硬盘中的MDF文件的所有表.
我正在使用以下方法.我正在从openfile对话框获取文件名.
但是当我尝试使用fill方法填充数据表时.
我收到异常关键字不受支持.\ sqlexpress; attachdbfilename"
还有一件事.如果我要对从openfiledialog中选择的数据库文件的名称进行硬编码,则代码可以正常运行.
代码如下
Hello all...
I am writing a windows application to get all the tables of an MDF file which is not already attached to the server but is kept in the hdd.
I am using the following method. I am taking the filename from an openfiledialog box.
But when i try to fill the datatable using the fill method.
I get the exception "keyword not supported .\sqlexpress;attachdbfilename"
One more thing. If I am hard coding the name of the database file I am choosing from openfiledialog then the code is running without any problems.
the code is as follows
openFileDialog1.Filter = "MDF Files| *.mdf";
DialogResult drr = openFileDialog1.ShowDialog();
if (drr == DialogResult.OK)
{
String db = ".\\SQLEXPRESS;";
String server = "AttachDbFilename=\"" + openFileDialog1.FileName + "\";";
String auth = "Integrated Security=True;User Instance=True";
String cnstr = db + server + auth;
SqlDataAdapter da = new SqlDataAdapter("select name from sys.tables", cnstr);
DataTable dttb = new DataTable();
da.Fill(dttb);
}
我什至尝试使用@表示法而不是使用\.问题仍然存在..
请帮忙.
I even tried using the @ notation instead of using \". Still the problem persists..
Please help.
推荐答案
您的代码在SQL Express之前具有:db = ".\\SQLEXPRESS;";
反斜杠,这是正确的,但是您的错误显示./sqlexpress;attachdbfilename
-前斜杠!
应该是这样的:
Your code has:db = ".\\SQLEXPRESS;";
backslash before SQL Express which is correct but your error says./sqlexpress;attachdbfilename
- a front slash!
It should be like:
Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
请参考: SQL Server的连接字符串 [cnstr"的值是什么.
Refer: Connection String for SQL Server[^]
Make sure that the connection string is correct. Use Visual Studio DEBUGGER and see what is the value of ''cnstr
'' being passed to adapter.
您应该对
使用连接字符串
sqlconnection con = new sqlconnection("data source = .; database =-databasename-; integrated security = true");
如果您的sql需要密码,那么
sqlconnection con = new sqlconnection("data source = .; database =-databasename-; uid = sa; pwd = sa");
you should use connection string for
sqlconnection con=new sqlconnection("data source=.;database=--databasename--;integrated security=true");
if your sql required password then
sqlconnection con=new sqlconnection("data source=.;database=--databasename--;uid=sa;pwd=sa");
这篇关于连接字符串中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!