SQL Server error CS1069: The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'

esyap4oy  于 2023-06-21  发布在  其他
关注(0)|答案(5)|浏览(375)

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.

brc7rcf0

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

2eafrhcq

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.

qkf9rpyu

qkf9rpyu3#

Right-click on references, then go to "Nuggets" and look for (System.Data.SqlClient). Then install and accept the terms of use.

t98cgbkg

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.

z4iuyo4d

z4iuyo4d5#

You can make a new "clean project", try closing VS and starting again (sometimes it's just stuck)

OR

  1. In Solution Explorer, in the project where you want to use this line of code
  2. Right click in References, choose Add Reference...
  3. Then choose Assemblies on the left-side and Framework under it.
  4. Pick System.Configuration on the list and Click OK.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ConnectionString" value="Data Source=MY-PC;Initial Catalog=DB2013;User ID=sa;Password=MYSQL123" />
  </appSettings>
</configuration>
using System.Configuration;
using System.Data.SqlClient;

namespace Test
{
public partial class Form1: Form
{
   SqlConnection MyConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);

    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
           MyConnection.Open();
        }
        catch (Exception)
        {

            throw;
        }

    }
}

相关问题