如何定义一个连接字符串到SQL Server 2008数据库? [英] How to define a connection string to a SQL Server 2008 database?
问题描述
我使用微软的Visual Studio 2010中创建与SQL Server 2008数据库访问的应用程序,但我做了什么来创建数据库是增加一个新的SQL Server 2008数据库项目,它增加了它,并显示出我的一切我的解决方案资源管理,但我怎么写连接字符串连接到它,因为我写了这个,也没有工作。
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.
SqlConnection cnTrupp = new SqlConnection("Initial Catalog = Database;Data Source = localhost;Persist Security Info=True;");
更新:
update:
我用这一个
cnTrupp = new SqlConnection("database=DB_Trupp;server=.\\SQLExpress;Persist Security Info=True;integrated security=SSPI");
但是,当我使用 cnTrupp.Open()$ C $ 。C>它告诉我,登录失败
推荐答案
检查出的连接字符串网站拥有万吨例如,为您的连接字符串
Check out the connection strings web site which has tons of example for your connection strings.
基本上,你需要三样东西:
Basically, you need three things:
- 您要连接(使用的服务器的名称
。
或(本地)
或本地主机
本地计算机) - 您想要连接到数据库的名称
- 定义安全的某种方式 - 无论是集成的Windows的安全性,或者定义的用户名/密码组合
- name of the server you want to connect to (use "
.
" or(local)
orlocalhost
for the local machine) - 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
例如,如果要使用集成的安全性,使用连接到你的本地计算机和的AdventureWorks
数据库:
For example, if you want to connect to your local machine and the AdventureWorks
database using integrated security, use:
server=(local);database=AdventureWorks;integrated security=SSPI;
或者,如果你有SQL Server Express的计算机上的默认安装,并要连接到在AdventureWorksLT2008数据库,使用:
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:
server=.\SQLExpress;database=AdventureWorksLT2008;integrated Security=SSPI;
这篇关于如何定义一个连接字符串到SQL Server 2008数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!