【问题标题】:Connecting to local SQL Server database using C#使用 C# 连接到本地 SQL Server 数据库
【发布时间】:2021-08-25 17:46:26
【问题描述】:

假设我在 Visual Studio 的 App_Data 文件夹中创建了一个名为 Database1.mdf 的 SQL Server 数据库,其中包含一个名为 Names 的表。

如何建立连接以使用 C# 读取表值?

到目前为止,我已经尝试过这样的事情:

SqlConnection conn = new SqlConnection("Server=localhost;Database=Database1;");

conn.Open();

// create a SqlCommand object for this connection
SqlCommand command = conn.CreateCommand();
command.CommandText = "Select * from Names";

但我得到一个错误:

找不到数据库/连接数据库时出错

【问题讨论】:

  • 您是否将数据库附加到 SQL Server?

标签: c# sql-server


【解决方案1】:

Data Source(Visual Studio 左侧)中右键单击数据库,然后单击Configure Data Source With Wizard。将出现一个新窗口,展开连接字符串,您可以在其中找到连接字符串

【讨论】:

  • 显示数据源窗口:在菜单栏上,选择查看、其他窗口、数据源(或选择 Shift+Alt+D 键)。
【解决方案2】:

如果您使用 SQL 身份验证,请使用:

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "User Id=UserName;" + 
     "Password=Secret;" + 
     "AttachDbFilename=|DataDirectory|Database1.mdf;"
conn.Open();

如果您使用 Windows 身份验证,请使用:

using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "Integrated Security=true;" + 
     "AttachDbFilename=|DataDirectory|Database1.mdf;"
conn.Open();

【讨论】:

  • 如果您不使用命名实例,那么只需使用像这样的点"Data Source=."
  • 我将它用于 SQLEXPRESS connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" 来自 MSDN 这里 msdn.microsoft.com/en-us/library/…
【解决方案3】:

如果您使用的是 SQL Server express,请更改

SqlConnection conn = new SqlConnection("Server=localhost;" 
       + "Database=Database1;");

SqlConnection conn = new SqlConnection("Server=localhost\SQLExpress;" 
       + "Database=Database1;");

还有数百个连接字符串可以在http://www.connectionstrings.com/找到

【讨论】:

    【解决方案4】:
    SqlConnection c = new SqlConnection(@"Data Source=localhost; 
                               Initial Catalog=Northwind; Integrated Security=True");
    

    【讨论】:

      【解决方案5】:

      你试试这个字符串连接

      Server=.\SQLExpress;AttachDbFilename=|DataDirectory|Database1.mdf;Database=dbname; Trusted_Connection=Yes;
      

      【讨论】:

        【解决方案6】:

        我喜欢使用here 概述的便捷过程来使用 .udl 文件构建连接字符串。这允许您从 udl 文件中测试它们,以确保您可以在运行任何代码之前进行连接。

        希望对您有所帮助。

        【讨论】:

          【解决方案7】:

          Visual Studio 2019(可能还有几个以前的版本)。

          • 查看 -> SQL Server 对象资源管理器
          • 树的顶部是“SQL Server”
          • 在“SQL Server”下,有几个“(localdb)....”
          • 展开 (localdb)... -> 数据库 直到找到您的数据库。
          • 数据库名称(例如 Database1) -> 右键单击​​ -> 属性,然后滚动许多属性(例如。“ANSI NULL Default")。找到“连接字符串”属性,复制值 进入你的代码,你正在运行。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-02-04
            • 1970-01-01
            • 1970-01-01
            • 2012-01-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多