2016-09-16 100 views
0

我目前正在編寫代碼以將信息保存到數據庫。 我想我現在正在接近最後一道工作的障礙。數據庫無法打開 - 用戶登錄失敗

我的問題是,當我點擊保存,我收到一個消息框說,無法打開登錄請求的數據庫[數據庫文件路徑]。登錄失敗。登錄失敗的'User-PC \ User'。

但是,我沒有爲將來的數據庫創建任何登錄詳細信息,我希望使用該程序的任何人都能夠將信息記錄到其中。

我不太清楚在哪裏可以解決問題,但我目前正在下載SSMS以查看我是否可以在那裏修復它?

任何幫助將是偉大的,謝謝!

我的代碼,

串constring =「數據源=(的LocalDB)\ MSSQLLocalDB;初始目錄= C:\用戶\用戶\文件\視覺工作室2015 \項目\ LossApplication \ LossApplication \ LossDB.mdf ;集成安全性=是; Trusted_Connection = True;「; (@lossid,@equipment,@Cause,@reason,@start);「;}插入LossDB.LossTable(lossid,Equipment,Event,responsinility,start)值(0127)。

  SqlConnection conLossDB = new SqlConnection(constring); 
      SqlCommand cmdLossDB = new SqlCommand(query, conLossDB); 
      cmdLossDB.Parameters.AddWithValue("@lossid", textBox1.Text); 
      cmdLossDB.Parameters.AddWithValue("@Equipment", comboBox1.Text); 
      cmdLossDB.Parameters.AddWithValue("@Cause", comboBox2.Text); 
      cmdLossDB.Parameters.AddWithValue("@Reason", comboBox3.Text); 
      cmdLossDB.Parameters.AddWithValue("@start", dateTimePicker1.Text); 
      //Defines which boxes to read in order to input the text from the defined boxes into the corresponding columns 

      SqlDataReader myReader; 
      try 
      { 
       conLossDB.Open(); 
       myReader = cmdLossDB.ExecuteReader(); 
       MessageBox.Show("Loss Entry Saved"); 
       //Opens the database and carries out the defined command outlined in the code above 

       while (myReader.Read()) 
       { 

       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
+0

嘗試更改爲「Integrated Security = True」。或「集成安全性= SSPI」。 – Venky

+0

我已經試過並且兩次都得到相同的錯誤框... – Loplac92

+0

DB的權限是什麼?服務器上存在哪些登錄名以及這些登錄名對數據庫的訪問級別?如果你想允許「Everyone」通過你的應用程序寫入你的數據庫,你可能需要考慮設置一個你的應用程序用來驗證的SQL賬號,​​然後將當前用戶的信息存儲在數據庫本身中(比如審計跟蹤),而不是直接使用它對數據庫進行身份驗證。 – mikey

回答

0

使用Integrated Security意味着在SQL Server的Windows身份驗證模式,必須有能夠訪問數據庫中的一個有效的Windows用戶。如果要使用此模式併爲計算機上的所有用戶提供訪問權限,則可以配置Windows Users組(可向其所有用戶通常屬於的組)訪問數據庫(請參閱數據庫配置中的安全性 - >登錄)。

另一種方法是創建一個SQL用戶和用戶SQL Server身份驗證模式。然後,使用此用戶登錄名和密碼訪問數據庫,而不管Windows用戶如何。在這種情況下,您的連接字符串將看起來像

(LocalDB)\MSSQLLocalDB;Initial Catalog=<path>\LossDB.mdf;User ID=<UserLogin>;Password=<***>;Trusted_Connection=True; 
相關問題