SQL Server c# connection

时光总嘲笑我的痴心妄想 提交于 2019-12-13 03:58:55

问题


So Im running a Sql Server 2012 in a virtual machine on my computer and I want to be able to connect on it with the SqlConnection on Visual Studio, but I have no idea what to put on the servername parameter.

I dont know if I have to put the vm ipaddress or the machine address "WIN-NIJ53etc\SQLEXPRESS"


回答1:


UPDATED:

FOR MIGRATING BETWEEN SQL CE AND SQL SERVER: Please take a look at this instruction here.

You should follow these steps:

1- Set the virtual machine to have a physical IP address.

For configuring network on Microsoft Virtual PC: http://support.microsoft.com/kb/833134

2- Be sure that you can see the VM with a static IP address. (ping is a good idea for checking that, but don't forget to turn off the firewall on the target machine, I mean the machine you wanna ping. There is no need for the server to see the client, so check that your client (in this case your VM) can ping the server IP address like "ping 192.168.10.1 -t")

3- Use Configuration Manager (All Programs\Microsoft SQL Server\Configuration Tools) and set SQL port and visibility over network.

Reference: http://msdn.microsoft.com/en-us/library/ms189083(v=sql.105).aspx

4- Restart the SQL server.

5- Be sure that "SQL Server Browser" service is started. (incase you wanna use the instance name to access the SQL server)

6- Config the firewall on the host machine (machine that has SQL engine) and add the port you specified in the Configuration Manager to TCP Inbound rules.

Take a look at here: http://www.tavislovell.com/post/How-to-configure-Windows-Firewall-to-allow-access-to-SQL-Server.aspx

BUT be advised that you should set the port specified in the Configuration Manager (default is 1433 but sometimes it's dynamic and you should set it again)

7- After you set the port and network visibility, you should change the "Server" value in the connection string to this format "IPAddress, PORT" like "192.168.10.50, 1433" (When you specify the port, there is no need to set the instance name like this "192.168.10.50\SQLEXPRESS")

Hope it helps




回答2:


Ahh the joys of SQL Server Installation setup.

Make sure you have:

  • Network communication between the VM and the client computer (ping will work)
  • The SQL Server Service configured to start automatically or that it is started
  • The service configured to use TCP (if you don't want to use Named Pipes)
  • Either the SQL Server Browser enabled and started or that a static port is configured (1433 or similar)
  • The firewall rules configured to allow 1433 (or the port you have chosen) and 1434 (if you are using browser)
  • The connection string using the proper format Computer Name\Instance (to use browser) or Computer Name,port (if you are using a static port

There is a good tutorial on MSDN that may prove helpful. With specific regard to the Server Name/IP question, it doesn't matter so long as the client computer can resolve it. If you are testing, "IP,Port" is the most reliable (E.g. 10.10.10.100,1433) as it does not depend on NetBIOS, DNS nor the SQL Browser service.



来源:https://stackoverflow.com/questions/13441986/sql-server-c-sharp-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!