2012-04-19 59 views
1
SqlConnection con = new SqlConnection(@"Server=.\SQLEXPRESS;AttachDbFilename='C:\HashTags.mdf';Integrated Security=True;User Instance=True"); 

con.Open(); 
String queryStr = "SELECT name FROM ttable WHERE name LIKE '*%'"; 
SqlCommand com = new SqlCommand(queryStr, con); 
SqlDataReader sdr = com.ExecuteReader(); 

while (sdr.Read()) 
{ 
    this.trendingBx.Text = sdr.GetValue(0).ToString(); 
} 

sdr.Close(); 

誰能告訴我,爲什麼我得到這個錯誤:C#數據庫錯誤

A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

回答

1

您確定數據庫尚未附加?如果是你應該使用:

Data Source=.\SQLEXPRESS;Database=your database name;Integrated Security=SSPI 
+0

我將如何添加到我的代碼?只是在括號內? – user1300788 2012-04-19 20:00:18

+0

SqlConnection con = new SqlConnection(@「Server =。\ SQLEXPRESS; Database = HashTags; Integrated Security = sspi」); – richk 2012-04-19 21:05:43

0

假設「指定的文件無法打開,或它位於UNC共享」你已經檢查&的.mdf在該位置確實存在,我認爲你不需要有連接字符串中引號:

AttachDbFilename='C:\HashTags.mdf' 

應該是:

AttachDbFilename=C:\HashTags.mdf (no single quotes) 

這應該工作IMO。

0

你應該使用:

Integrated Security=SSPI 

Integrated Security=True 

您的連接的字符串中。