gpt4 book ai didi

c# - 使用 C# 连接到本地 SQL Server 数据库

转载 作者:可可西里 更新时间:2023-11-01 08:33:38 25 4
gpt4 key购买 nike

假设我在 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";

但是我得到一个错误:

database not found/error connecting to database

最佳答案

Data Source(在 Visual Studio 的左侧)中,右键单击数据库,然后选择 Configure Data Source With Wizard。会出现一个新的窗口,展开Connection string,你可以在里面找到连接字符串

关于c# - 使用 C# 连接到本地 SQL Server 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12220865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com