Unity3d connection to SQL Server database socketexception: An address error is used that is incompatible with the requested protocol

Source: Internet
Author: User



These two days, the classmate asked me unity3d connection SQL Server problem, at that time I simply said: "Should be the same, is that simple sentence ah." After he asked me to try it, I found that there was a problem. So write down a blog, to keep in mind the lessons of this matter, the operation of the database, we all will, however, we need to know: some things, not we will have the ability to do, we must pay attention to the details, to be able to do our job as a programmer (although I am a student).



Let's take a look at the connection database first:



1, introduce the Assembly System.Data.dll, position in C:\Program files\unity\editor\data\mono\lib\mono\unity (default location), Copy the System.Data.dll file to the current Unity project file, which is now available under the Assets folder.



2, the Assembly Sysem.Data.dll with VS2015 manual introduction can.


 
 
using UnityEngine;
using System.Collections;
using System.Data.SqlClient;
using System;

public class Testaa : MonoBehaviour
{

    // Use this for initialization
    SqlConnection con = new SqlConnection("Data Source=主机名;Initial Catalog=db_CSharp;Persist Security Info=True;User ID=sa;Password=密码");

    void Start()
    {
        Do();
    }
    void Do()
    {
        con.Open();
        SqlCommand com = new SqlCommand("select * from tb_PDic", con);
        SqlDataReader sdr = null;
        try
        {
            sdr = com.ExecuteReader();
            print("名称\t\t\t\t价格\n");
            while (sdr.Read())
            {
                print(sdr["Name"] + "\t\t\t\t" + sdr["Money"] + "\n");
            }
            con.Close();
        }
        catch (Exception ex)
        {
            print(ex.Message);
        }
    }
    // Update is called once per frame
    void Update()
    {
    }
}





Run after saving. The following error occurred:



SocketException: An address that is incompatible with the requested protocol is used.


System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteep, Boolean requiresocketpolicy)
System.net.sockets.socket+worker.connect ()
Rethrow as Tdsinternalexception:server does not exist or connection refused.
Mono.Data.Tds.Protocol.TdsComm. ctor (System.String DataSource, Int32 port, Int32 packetsize, Int32 timeout, tdsversion tdsversion)
Mono.Data.Tds.Protocol.Tds. ctor (System.String DataSource, Int32 port, Int32 packetsize, Int32 timeout, tdsversion tdsversion)
Mono.Data.Tds.Protocol.Tds70. ctor (System.String server, Int32 port, Int32 packetsize, Int32 timeout, tdsversion version)
Mono.Data.Tds.Protocol.Tds80. ctor (System.String server, Int32 port, Int32 packetsize, Int32 timeout)
Mono.Data.Tds.Protocol.TdsConnectionPoolManager.CreateConnection (Mono.Data.Tds.Protocol.TdsConnectionInfo info)
Mono.Data.Tds.Protocol.TdsConnectionPool.GetConnection ()
System.Data.SqlClient.SqlConnection.Open ()
Rethrow as Sqlexception:server does not exist or connection refused.
System.Data.SqlClient.SqlConnection.Open ()
(Wrapper Remoting-invoke-with-check) System.Data.SqlClient.SqlConnection:Open ()
Testaa.do () (at assets/testaa.cs:17)
Testaa.start () (at assets/testaa.cs:13)





WORKAROUND: Change the host name to the host's IP address.



Unity3d connection to SQL Server database socketexception: An address error is used that is incompatible with the requested protocol


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.