code

ASP.NET Core에서 SqlClient를 사용하는 방법?

codestyles 2020. 12. 4. 08:15
반응형

ASP.NET Core에서 SqlClient를 사용하는 방법?


ASP.net Core에서 SQLClient 라이브러리를 사용하려고하지만 작동하지 않는 것 같습니다. 이 기사는 온라인에서 설정 방법을 조언하지만 저에게 적합하지 않습니다. http://blog.developers.ba/using-classic-ado-net-in-asp-net-vnext/

간단한 콘솔 응용 프로그램 패키지가 있습니다. 내 project.json은 다음과 같습니다.

{
  "version": "1.0.0-*",
  "description": "DBTest Console Application",
  "authors": [ "" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "System.Data.Common": "4.0.1-beta-23516",
    "System.Data.SqlClient" :  "4.0.0-beta-23516"
  },

  "commands": {
    "DBTest": "DBTest"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Console": "4.0.0-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Threading": "4.0.11-beta-23516"
      }
    }
  }
}

그리고 다음 코드를 시도합니다.

using System;
using System.Data.SqlClient;

namespace DBTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (SqlConnection con = new SqlConnection(ConnStr)) {
                con.Open();
                try {
                    using (SqlCommand command = new SqlCommand("SELECT * FROM SAMPLETABLE", con)) {
                        command.ExecuteNonQuery();
                    }
                }
                catch {
                    Console.WriteLine("Something went wrong");
                }
            }

            Console.Read();
        }
    }
}

그러나 다음과 같은 오류가 발생합니다.

enter image description here

다른 사람이이 작업을 했습니까?


튜토리얼에서이 부분을 놓친 것 같습니다.

System.Data 및 System.Data.SqlClient를 참조하는 대신 Nuget에서 가져와야합니다.

System.Data.Common 및 System.Data.SqlClient.

현재 이것은 project.json –> aspnetcore50 섹션에이 두 라이브러리에 대한 종속성을 생성합니다.

"aspnetcore50": {
       "dependencies": {
           "System.Runtime": "4.0.20-beta-22523",
           "System.Data.Common": "4.0.0.0-beta-22605",
           "System.Data.SqlClient": "4.0.0.0-beta-22605"
       }
}

Try getting System.Data.Common and System.Data.SqlClient via Nuget and see if this adds the above dependencies for you, but in a nutshell you are missing System.Runtime.


Try this one Open your projectname.csproj file its work for me.

<PackageReference Include="System.Data.SqlClient" Version="4.6.0" />

You need to add this Reference "ItemGroup" tag inside.

참고URL : https://stackoverflow.com/questions/35444487/how-to-use-sqlclient-in-asp-net-core

반응형