简体   繁体   English

在联网服务器上的C#.net中连接SQL SERVER

[英]Connecting SQL SERVER in C#.net on networked server

I have an existing application build in C#.net, using SQL SERVER 2005, earlier application and SQL was on same machine, but now I need to connect SQL which is on server and is networked to the machine on which application is installed. 我在C#.net中使用SQL SERVER 2005建立了一个现有的应用程序,较早的应用程序和SQL位于同一台计算机上,但是现在我需要连接服务器上的SQL并联网到安装了该应用程序的计算机上。 So can any one tell me how to connect SQL to application , and what changes need to be made in connection string. 因此,谁能告诉我如何将SQL连接到应用程序,以及需要对连接字符串进行哪些更改。

Thanks EDIT : I am a bit new to c#.net. 谢谢编辑:我对c#.net有点陌生。 currently my connection string is : public SqlConnection con = new sqlConnection("server=.;database=Database1;integrated security=sspi); so if I just put this will it directly connect to the SQL which is on other computer ? 当前我的连接字符串是:public SqlConnection con = new sqlConnection(“ server = .; database = Database1; integrated security = sspi);因此,如果我只输入它,它将直接连接到另一台计算机上的SQL吗?

The answer depends on whether you are using SQL Server authentication 答案取决于您是否正在使用SQL Server身份验证

Standard Security (SQL Server authentication) 标准安全性 (SQL Server身份验证)

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword; Server = myServerAddress; Database = myDataBase; User Id = myUsername; Password = myPassword;

or to connect to a specific instance on the server 或连接到服务器上的特定实例

Server=myServerName\\myInstanceName;Database=myDataBase;User Id=myUsername; Server = myServerName \\ myInstanceName;数据库= myDataBase;用户ID = myUsername; Password=myPassword; 密码= myPassword;

Trusted Connection 可信连接

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True; Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;

http://connectionstrings.com/sql-server-2005 http://connectionstrings.com/sql-server-2005

Server=.; 服务器=。

Means you were connecting to a default instance of sql2005 on the same machine. 表示您正在连接到同一台计算机上的sql2005的默认实例。

Since 2005 SQL Server has allowed instances. 从2005年开始,SQL Server允许使用实例。

So you could have MyMachine/MyLiveDBMSInstance and MyMachine/MyTestDBMSInstance. 因此,您可以拥有MyMachine / MyLiveDBMSInstance和MyMachine / MyTestDBMSInstance。

Basically as though you'd installed sql server twice. 基本上,就好像你安装了SQL Server的两倍。 Default (ie unnamed) instances were a backwards compatibility measure, so DBAs didn't have to go around explaining what an instance was to mere programmers. 默认(即未命名)实例是向后兼容的措施,因此DBA不必四处为程序员解释什么是实例。 :) :)

You would have connected to them with Server = MyMachine\\MyLiveDBMSInstance or perhaps Server = .\\MyInstance dot being this localhost basically. 您将使用Server = MyMachine \\ MyLiveDBMSInstance或可能是Server =。\\ MyInstance点基本上是此本地主机来连接到它们。

So you can't use . 所以你不能使用。 any more, you need tthe name of the machine, and if it's using a named instance, you'll need that as well. 再者,您还需要计算机的名称,如果它使用的是命名实例,则也需要它。

ie Server = RemoteMachine or Server = RemoteMachine\\InstanceName 即Server = RemoteMachine或Server = RemoteMachine \\ InstanceName

Course all this assumes the machine with the dbms is on is set up correctly and the windows user running the application has been allowed access. 当然,所有这一切都假定已正确设置了具有dbms的计算机,并且已允许运行该应用程序的Windows用户访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM