如何定义SQL Server 2008数据库的连接字符串?

时间:2022-04-12 12:27:47

I'm using MS Visual Studio 2010 to create an application with SQL Server 2008 database access, but what I did to create the database was add a new "SQL Server 2008 Database Project", it added it, and shows me everything on my Solution Explorer, but how do I write the connection string to connect to it, because I wrote this one, and it didn't work.

我正在使用MS Visual Studio 2010创建一个具有SQL Server 2008数据库访问权限的应用程序,但是我创建数据库所做的是添加一个新的“SQL Server 2008数据库项目”,它添加了它,并向我展示了我的解决方案中的所有内容资源管理器,但是如何编写连接字符串以连接它,因为我写了这个,但它没有用。

SqlConnection cnTrupp = new SqlConnection("Initial Catalog = Database;Data Source = localhost;Persist Security Info=True;");

update:

I used this one:

我用过这个:

cnTrupp = new SqlConnection("database=DB_Trupp;server=.\\SQLExpress;Persist Security Info=True;integrated security=SSPI");

But when I use the cnTrupp.Open() it tells me that the login failed.

但是,当我使用cnTrupp.Open()时,它告诉我登录失败。

5 个解决方案

#1


11  

Check out the connection strings web site which has tons of example for your connection strings.

查看连接字符串网站,其中包含大量连接字符串示例。

Basically, you need three things:

基本上,你需要三件事:

  • name of the server you want to connect to (use "." or (local) or localhost for the local machine)
  • 要连接的服务器的名称(对本地计算机使用“。”或(本地)或localhost)

  • name of the database you want to connect to
  • 要连接的数据库的名称

  • some way of defining the security - either integrated Windows security, or define a user name / password combo
  • 某种定义安全性的方法 - 集成Windows安全性,或定义用户名/密码组合

For example, if you want to connect to your local machine and the AdventureWorks database using integrated security, use:

例如,如果要使用集成安全性连接到本地计算机和AdventureWorks数据库,请使用:

server=(local);database=AdventureWorks;integrated security=SSPI;

Or if you have SQL Server Express on your machine in the default installation, and you want to connect to the AdventureWorksLT2008 database, use this:

或者,如果您的计算机上有默认安装的SQL Server Express,并且您想要连接到AdventureWorksLT2008数据库,请使用以下命令:

server=.\SQLExpress;database=AdventureWorksLT2008;integrated Security=SSPI;

#2


3  

Standard Security
Data Source=serverName\instanceName;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

标准安全数据源= serverName \ instanceName;初始目录= myDataBase;用户ID = myUsername;密码= myPassword;

Trusted Connection
Data Source=serverName\instanceName;Initial Catalog=myDataBase;Integrated Security=SSPI;

可信连接数据源= serverName \ instanceName;初始目录= myDataBase;集成安全性= SSPI;

Here's a good reference on connection strings that I keep handy: ConnectionStrings.com

这里有一个很方便的连接字符串参考:ConnectionStrings.com

#3


0  

You need to specify how you'll authenticate with the database. If you want to use integrated security (this means using Windows authentication using your local or domain Windows account), add this to the connection string:

您需要指定如何使用数据库进行身份验证。如果要使用集成安全性(这意味着使用本地或域Windows帐户使用Windows身份验证),请将其添加到连接字符串:

Integrated Security = True;

If you want to use SQL Server authentication (meaning you specify a login and password rather than using a Windows account), add this:

如果要使用SQL Server身份验证(意味着您指定登录名和密码而不是使用Windows帐户),请添加以下内容:

User ID = "username"; Password = "password";

#4


0  

Copy/Paste what is below into your code:

将以下内容复制/粘贴到您的代码中:

SqlConnection cnTrupp = new SqlConnection("Initial Catalog = Database;Data Source = localhost;Persist Security Info=True;Integrated Security = True;");

Keep in mind that this solution uses your windows account to log in.

请记住,此解决方案使用您的Windows帐户登录。

As John and Adam have said, this has to do with how you are logging in (or not logging in). Look at the link John provided to get a better explanation.

正如John和Adam所说,这与您登录(或不登录)的方式有关。查看John提供的链接以获得更好的解释。

#5


0  

Instead of writing it in your code directly I suggest you make use of the dedicated <connectionStrings> element in the .config file and retrieve it from there.

我建议您不要直接在代码中编写它,而是使用.config文件中的专用 元素并从那里检索它。

Also make use of the using statement so that after usage your connection automatically gets closed and disposed of.

还可以使用using语句,以便在使用后您的连接自动关闭并处理掉。

A great reference for finding connection strings: connectionstrings.com/sql-server-2008.

查找连接字符串的绝佳参考:connectionstrings.com/sql-server-2008。

#1


11  

Check out the connection strings web site which has tons of example for your connection strings.

查看连接字符串网站,其中包含大量连接字符串示例。

Basically, you need three things:

基本上,你需要三件事:

  • name of the server you want to connect to (use "." or (local) or localhost for the local machine)
  • 要连接的服务器的名称(对本地计算机使用“。”或(本地)或localhost)

  • name of the database you want to connect to
  • 要连接的数据库的名称

  • some way of defining the security - either integrated Windows security, or define a user name / password combo
  • 某种定义安全性的方法 - 集成Windows安全性,或定义用户名/密码组合

For example, if you want to connect to your local machine and the AdventureWorks database using integrated security, use:

例如,如果要使用集成安全性连接到本地计算机和AdventureWorks数据库,请使用:

server=(local);database=AdventureWorks;integrated security=SSPI;

Or if you have SQL Server Express on your machine in the default installation, and you want to connect to the AdventureWorksLT2008 database, use this:

或者,如果您的计算机上有默认安装的SQL Server Express,并且您想要连接到AdventureWorksLT2008数据库,请使用以下命令:

server=.\SQLExpress;database=AdventureWorksLT2008;integrated Security=SSPI;

#2


3  

Standard Security
Data Source=serverName\instanceName;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

标准安全数据源= serverName \ instanceName;初始目录= myDataBase;用户ID = myUsername;密码= myPassword;

Trusted Connection
Data Source=serverName\instanceName;Initial Catalog=myDataBase;Integrated Security=SSPI;

可信连接数据源= serverName \ instanceName;初始目录= myDataBase;集成安全性= SSPI;

Here's a good reference on connection strings that I keep handy: ConnectionStrings.com

这里有一个很方便的连接字符串参考:ConnectionStrings.com

#3


0  

You need to specify how you'll authenticate with the database. If you want to use integrated security (this means using Windows authentication using your local or domain Windows account), add this to the connection string:

您需要指定如何使用数据库进行身份验证。如果要使用集成安全性(这意味着使用本地或域Windows帐户使用Windows身份验证),请将其添加到连接字符串:

Integrated Security = True;

If you want to use SQL Server authentication (meaning you specify a login and password rather than using a Windows account), add this:

如果要使用SQL Server身份验证(意味着您指定登录名和密码而不是使用Windows帐户),请添加以下内容:

User ID = "username"; Password = "password";

#4


0  

Copy/Paste what is below into your code:

将以下内容复制/粘贴到您的代码中:

SqlConnection cnTrupp = new SqlConnection("Initial Catalog = Database;Data Source = localhost;Persist Security Info=True;Integrated Security = True;");

Keep in mind that this solution uses your windows account to log in.

请记住,此解决方案使用您的Windows帐户登录。

As John and Adam have said, this has to do with how you are logging in (or not logging in). Look at the link John provided to get a better explanation.

正如John和Adam所说,这与您登录(或不登录)的方式有关。查看John提供的链接以获得更好的解释。

#5


0  

Instead of writing it in your code directly I suggest you make use of the dedicated <connectionStrings> element in the .config file and retrieve it from there.

我建议您不要直接在代码中编写它,而是使用.config文件中的专用 元素并从那里检索它。

Also make use of the using statement so that after usage your connection automatically gets closed and disposed of.

还可以使用using语句,以便在使用后您的连接自动关闭并处理掉。

A great reference for finding connection strings: connectionstrings.com/sql-server-2008.

查找连接字符串的绝佳参考:connectionstrings.com/sql-server-2008。