簡體   English   中英

從Visual Studio 2013連接到SQL Server 2008 R2數據庫

[英]Connecting to SQL Server 2008 R2 database from Visual Studio 2013

我知道這是一個經典的問題。 但是我受夠了。 我試圖在VS 2013中生成Windows窗體應用程序。對於數據庫,我使用SQL Server 2008 R2。 數據庫和應用程序在同一系統上。 我在我的app.config文件中使用以下連接字符串

<connectionStrings>
<add  name="Connectionstring1" 
      connectionString="Data Source=PC02;Initial Catalog=KCPIMSTest;
      User ID=sa;Password=***********;Integrated Security=true"  
      providerName="System.Data.SqlClient"/>
 </connectionStrings>

我通過將數據庫添加到VS 2013的服務器資源管理器並從屬性中獲取該連接字符串。 但是,在運行應用程序時,我在con.open();con.open();了異常con.open();

System.Data.dll中發生了類型為'System.Data.SqlClient.SqlException'的未處理的異常

附加信息:無法打開登錄請求的數據庫“ KCPIMSTest”。 登錄失敗。

代替Data Source=PC02 ,我已經嘗試了localhostsqlexpress等。

這些是附加代碼

  public void Save(string uname, string pwd)
  {
        string constring = getConnection();
        SqlConnection con = new SqlConnection(constring);

        if (con.State != ConnectionState.Open)
            con.Open();

        SqlCommand cmd = new SqlCommand("tblTest", con);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@UserName", uname);
        cmd.Parameters["@UserName"].Direction = ParameterDirection.Input;
        .....

        cmd.ExecuteNonQuery();
        con.Close();

        MessageBox.Show("Data Inserted Succesfully");
    }

    public static string getConnection()
    {
        /*Reading Connection string from App.config File */
        string constr = ConfigurationManager.ConnectionStrings["Connectionstring1"].ConnectionString;
        return constr;
    }

如果您使用的是SQL Server登錄名,並假設您具有正確的密碼,請從連接字符串中刪除Integrated Security=true

參考: http : //msdn.microsoft.com/zh-CN/library/ms254500(v=vs.80).aspx

PS使用的實踐using在可能的情況(即,實現類IDisposableSqlConnection )。

您應提供:

  • User IDPassword (使用SQL Server登錄名時)
  • 或設置Integrated Security=true (使用Windows登錄名時)。

請勿同時使用兩者。

Data Source=PC02;Initial Catalog=KCPIMSTest;
      User ID=sa;Password=***********

刪除Integrated Security 然后將您的password=*****更改為password=youroriginalpasswordtext然后您的登錄名將password=youroriginalpasswordtext

我認為您是從ServerExplorer復制了此connection string 默認情況下,密碼將被Masked 因此,您應該將掩碼更改為original Password本身。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM