开发与变更

master
bicijinlian 4 years ago
parent 7aa8c46b74
commit 79d80ca21b

@ -8,7 +8,7 @@ namespace AccessStudy.Core
{
public interface IDbUtil
{
DbConnection DbConnection { get; set; }
IDbConnection DbConnection { get; set; }
/// <summary>
/// 表是否存在
@ -23,7 +23,7 @@ namespace AccessStudy.Core
/// <summary>
/// 获取DataSet
/// </summary>
DataSet GetDataSet(string sqlText, List<DbParameter> parameters = null);
DataSet GetDataSet(string sqlText, List<IDbDataParameter> parameters = null);
/// <summary>
/// 获取DataTable
@ -33,7 +33,7 @@ namespace AccessStudy.Core
/// <summary>
/// 获取DataTable
/// </summary>
DataTable GetDataTable(string sqlText, List<DbParameter> parameters = null);
DataTable GetDataTable(string sqlText, List<IDbDataParameter> parameters = null);
/// <summary>
/// 获取DataReader
@ -45,19 +45,19 @@ namespace AccessStudy.Core
/// 获取DataReader
/// 切记:用完之后主动关闭连接
/// </summary>
DbDataReader GetDataReader(string sqlText, List<DbParameter> parameters = null);
DbDataReader GetDataReader(string sqlText, List<IDbDataParameter> parameters = null);
/// <summary>
/// 获取第一行第一列的值
/// 不存在则为null
/// </summary>
object GetScalar(string sqlText, List<DbParameter> parameters = null);
object GetScalar(string sqlText, List<IDbDataParameter> parameters = null);
/// <summary>
/// 执行非查询语句
/// </summary>
/// <returns>受影响行数</returns>
int ExecuteNonQuery(string sqlText, List<DbParameter> parameters = null);
int ExecuteNonQuery(string sqlText, List<IDbDataParameter> parameters = null);
/// <summary>
/// 打开当前连接

@ -4,12 +4,34 @@ using System.Text;
namespace AccessStudy.Core
{
/// <summary>
/// 学生
/// </summary>
public class Student
{
/// <summary>
/// 编号
/// </summary>
public int Id { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
/// <summary>
/// 地址
/// </summary>
public string Address { get; set; }
/// <summary>
/// 学校
/// </summary>
public string School { get; set; }
}
}

@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace AccessStudy.Core
@ -9,19 +12,110 @@ namespace AccessStudy.Core
{
private OledbUtil dbUtil = new OledbUtil();
/// <summary>
/// 获取所有学生
/// </summary>
/// <returns></returns>
public List<Student> GetAll()
{
var dataTable = dbUtil.GetDataTable("Student");
List<Student> students = DataTable2Model(dataTable);
return students;
}
/// <summary>
/// 按标识查询学生
/// </summary>
public Student Get(int studentId)
{
var querySql = "select * from Student where Id=@studentId";
var paras = new List<OleDbParameter>()
{
new OleDbParameter("studentId",studentId)
};
var dataTable = dbUtil.GetDataTable(querySql, paras);
return DataTable2Model(dataTable).FirstOrDefault();
}
/// <summary>
/// 添加学生
/// </summary>
public bool Add(Student student)
{
var querySql = "INSERT INTO `Student` (Name,Age,Address,School) VALUES(@Name,@Age,@Address,@School); ";
var paras = new List<OleDbParameter>()
{
new OleDbParameter("Id",student.Id),
new OleDbParameter("Name",student.Name),
new OleDbParameter("Age",student.Age),
new OleDbParameter("Address",student.Address),
new OleDbParameter("School",student.School),
};
var effectRow = dbUtil.ExecuteNonQuery(querySql, paras);
return effectRow > 0;
}
/// <summary>
/// 更新学生
/// </summary>
public bool Update(Student student)
{
var querySql = "Update Student Set Name=@Name,Age=@Age,Address=@Address,School=@School where Id=@Id;";
var paras = new List<OleDbParameter>()
{
new OleDbParameter("Id",student.Id),
new OleDbParameter("Name",student.Name),
new OleDbParameter("Age",student.Age),
new OleDbParameter("Address",student.Address),
new OleDbParameter("School",student.School),
};
var effectRow = dbUtil.ExecuteNonQuery(querySql, paras);
return effectRow > 0;
}
/// <summary>
/// 删除学生
/// </summary>
public bool Delete(int studentId)
{
var querySql = "DELETE FROM Student WHERE Id = @Id; ";
var paras = new List<OleDbParameter>()
{
new OleDbParameter("Id",studentId),
};
var effectRow = dbUtil.ExecuteNonQuery(querySql, paras);
return effectRow > 0;
}
/// <summary>
/// DataTable转实体列表
/// </summary>
public List<Student> DataTable2Model(DataTable dTable)
{
List<Student> students = new List<Student>();
var dataTable = dbUtil.GetDataTable("Student");
if (dTable==null)
{
return students;
}
foreach (DataRow row in dataTable.Rows)
foreach (DataRow row in dTable.Rows)
{
var student = new Student()
{
Id = (int)row["Id"],
Name=row["Name"].ToString(),
Name = row["Name"]?.ToString() ?? "",
Age = (int)row["Age"],
Address = row["Address"]?.ToString() ?? "",
School = row["School"]?.ToString() ?? "",
};
students.Add(student);
@ -29,5 +123,6 @@ namespace AccessStudy.Core
return students;
}
}
}

@ -9,7 +9,7 @@ namespace AccessStudy.CoreTest
[Fact]
public void Test()
{
Assert.True(true,"使用Xunit单元测试!");
Assert.True(true,"使用xUnit");
}
}
}

@ -1,35 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AccessStudy.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace AccessStudy.OledbApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class DefaultController : ControllerBase
{
private readonly ILogger<DefaultController> _logger;
private readonly StudentServer _studentServer;
public DefaultController(ILogger<DefaultController> logger,StudentServer studentServer)
{
_logger = logger;
_studentServer = studentServer;
}
public IActionResult Index()
{
var students = _studentServer.GetAll();
return new JsonResult(students);
}
}
}

@ -4,9 +4,5 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AccessStudy.Core\AccessStudy.Core.csproj" />
</ItemGroup>
</Project>

@ -1,12 +1,12 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace AccessStudy.OledbApi.Controllers
namespace AccessStudy.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]

@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace AccessStudy.OledbApi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AccessStudy.WebApi
{
public class Program
{

@ -4,7 +4,7 @@
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:30078",
"applicationUrl": "http://localhost:12854",
"sslPort": 0
}
},
@ -17,10 +17,10 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"AccessStudy.OledbApi": {
"AccessStudy.WebApi": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/Default/Index",
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"

@ -1,10 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AccessStudy.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
@ -13,7 +6,12 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace AccessStudy.OledbApi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AccessStudy.WebApi
{
public class Startup
{
@ -28,7 +26,6 @@ namespace AccessStudy.OledbApi
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<StudentServer>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

@ -1,6 +1,6 @@
using System;
namespace AccessStudy.OledbApi
namespace AccessStudy.WebApi
{
public class WeatherForecast
{

@ -5,9 +5,9 @@ VisualStudioVersion = 16.0.30621.155
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccessStudy.Core", "AccessStudy.Core\AccessStudy.Core.csproj", "{0670AFE2-3712-44A0-883B-4392E9FD2E73}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccessStudy.OledbApi", "AccessStudy.OledbApi\AccessStudy.OledbApi.csproj", "{A2D5ADA0-2D19-4AB3-AAED-4397727D5866}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccessStudy.CoreTest", "AccessStudy.CoreTest\AccessStudy.CoreTest.csproj", "{B0118C4C-10D8-4515-A336-A90479024D7C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccessStudy.CoreTest", "AccessStudy.CoreTest\AccessStudy.CoreTest.csproj", "{B0118C4C-10D8-4515-A336-A90479024D7C}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccessStudy.WebApi", "AccessStudy.WebApi\AccessStudy.WebApi.csproj", "{2858AA9B-4F2B-4591-9179-033139719704}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -19,14 +19,14 @@ Global
{0670AFE2-3712-44A0-883B-4392E9FD2E73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0670AFE2-3712-44A0-883B-4392E9FD2E73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0670AFE2-3712-44A0-883B-4392E9FD2E73}.Release|Any CPU.Build.0 = Release|Any CPU
{A2D5ADA0-2D19-4AB3-AAED-4397727D5866}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A2D5ADA0-2D19-4AB3-AAED-4397727D5866}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2D5ADA0-2D19-4AB3-AAED-4397727D5866}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2D5ADA0-2D19-4AB3-AAED-4397727D5866}.Release|Any CPU.Build.0 = Release|Any CPU
{B0118C4C-10D8-4515-A336-A90479024D7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0118C4C-10D8-4515-A336-A90479024D7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0118C4C-10D8-4515-A336-A90479024D7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0118C4C-10D8-4515-A336-A90479024D7C}.Release|Any CPU.Build.0 = Release|Any CPU
{2858AA9B-4F2B-4591-9179-033139719704}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2858AA9B-4F2B-4591-9179-033139719704}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2858AA9B-4F2B-4591-9179-033139719704}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2858AA9B-4F2B-4591-9179-033139719704}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save