简体   繁体   English

从Visual Studio连接到SQL Server Express

[英]Connecting to SQL Server Express from Visual Studio

I have SQL Server 2008 installed with Visual Studio 2010, I have downloaded a project which has database files created in Visual Studio project itself, now I want to connect to that database. 我在Visual Studio 2010中安装了SQL Server 2008,我下载了一个项目,其中包含在Visual Studio项目中创建的数据库文件,现在我想连接到该数据库。

I tried changing the connection string but unable to connect to the database. 我尝试更改连接字符串但无法连接到数据库。

Can anyone tell me how to connect to the database, I have SQL Server 2008 (10.0.1600.22) installed on my machine 任何人都可以告诉我如何连接到数据库,我在我的机器上安装了SQL Server 2008(10.0.1600.22)

Updated connection string : 更新连接字符串

Here is the connection string I am using 这是我正在使用的连接字符串

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

where xxx\\xxx is my machine and my installed SQL Server instance name respectively. 其中xxx\\xxx是我的机器和我安装的SQL Server实例名称。

Are you using C#? 你在用C#吗?

try this: 试试这个:

using namespace System.Data.SqlClient;

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

con.Open();

set a breakpoint at con.Open(), if it succeeds and passed this line, that means you have successfully connected to the database. 在con.Open()处设置断点,如果成功并传递此行,则表示您已成功连接到数据库。

After that, you may want to execute a SQL Command try this: 之后,您可能想要执行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
    }

To connect to a database file created in SQL Server, open the web.config file in visual studio and do the following: 要连接到在SQL Server中创建的数据库文件,请在visual studio中打开web.config文件并执行以下操作:

  1. Open a <connectionString> tag inside <configuration> tag. <configuration>标记内打开<connectionString> <configuration>标记。
  2. Paste the following in between the <connectionString> tags. 将以下内容粘贴到<connectionString>标记之间。

 <add name="NameofConnectionString" connectionString="server=YourServerInstanceName; database=DatabaseName; integrated security=SSPI" providerName="System.Data.SqlClient"/> 

  1. Refresh the server explorer in visual studio to get connected to the database 刷新vi​​sual studio中的服务器资源管理器以连接到数据库

Hope this helps! 希望这可以帮助!

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

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