从用C#编写的客户端连接到SQL [英] connect to SQL from a client written in C#

查看:69
本文介绍了从用C#编写的客户端连接到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您已经在PC上安装了SQL Server Express 2008 R2,并且想要编写一个程序(以C#语言编写),该程序能够连接(进行查询)到上述数据库引擎托管的数据库,但是该程序将在另一台机器;所有机器都在同一网络中(甚至是同一工作组-假设您家中有一个网络)

您想使用实体数据模型框架来简化生活.

您如何解决该问题?如何使程序能够识别另一台计算机上的SQL Server?您如何连接到数据库?

Suppose you have installed SQL Server Express 2008 R2 on a PC and you want to write a program(in C#) able to connect(make queries) to a database hosted by the mentioned database engine, but the program will be running on another machine; all machines are in the same network(even the same workgroup - Assume a network in your home)

You want to use Entity Data Model Framework to make your life easier.

How can you solve the problem? How do you make the program able to recognize the SQL Server on another machine? How do you connect to the database?

推荐答案


只需添加带有连接字符串的web.config connectionstring部分,并将数据源设置为实例的有效名称

Hi,
just add to web.config connectionstring section with connection string and and set data source to valid name of instance

<connectionStrings>
   <add

      name="LocalSqlServer"

      connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"

      providerName="System.Data.SqlClient"

   />
</connectionStrings>



http://msdn.microsoft.com/en-us/library/bf7sd233.asp [ ^ ]

但不要忘记设置与数据库一起使用的所有对象的所有连接字符串属性.

问候
罗伯特



http://msdn.microsoft.com/en-us/library/bf7sd233.asp[^]

but don''t forget to set all connection string properties of all object working with database.

Regards
Robert


这篇关于从用C#编写的客户端连接到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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