Unity connects to the SQLServer database and reports an error that System.Data.dll cannot be found

Recently, I want to collect some data. It turned out that there is no problem with reading MySQL. Now it is changed to SQLServer as the database, but it has been reporting an error that the System.Data.dll file cannot be found. Record the solution here for future avoidance ( PS: I always thought that there was a problem with the dll file I was looking for. I tried many versions to no avail. I was too tired and I didn’t want to look for them one by one.)

error message

Let me release my error message first, and System.Data.dll will go wrong if I put it in or not (it’s a big deal!)
1. I didn’t put System.Data.dll in the project. VS Code reported an error, obviously I couldn’t find the class
error message 1
2. I didn’t Put System.Data.dll into the unity error message in the project
insert image description here

3. The error message of putting System.Data.dll into the project
error message 1

Error solution in Unity (Unity2018 can be used later, the previous version should still need to be copied and put into the project)

Before Unity2018, you need to copy System.Data.dll to the unity project. After Unity2018, you don’t need this operation. You only need to set the Api Compatibility Level* to .Net 4.x in the settings (vomit, Who knew it was so simple, let’s talk about whether the articles on the Internet can keep pace with the times, I searched a lot before I found one), the Unity problem was solved.
Unity settings

VS error resolution

Find Tools -> NuGet Package Manager -> NuGet package management solution, search for sql in the pop-up window, find System.Data.SqlClient, and then select all to install (I have already installed it here, so I can't install it again ).
insert image description here
Remember to connect to the Internet to download. After everything is installed, you need to restart VS, then no error will be reported, and the VS problem will be solved.

I started coding right after get off work, everyone who came here, if you find it useful, please bookmark and follow (I haven’t written in a year, I still need to write more and write more in the future, well, it’s another day full of harvest, continue diving, haha~~)

By the way, attach the VS detailed code for your reference

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

public class SqlServerUtils:MonoBehaviour
{
    
    
    //指定ip和数据库账号密码
    private const string ipAddress = "server=127.0.0.1;database=TestDB;uid=sa;pwd=admin123";
    //定义连接
    private SqlConnection sqlCon;
    //适配器
    SqlDataAdapter sda = null;

    void Start()
    {
    
    
        ConnectSQLServer();
    }

    public void ConnectSQLServer()
    {
    
    
        try
        {
    
    
            Debug.Log("开始连接SqlServer!");
            //创建一个数据库连接
            sqlCon = new SqlConnection(ipAddress);
            //打开连接
            sqlCon.Open();
            Debug.Log("SqlServer连接成功!");
            //数据库操作语句  
            //注意数据库名字要用中括号括起来
            string sql = "select * from [user]";
            //数据库操作
            sda = new SqlDataAdapter(sql, ipAddress);
            //结果集
            DataSet ds = new DataSet();
            //将查询的结果放入结果集
            sda.Fill(ds, "user");
            //打印结果,这种打印比较麻烦,毕竟谁也不会记住要找的数据是第几行第几列
            Debug.Log(ds.Tables[0].Rows[0][1]);

            //将得到的数据转换成 XML,使用简单,可以直接找到对应的字段名字进行获取
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(ds.GetXml());
            XmlNode xmlNode = xdoc.FirstChild;
            //遍历数据库中的数据
            foreach (XmlNode item in xmlNode)
            {
    
    
                Debug.Log(item.SelectSingleNode("name").InnerText);
                Debug.Log(item.SelectSingleNode("age").InnerText);
            }

            //关闭数据库
            sqlCon.Close();

        }
        catch (Exception e)
        {
    
    
            Debug.LogError(e.StackTrace);
        }
    }
}

The following is a personal reference website
SQLServer2016 installation tutorial
Unity connects to SQLServer and performs queries
VS cannot find System.Data.SqlClient solution
Unity cannot find System.Data.SqlClient minefield

Guess you like

Origin blog.csdn.net/qq_40138744/article/details/121905650