gpt4 book ai didi

c# - 从 Visual Studio 连接到 SQL Server Express

转载 作者:行者123 更新时间:2023-11-30 14:33:54 30 4
gpt4 key购买 nike

我安装了带有 Visual Studio 2010 的 SQL Server 2008,我下载了一个项目,其中包含在 Visual Studio 项目本身中创建的数据库文件,现在我想连接到该数据库。

我尝试更改连接字符串但无法连接到数据库。

谁能告诉我如何连接到数据库,我的机器上安装了 SQL Server 2008 (10.0.1600.22)

更新的连接字符串:

这是我使用的连接字符串

Data Source=SQLEXPRESS\;Initial Catalog=INVENTORY;uid=xxx;pwd=xxx

其中 xxx\xxx 分别是我的机器和我安装的 SQL Server 实例名称。

最佳答案

您使用的是 C# 吗?

试试这个:

using namespace System.Data.SqlClient;

SqlConnection con = new SqlConnection("Data source = [ip]; Initial catalog = [db name]; User id = [user name]; password = [password]");

con.Open();

在con.Open()处设置断点,如果成功并通过这一行,则表示连接成功。

之后,你可能想执行一个 SQL 命令试试这个:

    SqlCommand cmd = new SqlCommand("[command]", con);

// if the command has no return value
cmd.ExecuteNonQuery();
//else you might want a Sql data reader to read the return value
SqlDataReader read = cmd.ExecuteReader();
while(read.Read())
{
//do something
}

关于c# - 从 Visual Studio 连接到 SQL Server Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15127073/

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