如何从数据库中获取连接字符串 [英] How to get the connection String from a database

查看:26
本文介绍了如何从数据库中获取连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 SQL Server Management Studio 创建了一个数据库,现在我想在我的 C# 应用程序中使用它.我需要连接字符串?

I have created a database with SQL Server Management Studio, I would like to now use it in my C# application. I need the connection string?

在哪里可以找到连接字符串,我的数据库存储在哪里?

Where can I find the connection string, and where is my database stored?

我是否必须发布它或类似的东西,还是在我的文档中的某个地方?

Do I have to publish it or something like that, or is it in my documents somewhere?

using (var conn = new SqlConnection("your connection string to the database"))

如何获取连接字符串?在哪里可以找到连接字符串复制粘贴到上面的部分?

How do I obtain the connection string? Where can I find the connection string to copy paste into the above section?

如何发布我的数据库以便 Visual Studio 可以获取它?那我可以直接拉那里的连接串吗?

How to I publish my database so that Visual Studio can pick it up? Then I can just pull the connection string of there?

推荐答案

获取连接字符串的最简单方法是使用 Visual Studio 中的服务器资源管理器"窗口(菜单 View服务器资源管理器)并从该窗口连接到服务器.

The easiest way to get the connection string is using the "Server Explorer" window in Visual Studio (menu View, Server Explorer) and connect to the server from that window.

然后就可以在连接的服务器的属性中看到连接字符串(选择连接后按F4或Alt+Enter或者右键菜单选择属性).

Then you can see the connection string in the properties of the connected server (choose the connection and press F4 or Alt+Enter or choose Properties on the right click menu).

高级连接字符串设置:在创建连接时,您可以通过单击高级..."添加连接"对话框底部的按钮.您可以稍后通过右键单击数据连接并选择修改连接..."来访问此对话框.可用的高级选项因服务器类型而异.

Advanced connection string settings: when creating the connection, you can modify any of the advanced connection string options, like MARS, resiliency, timeot, pooling configuration, etc. by clicking on the "Advanced..." button on the bottom of the "Add connection" dialog. You can access this dialog later by right clicking the Data Connection, and choosing "Modify connection...". The available advanced options vary by server type.

如果您使用 SQL Server Management Studio 创建数据库,该数据库将在服务器实例中创建,因此,要部署您的应用程序,您必须备份数据库并将其部署在部署 SQL Server 中.或者,您可以使用 SQL Server Express(SQL Server 2012 中的 localDB)使用数据文件,该文件将随您的应用轻松分发.

If you create the database using SQL Server Management Studio, the database will be created in a server instance, so that, to deploy your application you'll have to make a backup of the database and deploy it in the deployment SQL Server. Alternatively, you can use a data file using SQL Server Express (localDB in SQL Server 2012), that will be easily distributed with your app.

即如果是 ASP.NET 应用程序,则有一个 App_Datafolder.如果右键单击它,您可以添加一个新元素,它可以是 SQL Server 数据库.该文件将位于该文件夹中,可与 SQL Express 一起使用,并且易于部署.您需要在您的机器上安装 SQL Express/localDB 才能使其工作.

I.e. if it's an ASP.NET app, there's an App_Datafolder. If you right click it you can add a new element, which can be a SQL Server Database. This file will be on that folder, will work with SQL Express, and will be easy to deploy. You need SQL Express / localDB installed on your machine for this to work.

这篇关于如何从数据库中获取连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆