简体   繁体   English

使用C#连接本地SQL服务器数据库

[英]Connecting to local SQL Server database using C#

Suppose I have created a SQL Server database called Database1.mdf in the App_Data folder in Visual Studio with a table called Names .假设我在 Visual Studio 的App_Data文件夹中创建了一个名为Database1.mdf的 SQL 服务器数据库,其中包含一个名为Names的表。

How could I establish a connection to read the table values using C#?我如何建立连接以使用 C# 读取表值?

So far I've tried something like this:到目前为止,我已经尝试过这样的事情:

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

But I get an error:但我得到一个错误:

database not found/error connecting to database未找到数据库/连接到数据库时出错

In Data Source (on the left of Visual Studio) right click on the database, then Configure Data Source With Wizard . Data Source (Visual Studio左侧)中右键单击数据库,然后单击“ Configure Data Source With Wizard A new window will appear, expand the Connection string, you can find the connection string in there 将出现一个新窗口,展开Connection字符串,您可以在其中找到连接字符串

If you use SQL authentication , use this: 如果使用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();

If you use Windows authentication , use this: 如果使用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();
SqlConnection c = new SqlConnection(@"Data Source=localhost; 
                           Initial Catalog=Northwind; Integrated Security=True");

If you're using SQL Server express, change 如果您使用的是SQL Server Express,请进行更改

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

to

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

That, and hundreds more connection strings can be found at http://www.connectionstrings.com/ 可以在http://www.connectionstrings.com/找到数百个连接字符串

您尝试使用此字符串连接

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

I like to use the handy process outlined here to build connection strings using a .udl file. 我喜欢使用这里概述的方便过程来使用.udl文件构建连接字符串。 This allows you to test them from within the udl file to ensure that you can connect before you run any code. 这允许您从udl文件中测试它们,以确保在运行任何代码之前可以连接。

Hope that helps. 希望有所帮助。

Visual Studio 2019 (and probably a few previous versions). Visual Studio 2019(可能还有几个以前的版本)。

  • View -> SQL Server Object Explorer查看 -> SQL 服务器 Object 资源管理器
  • Top of the tree is 'SQL Server'树的顶部是“SQL Server”
  • Under 'SQL Server', are couple of '(localdb)....'在“SQL Server”下,有几个“(localdb)....”
  • Expand the (localdb)... -> Databases until you find your db.展开(localdb)... -> 数据库,直到找到您的数据库。
  • Database Name (eg. Database1) -> Right-click -> Properties, and scroll the many properties (eg. "ANSI NULL Default").数据库名称(例如 Database1) -> 右键单击 -> 属性,然后滚动许多属性(例如“ANSI NULL Default”)。 Find the "Connection string" property, copy the value into your code, and you're running.找到“连接字符串”属性,将值复制到代码中,即可运行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM