我添加了一个外部数据源到我的C#应用程序(我把它放在根目录下一个名为“数据”文件夹中),但是当我运行程序我在con.Open()
得到一个错误:SQL Server连接问题
An attempt to attach an auto-named database for file C:\Users\alex\documents\visual studio 2010\Projects\Network_Remote_Monitoring\Network_Remote_Monitoring\bin\Debug\data\bd.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
这里是我的源代码:
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data\\bd.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
ds1 = new DataSet();
ds2 = new DataSet();
con.Open();
string sql = "SELECT * From localitati";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da.Fill(ds1, "localitati");
sql = "SELECT * From sucursale";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da.Fill(ds1, "sucursale");
con.Close();
当我用它的工作实际路径替换connectionString。我的问题是如果我开发一个应用程序,我怎么能参考数据库?我的意思是“////////.mdf”之类的东西? – Alex 2010-11-04 19:15:33
和顺便说一句,在app.config我已经改变connectionString为“blabla”,它仍然工作(数据库连接)..是字符串rellevant的任何东西? – Alex 2010-11-04 19:24:37
@Badescu Alexandru,你可以把你的数据库放在可执行文件的后面,没有其他的路径,你也可以把它放在任何地方并为它设置系统路径,所以当它搜索路径时,它会找到它。 – 2010-11-04 19:41:42