diff --git a/AccessStudy.Core/AccessDB/Demo.accdb b/AccessStudy.Core/AccessDB/Demo.accdb index 1ce419a..e09c011 100644 Binary files a/AccessStudy.Core/AccessDB/Demo.accdb and b/AccessStudy.Core/AccessDB/Demo.accdb differ diff --git a/AccessStudy.Core/AccessDB/Demo.mdb b/AccessStudy.Core/AccessDB/Demo.mdb index b1cd5e9..ff4536d 100644 Binary files a/AccessStudy.Core/AccessDB/Demo.mdb and b/AccessStudy.Core/AccessDB/Demo.mdb differ diff --git a/AccessStudy.Core/IDbUtil.cs b/AccessStudy.Core/IDbUtil.cs index 1dfa053..33519f3 100644 --- a/AccessStudy.Core/IDbUtil.cs +++ b/AccessStudy.Core/IDbUtil.cs @@ -8,7 +8,7 @@ namespace AccessStudy.Core { public interface IDbUtil { - DbConnection DbConnection { get; set; } + IDbConnection DbConnection { get; set; } /// /// 表是否存在 @@ -23,7 +23,7 @@ namespace AccessStudy.Core /// /// 获取DataSet /// - DataSet GetDataSet(string sqlText, List parameters = null); + DataSet GetDataSet(string sqlText, List parameters = null); /// /// 获取DataTable @@ -33,7 +33,7 @@ namespace AccessStudy.Core /// /// 获取DataTable /// - DataTable GetDataTable(string sqlText, List parameters = null); + DataTable GetDataTable(string sqlText, List parameters = null); /// /// 获取DataReader @@ -45,19 +45,19 @@ namespace AccessStudy.Core /// 获取DataReader /// 切记:用完之后主动关闭连接 /// - DbDataReader GetDataReader(string sqlText, List parameters = null); + DbDataReader GetDataReader(string sqlText, List parameters = null); /// /// 获取第一行第一列的值 /// 不存在则为null /// - object GetScalar(string sqlText, List parameters = null); + object GetScalar(string sqlText, List parameters = null); /// /// 执行非查询语句 /// /// 受影响行数 - int ExecuteNonQuery(string sqlText, List parameters = null); + int ExecuteNonQuery(string sqlText, List parameters = null); /// /// 打开当前连接 diff --git a/AccessStudy.Core/Student.cs b/AccessStudy.Core/Student.cs index 6db9ed1..d48f97f 100644 --- a/AccessStudy.Core/Student.cs +++ b/AccessStudy.Core/Student.cs @@ -4,12 +4,34 @@ using System.Text; namespace AccessStudy.Core { + /// + /// 学生 + /// public class Student { + /// + /// 编号 + /// public int Id { get; set; } + /// + /// 姓名 + /// public string Name { get; set; } + /// + /// 年龄 + /// public int Age { get; set; } + + /// + /// 地址 + /// + public string Address { get; set; } + + /// + /// 学校 + /// + public string School { get; set; } } } diff --git a/AccessStudy.Core/StudentServer.cs b/AccessStudy.Core/StudentServer.cs index 8d0811a..40a82ae 100644 --- a/AccessStudy.Core/StudentServer.cs +++ b/AccessStudy.Core/StudentServer.cs @@ -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(); + /// + /// 获取所有学生 + /// + /// public List GetAll() + { + var dataTable = dbUtil.GetDataTable("Student"); + List students = DataTable2Model(dataTable); + + return students; + } + + /// + /// 按标识查询学生 + /// + public Student Get(int studentId) + { + var querySql = "select * from Student where Id=@studentId"; + var paras = new List() + { + new OleDbParameter("studentId",studentId) + }; + var dataTable = dbUtil.GetDataTable(querySql, paras); + + return DataTable2Model(dataTable).FirstOrDefault(); + } + + /// + /// 添加学生 + /// + public bool Add(Student student) + { + var querySql = "INSERT INTO `Student` (Name,Age,Address,School) VALUES(@Name,@Age,@Address,@School); "; + var paras = new List() + { + 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; + } + + /// + /// 更新学生 + /// + 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() + { + 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; + } + + /// + /// 删除学生 + /// + public bool Delete(int studentId) + { + var querySql = "DELETE FROM Student WHERE Id = @Id; "; + var paras = new List() + { + new OleDbParameter("Id",studentId), + }; + + var effectRow = dbUtil.ExecuteNonQuery(querySql, paras); + + return effectRow > 0; + } + + /// + /// DataTable转实体列表 + /// + public List DataTable2Model(DataTable dTable) { List students = new List(); - 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; } + } } diff --git a/AccessStudy.CoreTest/UseXunit.cs b/AccessStudy.CoreTest/UseXunit.cs index f2b340e..a38a974 100644 --- a/AccessStudy.CoreTest/UseXunit.cs +++ b/AccessStudy.CoreTest/UseXunit.cs @@ -9,7 +9,7 @@ namespace AccessStudy.CoreTest [Fact] public void Test() { - Assert.True(true,"ʹXunitԪ!"); + Assert.True(true,"使用xUnit"); } } } diff --git a/AccessStudy.OledbApi/Controllers/DefaultController.cs b/AccessStudy.OledbApi/Controllers/DefaultController.cs deleted file mode 100644 index 88f180a..0000000 --- a/AccessStudy.OledbApi/Controllers/DefaultController.cs +++ /dev/null @@ -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 _logger; - private readonly StudentServer _studentServer; - - public DefaultController(ILogger logger,StudentServer studentServer) - { - _logger = logger; - _studentServer = studentServer; - } - - public IActionResult Index() - { - - var students = _studentServer.GetAll(); - - return new JsonResult(students); - } - } -} diff --git a/AccessStudy.OledbApi/AccessStudy.OledbApi.csproj b/AccessStudy.WebApi/AccessStudy.WebApi.csproj similarity index 56% rename from AccessStudy.OledbApi/AccessStudy.OledbApi.csproj rename to AccessStudy.WebApi/AccessStudy.WebApi.csproj index d73f0a1..d12c450 100644 --- a/AccessStudy.OledbApi/AccessStudy.OledbApi.csproj +++ b/AccessStudy.WebApi/AccessStudy.WebApi.csproj @@ -4,9 +4,5 @@ netcoreapp3.1 - - - - diff --git a/AccessStudy.OledbApi/Controllers/WeatherForecastController.cs b/AccessStudy.WebApi/Controllers/WeatherForecastController.cs similarity index 92% rename from AccessStudy.OledbApi/Controllers/WeatherForecastController.cs rename to AccessStudy.WebApi/Controllers/WeatherForecastController.cs index ad36b1d..03c9242 100644 --- a/AccessStudy.OledbApi/Controllers/WeatherForecastController.cs +++ b/AccessStudy.WebApi/Controllers/WeatherForecastController.cs @@ -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]")] diff --git a/AccessStudy.OledbApi/Program.cs b/AccessStudy.WebApi/Program.cs similarity index 95% rename from AccessStudy.OledbApi/Program.cs rename to AccessStudy.WebApi/Program.cs index ebd5376..6640204 100644 --- a/AccessStudy.OledbApi/Program.cs +++ b/AccessStudy.WebApi/Program.cs @@ -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 { diff --git a/AccessStudy.OledbApi/Properties/launchSettings.json b/AccessStudy.WebApi/Properties/launchSettings.json similarity index 84% rename from AccessStudy.OledbApi/Properties/launchSettings.json rename to AccessStudy.WebApi/Properties/launchSettings.json index a69b475..509bb83 100644 --- a/AccessStudy.OledbApi/Properties/launchSettings.json +++ b/AccessStudy.WebApi/Properties/launchSettings.json @@ -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" diff --git a/AccessStudy.OledbApi/Startup.cs b/AccessStudy.WebApi/Startup.cs similarity index 92% rename from AccessStudy.OledbApi/Startup.cs rename to AccessStudy.WebApi/Startup.cs index 51fb308..8ad109c 100644 --- a/AccessStudy.OledbApi/Startup.cs +++ b/AccessStudy.WebApi/Startup.cs @@ -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(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/AccessStudy.OledbApi/WeatherForecast.cs b/AccessStudy.WebApi/WeatherForecast.cs similarity index 89% rename from AccessStudy.OledbApi/WeatherForecast.cs rename to AccessStudy.WebApi/WeatherForecast.cs index beb937b..775e0c0 100644 --- a/AccessStudy.OledbApi/WeatherForecast.cs +++ b/AccessStudy.WebApi/WeatherForecast.cs @@ -1,6 +1,6 @@ using System; -namespace AccessStudy.OledbApi +namespace AccessStudy.WebApi { public class WeatherForecast { diff --git a/AccessStudy.OledbApi/appsettings.Development.json b/AccessStudy.WebApi/appsettings.Development.json similarity index 100% rename from AccessStudy.OledbApi/appsettings.Development.json rename to AccessStudy.WebApi/appsettings.Development.json diff --git a/AccessStudy.OledbApi/appsettings.json b/AccessStudy.WebApi/appsettings.json similarity index 100% rename from AccessStudy.OledbApi/appsettings.json rename to AccessStudy.WebApi/appsettings.json diff --git a/AccessStudy.sln b/AccessStudy.sln index 9bd8abb..a5f0037 100644 --- a/AccessStudy.sln +++ b/AccessStudy.sln @@ -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