You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity项目用VS2022连接SQL Server时SqlConnection未被识别

问题解决:Unity连接SQL Server时找不到SqlConnection的错误

问题描述

在Unity项目中使用VS2022编写SQL Server连接代码时,出现以下错误:

Error CS1069
The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'. This type has been forwarded to assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Consider adding a reference to that assembly.

已安装System.Data.SqlClient 4.8.6版本NuGet包,但错误仍未解决。

核心原因

Unity的运行时环境(Mono或兼容.NET Framework的运行时)无法直接识别VS中安装的NuGet包,需要手动将对应的程序集引入Unity项目。

解决方案

1. 手动导入SqlClient程序集

  • 找到本地NuGet包的安装路径:一般位于C:\Users\[你的用户名]\.nuget\packages\system.data.sqlclient\4.8.6\lib
  • 根据Unity的脚本运行时版本选择对应程序集:
    • 若Unity设置为.NET 4.x Equivalent(推荐),选择net46\System.Data.SqlClient.dll
    • 若使用新的.NET Core兼容模式,选择netstandard2.0\System.Data.SqlClient.dll
  • 将选中的System.Data.SqlClient.dll复制到Unity项目的Assets/Plugins文件夹(无此文件夹则新建)
  • 回到Unity编辑器,等待项目重新编译,检查错误是否消失。

2. 修正代码中的语法错误

原代码存在两处小问题,修正后代码如下:

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

public class GenerateLabels : MonoBehaviour
{
    public string UserName;

    void Start()
    {
        Debug.Log("Hello world !!!");
        string connectionString =
           "Server=XX;" + 
           "Database=XX;" + 
           "User ID=XX;" + 
           "Password=XX;"; 
        IDbConnection dbcon;
        using (dbcon = new SqlConnection(connectionString))
        {
            dbcon.Open();

            using (IDbCommand dbcmd = dbcon.CreateCommand())
            {
                string sql = "SELECT Name, UserPrincipalName FROM Users WHERE Id=1";
                dbcmd.CommandText = sql;

                using (IDataReader reader = dbcmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string FirstName = (string)reader["Name"];
                        string LastName = (string)reader["UserPrincipalName"];
                        UserName = "Name: " + FirstName + " " + LastName;
                        Debug.Log(UserName); 
                    }
                }
            }
        }
    }

    void OnGUI()
    {
        Rect position = new Rect(60, 20, 400, 60);
        GUI.Label(position, UserName);
    }
}

修正点:

  • 移除了错误的function关键字(Unity MonoBehaviour方法无需此关键字)
  • 删除了无用的using static UnityEngine.UIElements.UxmlAttributeDescription;引用

3. 额外优化建议

  • 在Unity的Player Settings中确认脚本运行时版本:Edit > Project Settings > Player > Other Settings > Scripting Runtime Version,选择.NET 4.x Equivalent以获得更好的兼容性
  • 不要在Unity主线程中执行数据库操作,会导致游戏卡顿,建议使用Coroutine或异步Task处理
  • 若仍有问题,可尝试添加System.Data.dll(从.NET Framework安装目录复制,如C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Data.dll)到Plugins文件夹

内容的提问来源于stack exchange,提问作者Tat0r0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 06:03:15