从Visual Studio连接到SQL Server Express

Abb*_*bas 3 c# database-connectivity visual-studio-2010 sql-server-express sql-server-2008

我已经在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
Run Code Online (Sandbox Code Playgroud)

xxx\xxx我的机器和安装的SQL Server实例名称分别在哪里。

Use*_*384 5

您在使用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();
Run Code Online (Sandbox Code Playgroud)

如果成功并通过了此行,则在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
    }
Run Code Online (Sandbox Code Playgroud)