2012-11-19 129 views
2

其實我開發一個Windows窗體使用Visual C#速成2010年的數據從SQL Server 2008 Express的DBC#2010速成+ SQL Server 2008 Express的 - 連接 「登錄失敗」

應用程序,它會使用(讀/寫)我創建了我的數據庫與SQL Server Management Studio中(2008年快遞), 我明白實例被命名爲ATLELAG786576\SQLEXPRESS 我的數據庫被稱爲「TEST」

看着我的數據庫「TEST」屬性在SQL Server Management Studio中(2008年快遞): 在文件下,我是(ATLE\bneveux)DB的擁有者

安全,登錄Mylogin(ATLE \ bneveux)

  • 我的默認數據庫是 'TEST' 下尋找
  • 服務器角色是 '公' + '系統管理員'
  • 用戶映射DB 'TEST'用戶 'DBO' 默認模式 'DBO'

在我的C#應用​​程序

的app.config:

<?xml version="1.0" encoding="utf-8" ?> <configuration> 
    <configSections> 
    </configSections> 
    <connectionStrings> 
     <add name="connectionStringTestDb" 
      connectionString="Data Source=ATLELAG786576\SQLEXPRESS;Initial Catalog=D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf;Integrated Security=True;Connect Timeout=30;User Instance=False" 
      providerName="System.Data.SqlClient" /> 
    </connectionStrings> </configuration> 

dbConnection.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

namespace SQLServerConnectionDemo 
{ 
    class dbConnection 
    { 
     public static SqlConnection newCon; 
     public static string connectionStringTestDb = ConfigurationManager.ConnectionStrings["connectionStringTestDb"].ConnectionString; 

     public static SqlConnection GetConnection() 
     { 
      newCon = new SqlConnection(connectionStringTestDb); 
      return newCon; 
     } 
    } 
} 

dbAccess.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.SqlClient; 

namespace SQLServerConnectionDemo 
{ 
    class dbAccess 
    { 
     SqlConnection conn; 
     public dbAccess() 
     { 
      conn = dbConnection.GetConnection(); 
     } 

     //Method insert new in tblEmployees 
     public void addEmployee(string Id, string Name, string Email) 
     { 
      if (conn.State.ToString() == "Closed") 
      { 
       conn.Open(); 
      } 
      SqlCommand newCmd = conn.CreateCommand(); 
      newCmd.Connection = conn; 
      newCmd.CommandType = CommandType.Text; 
      newCmd.CommandText = "INSERT INTO tblEmployees VALUES ('"+ Id +"','"+ Name +"','"+ Email +"')"; 
      newCmd.ExecuteNonQuery(); 
     } 

    } 
} 

的形式formEmployeeAdd.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace SQLServerConnectionDemo 
{ 
    public partial class formEmployeeAdd : Form 
    { 

     dbAccess access = new dbAccess(); 

     public formEmployeeAdd() 
     { 
      InitializeComponent(); 
     } 

     private void btnInsert_Click(object sender, EventArgs e) 
     { 
      access.addEmployee(txtId.Text, txtName.Text, txtEmail.Text); 
      MessageBox.Show("Data successfully added"); 
     } 
    } 
} 

而且這裏的錯誤信息嘗試運行這個過程時,我總是得到:

System.Data.SqlClient.SqlException(0x80131904):無法打開數據庫「d:\ Microsoft SQL Server的\ MSSQL10.SQLEXPRESS \ MSSQL \ DATA \ TEST.mdf「。登錄失敗。 用戶'ATLE \ bneveux'登錄失敗。

注意,我從來沒有真正能夠從VS加我的數據源在Visual C#2010 Express,以使我可以管理數據庫,我總是得到以下錯誤消息:

無法打開物理文件「D:\ Microsoft SQL Server \ MSSQL10.SQLEXPRESS \ MSSQL \ DATA \ TEST.mdf」。操作系統錯誤32:「32(Le processus ne peut pasaccéderau fichier car ce fichier estutilisépar par un autre processus。)」。 嘗試附加文件D:\ Microsoft SQL Server \ MSSQL10.SQLEXPRESS \ MSSQL \ DATA \ TEST.mdf的自動命名數據庫失敗。具有相同名稱的數據庫存在,或指定的文件無法打開,或位於UNC共享上。

回答

1

嘗試用簡單的

Initial Catalog=TEST 
+0

jeroenh嗨更換

Initial Catalog=D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf 

,只是想你的命題,它的工作和解決我的問題!感謝您的快速答覆! (我該如何標記此答案成功?) – Brice

+0

很好用。這個問題應該留下一個「接受」的標記。您也可以使用向上箭頭向上投票。 – jeroenh

+0

jeroenh, 我可以濫用並詢問您如何解決使用Visual C#2010 Express瀏覽/管理數據庫的可能性; 當我去 數據源>新建>數據庫>數據集>新連接 我選擇「數據源」Microsoft SQL Server文件...(抱歉如果翻譯不正確我用法語版) 然後瀏覽到我的TEST.mdf文件 當我點擊「測試連接」時,我現在收到以下錯誤消息「無法打開用戶默認數據庫,登錄失敗,用戶'ATLE \ bneveux'登錄失敗。」 謝謝! Brice – Brice

相關問題