エラーコード:CS1069
型名 'SqlConnection' は名前空間 'System.Data.SqlClient' に見つかりませんでした。この型はアセンブリ 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' に転送されました。このアセンブリへの参照を追加することを検討してください。
using System.Data.SqlClient
を記述しただけでは、SqlConnectionを利用できません。
この記事では、System.Data.SqlClientのアセンブリの参照を追加して、SqlConnectionを利用できるようにする手順を紹介します。using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace SimpleExecSql
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
SqlConnection con;
});
});
}
}
}
上記のプロジェクトをビルドします。ビルドするとエラーが発生します。System.Data.SqlClient
アセンブリには、SqlConnectionが含まれていないことが原因です。System.Data.SqlClient
アセンブリを参照に追加します。System.Data.SqlClient
アセンブリの参照を追加します。<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.8.0" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.6.0" />
</ItemGroup>
</Project>
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace SimpleExecSql
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
int count = -1;
endpoints.MapGet("/", async context =>
{
using (SqlConnection con = new SqlConnection(@"Data Source=[DBサーバーのホスト名 または IPアドレス];Initial Catalog=[接続するデータベース名];Connect Timeout=60;Persist Security Info=True;User ID=[DBの接続ユーザー名];Password=[DBの接続パスワード]")) {
con.Open();
SqlCommand com = new SqlCommand("select count (*) from ProductsA", con);
count = (int)com.ExecuteScalar();
con.Close();
}
context.Response.ContentType = "text/plain; charset=utf-8";
await context.Response.WriteAsync("レコード数:" + count.ToString());
});
});
}
}
}