I have this code in c# where i try to connect to my database and when i try to run the script in Unity, its says that I'm missing the namespace "System.Data.SqlClient" even though I have the reference in code and the DLL inside the Assets folder
I'm the visual studio doesn't show any error on the code only when I try to run it on Unity.
Here is the code:
EDIT : Added the using statments
using UnityEngine;
using System.Data.SqlClient;
using System;
private void Start()
{
Debug.Log("Connecting to database...");
connectionstring = "Server=MYSERVER;Database=MYDATABASE;User Id=MYUSER;Password = MYPASSWORD; ";
SqlConnection dbConnection = new SqlConnection(connectionstring);
try
{
dbConnection.Open();
Debug.Log("Connected to database.");
}
catch (Exception _exception)
{
Debug.LogWarning(_exception.ToString());
}
// conn.Close();
}
If anyone can help I'll appreciate. Thank you.
5条答案
按热度按时间brc7rcf01#
The Reason for this error is that the namespace System.Data.SqlClient will not be referenced by the .NET Core project by default as done by .NET Framework, So, in .NET Core, you have to manually add the package to the project.
The solution is to Install and use the new System.Data.SqlClient package in NuGet to your project or solution. Follow these steps to install the package.
Right Click on your solution from the solution panel in VS. From the context menu, select the Manage NuGet Packages for solutions... In the NuGet Package Manager window, select the Browse Tab. In the search box type in System.Data.SqlClient and press enter. Look for the package System.Data.SqlClient by Microsoft and select it. A small panel will open at the right side of the window with the list of projects in your solution. Select only the projects you want the SqlClient package to be installed. Then press the install button. Wait for the installation to complete. NuGet will install the selected package and all it’s dependencies. Now go back to your solution or project and rebuild. You will not get the error again. https://www.mytecbits.com/microsoft/dot-net/error-sqlconnection-could-not-be-found
2eafrhcq2#
you can also reference Microsoft.Data.SqlClient. System.Data.SqlClient is in servicing mode and is not getting any update unless a hotfix or something similar. Also, M.D.S (Microsoft.Data.SqlClient) is designed to cover netcore by having a different set of codes designed for that platform.
qkf9rpyu3#
Right-click on references, then go to "Nuggets" and look for (System.Data.SqlClient). Then install and accept the terms of use.
t98cgbkg4#
I had this same problem with VS2022 (.Net6) (one I've never had before in other versions of .Net) which persisted even after installing the Nuget project and rebuilding. I had to deselect system.data.sqlclient from the references and then reselect, rebuild to finally get sqlconnection, sqlreader, etc. recognized.
z4iuyo4d5#
You can make a new "clean project", try closing VS and starting again (sometimes it's just stuck)
OR
System.Configuration
on the list and Click OK.