缺少用于将 C# 连接到 Unity 中的 SQL 服务器的 .dll
Missing .dll's for connecting C# to SQL Server in Unity
我正在尝试通过 Unity 中的 C# 连接到 SQL 服务器(在某些 Azure 服务器上)。但是,我一直 运行 关注不同的问题,以至于我不知道如何解决它们,而且我发现的在线教程停止解释他们的解决方案是如何工作的。
我有这个代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.Data.SqlClient;
using Microsoft.Data;
public class DataBaseConnection : MonoBehaviour
{
// after the SQL query is executed, we will have a filled users array
List<float> SensorValue = new List<float>();
// Use this for initialization
void Start()
{
SensorValue = ConnectToDB();
}
// function to connect to the db and retrieve the data
List<float> ConnectToDB()
{
List<float> SensorValue = new List<float>();
// Build connection string
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "xxx";
builder.UserID = "xxx";
builder.Password = "xxx";
builder.InitialCatalog = "xxx";
try
{
// connect to the database
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
// if open then the connection is established
connection.Open();
Debug.Log("connection established");
connection.Close();
}
}
catch (SqlException e)
{
Debug.Log(e.ToString());
}
return SensorValue;
}
}
但是,我什至无法开始创建连接。首先,缺少 Microsoft.Data.SqlClient
命名空间,因此我将其添加到 Unity 项目的“Assets”文件夹中。然后尝试调试时,我收到一条消息:
Error: Could not load signature of Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationProvider:.ctor due to: Could not load file or assembly 'Microsoft.Identity.Client, Version=4.22.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae' or one of its dependencies. assembly:Microsoft.Identity.Client, Version=4.22.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae type: member:(null) signature:
为了解决这个问题,我将 Microsoft.Identity.Client dll 添加到 asset 文件夹中,但是随之而来的是找不到 10 多个其他 .dll,这在我看来非常不合逻辑浅见。我可以做些什么来解决这个问题吗?
注意:在各种教程中我发现他们正在使用 System.Data.SqlClient
命名空间,但是在使用此命名空间时甚至无法找到对命名空间的引用(例如 SqlConnection
)并且我收到命名空间丢失的错误消息...我用谷歌搜索了一下,发现 Microsoft decided to replace the System.Data.SqlClient
with a new Microsoft version。所以这似乎并没有解决它..
好吧,在尝试了有关添加.dll 的不同解决方案后,我发现项目设置不正确。因此,为了帮助可能 运行 遇到同样问题的任何人:我的解决方案是转到项目设置 -> 播放器 -> “配置”header 并确保“Api兼容级别”在 .NET 4.X 上而不在 .NET 2.X
上
我正在尝试通过 Unity 中的 C# 连接到 SQL 服务器(在某些 Azure 服务器上)。但是,我一直 运行 关注不同的问题,以至于我不知道如何解决它们,而且我发现的在线教程停止解释他们的解决方案是如何工作的。
我有这个代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.Data.SqlClient;
using Microsoft.Data;
public class DataBaseConnection : MonoBehaviour
{
// after the SQL query is executed, we will have a filled users array
List<float> SensorValue = new List<float>();
// Use this for initialization
void Start()
{
SensorValue = ConnectToDB();
}
// function to connect to the db and retrieve the data
List<float> ConnectToDB()
{
List<float> SensorValue = new List<float>();
// Build connection string
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "xxx";
builder.UserID = "xxx";
builder.Password = "xxx";
builder.InitialCatalog = "xxx";
try
{
// connect to the database
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
// if open then the connection is established
connection.Open();
Debug.Log("connection established");
connection.Close();
}
}
catch (SqlException e)
{
Debug.Log(e.ToString());
}
return SensorValue;
}
}
但是,我什至无法开始创建连接。首先,缺少 Microsoft.Data.SqlClient
命名空间,因此我将其添加到 Unity 项目的“Assets”文件夹中。然后尝试调试时,我收到一条消息:
Error: Could not load signature of Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationProvider:.ctor due to: Could not load file or assembly 'Microsoft.Identity.Client, Version=4.22.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae' or one of its dependencies. assembly:Microsoft.Identity.Client, Version=4.22.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae type: member:(null) signature:
为了解决这个问题,我将 Microsoft.Identity.Client dll 添加到 asset 文件夹中,但是随之而来的是找不到 10 多个其他 .dll,这在我看来非常不合逻辑浅见。我可以做些什么来解决这个问题吗?
注意:在各种教程中我发现他们正在使用 System.Data.SqlClient
命名空间,但是在使用此命名空间时甚至无法找到对命名空间的引用(例如 SqlConnection
)并且我收到命名空间丢失的错误消息...我用谷歌搜索了一下,发现 Microsoft decided to replace the System.Data.SqlClient
with a new Microsoft version。所以这似乎并没有解决它..
好吧,在尝试了有关添加.dll 的不同解决方案后,我发现项目设置不正确。因此,为了帮助可能 运行 遇到同样问题的任何人:我的解决方案是转到项目设置 -> 播放器 -> “配置”header 并确保“Api兼容级别”在 .NET 4.X 上而不在 .NET 2.X
上