SQL Server连接问题

时间:2010-11-04 18:44:26

标签: c# sql-server

我在我的C#app中添加了一个外部数据源(我把它放在名为'data'的文件夹的根目录下)但是当我运行程序时,我在con.Open()收到错误:

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();

2 个答案:

答案 0 :(得分:1)

我认为您遇到与此问题相同的问题:connect to .mdf file in vs2010 , wpf application 我在那里回答,但这只是我的猜测。因为这是通常的问题。

答案 1 :(得分:0)

运行应用程序的帐户是否可以访问数据文件夹?