简体   繁体   English

从Visual Studio 2013连接到SQL Server 2008 R2数据库

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

I know it is a classic problem. 我知道这是一个经典的问题。 But I am fed up. 但是我受够了。 I am trying to generate a Windows Forms application in VS 2013. For database I use SQL Server 2008 R2. 我试图在VS 2013中生成Windows窗体应用程序。对于数据库,我使用SQL Server 2008 R2。 The database and application are on the same system. 数据库和应用程序在同一系统上。 And I use the following connection string in my app.config file 我在我的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>

I got this connection string by adding the database to server explorer of VS 2013 and take it from properties. 我通过将数据库添加到VS 2013的服务器资源管理器并从属性中获取该连接字符串。 But while running the application I get an exception on con.open(); 但是,在运行应用程序时,我在con.open();con.open();了异常con.open(); :

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll System.Data.dll中发生了类型为'System.Data.SqlClient.SqlException'的未处理的异常

Additional information: Cannot open database "KCPIMSTest" requested by the login. 附加信息:无法打开登录请求的数据库“ KCPIMSTest”。 The login failed. 登录失败。

Instead of Data Source=PC02 , I already tried localhost , sqlexpress and all. 代替Data Source=PC02 ,我已经尝试了localhostsqlexpress等。

These are the additional codes 这些是附加代码

  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;
    }

If you're using a SQL server login, and assuming you have the correct password, remove Integrated Security=true from your connection string. 如果您使用的是SQL Server登录名,并假设您具有正确的密码,请从连接字符串中删除Integrated Security=true

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

PS Practice using using where possible (ie classes that implement IDisposable such as SqlConnection ). PS使用的实践using在可能的情况(即,实现类IDisposableSqlConnection )。

You should provide: 您应提供:

  • either User ID and Password (when you use SQL Server login) User IDPassword (使用SQL Server登录名时)
  • or set Integrated Security=true (when you use Windows login). 或设置Integrated Security=true (使用Windows登录名时)。

Don't use both at the same time. 请勿同时使用两者。

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

Remove Integrated Security . 删除Integrated Security Then change your password=***** to password=youroriginalpasswordtext Then your login will work and 然后将您的password=*****更改为password=youroriginalpasswordtext然后您的登录名将password=youroriginalpasswordtext

I think you copied this connection string from the ServerExplorer . 我认为您是从ServerExplorer复制了此connection string The passwords will be Masked by default. 默认情况下,密码将被Masked So you should change the Mask to original Password itself. 因此,您应该将掩码更改为original Password本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将SQL Server 2008 R2数据库添加到Visual Studio 2010安装文件中? - How to add my SQL Server 2008 R2 database to my Visual Studio 2010 Setup File? 错误948:带有SQL Server 2008 R2的Visual Studio 2012 - Error 948 :Visual Studio 2012 with SQL Server 2008 R2 在 Visual Studio 2008 中连接到 SQL Server 2012 数据库 - Connecting to a SQL Server 2012 database in Visual Studio 2008 使用 LINQ 将数据库从 SQL Server 2008 R2 更改为 SQL Server 2008 - Using LINQ changing database from SQL Server 2008 R2 to SQL Server 2008 在同一子网上连接到SQL Server 2008 R2 Express - Connecting to SQL Server 2008 R2 Express on same subnet EF代码首先连接到SQL Server 2008 R2 - EF Code First Connecting to SQL Server 2008 R2 从 Visual Studio 2013 数据库项目中清理 SQL Server 数据库 - Clean SQL Server database from Visual Studio 2013 database project 通过SQL Server Management Studio 2008连接数据库,但不通过Visual Studio 2012连接数据库 - connecting database through sql server management studio 2008 but not through visual studio 2012 删除数据库SQL Server 2008 R2 Express Edition-异常 - Drop Database SQL Server 2008 R2 Express Edition - Exception 如何在SQL Server 2008 R2数据库中创建临时表? - How to create temporary table in sql server 2008 R2 database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM