使用web.config中设置我的SQL数据库连接字符串? [英] Using the Web.Config to set up my SQL database connection string?

查看:306
本文介绍了使用web.config中设置我的SQL数据库连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我吗?我很困惑。

Can anyone help me out please? I'm confused.

我想设置我的连接字符串,这样我就可以刚刚从我的web.config文件中调用它。

I want to set up my connection string so I can just call it from my Web.Config file.

我需要一种方法来从我的code称呼它,请做一个小例子。 (

I need a way to call it from my code, please make a little example. :(

我还需要有关设置Web.Config文件帮助。

I also need help on setting up the Web.Config file.

我不知道是什么性质使用。下面是我的凭据是什么的截图。我没有密码设置为Windows。我真的失去了这里。

I don't know what properties to use. Here's a screenshot of what my credentials are. I have no password set up for Windows. I'm really lost here.

推荐答案

下面是一个 MSDN上很好的概述,讨论了如何做到这一点。

Here's a great overview on MSDN that covers how to do this.

在你的web.config中,添加一个连接字符串项:

In your web.config, add a connection string entry:

<connectionStrings>
  <add 
    name="MyConnectionString" 
    connectionString="Data Source=sergio-desktop\sqlexpress;Initial 
    Catalog=MyDatabase;User ID=userName;Password=password"
    providerName="System.Data.SqlClient"
  />
</connectionStrings>

让我们在这里打破组成部分:

Let's break down the component parts here:

数据源是您的服务器。在你的情况,塞尔吉奥 - 桌面上一个名为SQL实例

Data Source is your server. In your case, a named SQL instance on sergio-desktop.

初始目录是默认的数据库查询应针对执行。对于正常使用,这将是数据库的名称。

Initial Catalog is the default database queries should be executed against. For normal uses, this will be the database name.

有关的认证,我们有几种选择。

For the authentication, we have a few options.

用户ID 密码意味着使用SQL凭据,而不是Windows,但还是很简单 - 只要进入你的SQL Server的安全部分,并创建一个新的登录。给它一个用户名和密码,并给它的权利到您的数据库。所有基本的对话都非常不言自明的。

User ID and Password means using SQL credentials, not Windows, but still very simple - just go into your Security section of your SQL Server and create a new Login. Give it a username and password, and give it rights to your database. All the basic dialogs are very self-explanatory.

您还可以使用集成的安全性,这意味着你的.NET应用程序将尝试使用工作进程的凭据连接到SQL。点击这里,查看更多的信息上的。

You can also use integrated security, which means your .NET application will try to connect to SQL using the credentials of the worker process. Check here for more info on that.

最后,在code,您可以使用到您的连接字符串:

Finally, in code, you can get to your connection string by using:

ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString

这篇关于使用web.config中设置我的SQL数据库连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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