diff --git a/AccessStudy.Core/OdbcUtil.cs b/AccessStudy.Core/OdbcUtil.cs
index c2752e9..d5c9f9a 100644
--- a/AccessStudy.Core/OdbcUtil.cs
+++ b/AccessStudy.Core/OdbcUtil.cs
@@ -5,19 +5,23 @@ using System.Data.Odbc;
namespace AccessStudy.Core
{
- public class OdbcUtil:IDisposable
+ ///
+ /// 注意:参数用?代替,以顺序计数!
+ /// 如:select * from tableA where id=? and age>?
+ ///
+ public class OdbcUtil : IDisposable
{
public OdbcConnection DbConnection { get; set; }
public OdbcUtil()
{
var accessFile = $"{AppDomain.CurrentDomain.BaseDirectory}AccessDB\\Demo.accdb";
- var connetString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={accessFile}";
+ var connetString = $@"Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};Dbq={accessFile};";
DbConnection = new OdbcConnection(connetString);
}
public OdbcUtil(string connetString)
- {
+ {
DbConnection = new OdbcConnection(connetString);
}
@@ -71,7 +75,7 @@ namespace AccessStudy.Core
adapter.Fill(dataSet);
}
- catch(Exception ex)
+ catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
@@ -86,7 +90,7 @@ namespace AccessStudy.Core
///
/// 获取DataSet
///
- public DataSet GetDataSet(string sqlText, List parameters=null)
+ public DataSet GetDataSet(string sqlText, List parameters = null)
{
DataSet dataSet = new DataSet();
try
@@ -99,11 +103,11 @@ namespace AccessStudy.Core
CommandText = sqlText,
};
- if (parameters !=null)
- {
+ if (parameters != null)
+ {
command.Parameters.AddRange(parameters.ToArray());
}
-
+
var adapter = new OdbcDataAdapter(command);
adapter.Fill(dataSet);
@@ -132,9 +136,9 @@ namespace AccessStudy.Core
///
/// 获取DataTable
///
- public DataTable GetDataTable(string sqlText, List parameters=null)
+ public DataTable GetDataTable(string sqlText, List parameters = null)
{
- DataSet dataSet = GetDataSet(sqlText,parameters);
+ DataSet dataSet = GetDataSet(sqlText, parameters);
return dataSet.Tables[0];
}
@@ -174,7 +178,7 @@ namespace AccessStudy.Core
/// 获取DataReader
/// 切记:用完之后主动关闭连接
///
- public OdbcDataReader GetDataReader(string sqlText, List parameters=null)
+ public OdbcDataReader GetDataReader(string sqlText, List parameters = null)
{
OdbcDataReader dataReader = null;
try
@@ -184,7 +188,7 @@ namespace AccessStudy.Core
{
Connection = DbConnection,
CommandType = CommandType.Text,
- CommandText =sqlText,
+ CommandText = sqlText,
};
if (parameters != null)
@@ -211,7 +215,7 @@ namespace AccessStudy.Core
/// 获取第一行第一列的值
/// 不存在则为null
///
- public object GetScalar(string sqlText, List parameters=null)
+ public object GetScalar(string sqlText, List parameters = null)
{
object result = null;
@@ -234,7 +238,7 @@ namespace AccessStudy.Core
}
catch (Exception ex)
{
- throw ex;
+ throw ex;
}
finally
{
@@ -305,7 +309,7 @@ namespace AccessStudy.Core
public void Dispose()
{
- if (DbConnection.State==ConnectionState.Open)
+ if (DbConnection.State == ConnectionState.Open)
{
DbConnection.Close();
}
diff --git a/AccessStudy.Core/StudentServer.cs b/AccessStudy.Core/StudentDal.cs
similarity index 80%
rename from AccessStudy.Core/StudentServer.cs
rename to AccessStudy.Core/StudentDal.cs
index 40a82ae..89a26e1 100644
--- a/AccessStudy.Core/StudentServer.cs
+++ b/AccessStudy.Core/StudentDal.cs
@@ -8,7 +8,7 @@ using System.Text;
namespace AccessStudy.Core
{
- public class StudentServer
+ public class StudentDal
{
private OledbUtil dbUtil = new OledbUtil();
@@ -39,6 +39,21 @@ namespace AccessStudy.Core
return DataTable2Model(dataTable).FirstOrDefault();
}
+ ///
+ /// 按名称查询学生
+ ///
+ public Student Get(string studentName)
+ {
+ var querySql = "select * from Student where Name=@studentName";
+ var paras = new List()
+ {
+ new OleDbParameter("studentName",studentName)
+ };
+ var dataTable = dbUtil.GetDataTable(querySql, paras);
+
+ return DataTable2Model(dataTable).FirstOrDefault();
+ }
+
///
/// 添加学生
///
@@ -47,11 +62,10 @@ namespace AccessStudy.Core
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),
+ 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);
@@ -67,11 +81,11 @@ namespace AccessStudy.Core
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),
+ new OleDbParameter("Id",student.Id),
};
var effectRow = dbUtil.ExecuteNonQuery(querySql, paras);
@@ -98,7 +112,7 @@ namespace AccessStudy.Core
///
/// DataTable转实体列表
///
- public List DataTable2Model(DataTable dTable)
+ private List DataTable2Model(DataTable dTable)
{
List students = new List();
diff --git a/AccessStudy.CoreTest/OdbcUtilTest.cs b/AccessStudy.CoreTest/OdbcUtilTest.cs
index 0dbd266..77c4aa1 100644
--- a/AccessStudy.CoreTest/OdbcUtilTest.cs
+++ b/AccessStudy.CoreTest/OdbcUtilTest.cs
@@ -1,17 +1,121 @@
using System;
using System.Collections.Generic;
using System.Text;
+using System.Data.Odbc;
using Xunit;
+using Shouldly;
+
+using AccessStudy.Core;
namespace AccessStudy.CoreTest
{
- public class OdbcUtilTest
+ public class OdbcUtilTest:IDisposable
{
+ private readonly OdbcUtil _dbUtil;
+ public OdbcUtilTest()
+ {
+ _dbUtil = new OdbcUtil();
+ }
+
+ [Fact]
+ public void DbUtil_NotNull_Test()
+ {
+ Assert.NotNull(_dbUtil);
+ }
+
+ [Fact]
+ public void DbConnet_Test()
+ {
+ var con = _dbUtil.DbConnection;
+ Assert.NotNull(con);
+
+ Action action = () =>
+ {
+ con.Open();
+ con.Close();
+ con.Dispose();
+ };
+
+ Should.NotThrow(action);
+ }
+
+ [Fact]
+ public void DataSet_Test()
+ {
+ var ds = _dbUtil.GetDataSet("Student");
+
+ Assert.NotNull(ds);
+
+ Assert.NotNull(ds.Tables);
+
+ Assert.True(ds.Tables.Count > 0);
+ }
+
+ [Fact]
+ public void DataTable_Test()
+ {
+ var dt = _dbUtil.GetDataTable("Student");
+ Assert.NotNull(dt);
+ }
+
+ [Fact]
+ public void DataReader_Test()
+ {
+ Action action = () =>
+ {
+ var dataReader = _dbUtil.GetDataReader("Student");
+ while (dataReader.Read())
+ {
+ break;
+ }
+
+ dataReader.Close();
+ };
+
+ Should.NotThrow(action);
+ }
+
+ ///
+ /// 参数使用不同
+ /// SQL语句中用?代表参数,按参数顺序传参
+ /// 参数名以不用@开头都可以
+ ///
+ [Fact]
+ public void Scalar_Test()
+ {
+ //
+
+ var sqlText = @"select count(*) as Total from Student where Age>? ;";
+ var paras = new List()
+ {
+ new OdbcParameter("@Age",1),
+ };
+
+ var total = (int)_dbUtil.GetScalar(sqlText, paras);
+
+ Assert.True(total >= 0);
+ }
+
+
[Fact]
- public void Test()
- {
-
+ public void NonQuery_Test()
+ {
+ var sqlText = @"update Student Set Age=Age+1 where Age>? AND Name <>?;";
+ var paras = new List()
+ {
+ new OdbcParameter("Age",1),
+ new OdbcParameter("Name","王高峰"),
+ };
+
+ var total = _dbUtil.ExecuteNonQuery(sqlText, paras);
+
+ Assert.True(total >= 0);
+ }
+
+ public void Dispose()
+ {
+
}
}
}
diff --git a/AccessStudy.CoreTest/OleDbUtilTest.cs b/AccessStudy.CoreTest/OleDbUtilTest.cs
index 16dbeb9..f4db8d1 100644
--- a/AccessStudy.CoreTest/OleDbUtilTest.cs
+++ b/AccessStudy.CoreTest/OleDbUtilTest.cs
@@ -13,7 +13,7 @@ using System.Data.OleDb;
namespace AccessStudy.CoreTest
{
- public class OleDbUtilTest:IDisposable
+ public class OleDbUtilTest : IDisposable
{
private readonly OledbUtil _dbUtil;
@@ -34,13 +34,11 @@ namespace AccessStudy.CoreTest
var con = _dbUtil.DbConnection;
Assert.NotNull(con);
- Action action = () =>
- {
+ Action action = () =>
+ {
con.Open();
con.Close();
con.Dispose();
-
- //throw new Exception("特意抛出的异常!");
};
Should.NotThrow(action);
@@ -55,7 +53,7 @@ namespace AccessStudy.CoreTest
Assert.NotNull(ds.Tables);
- Assert.True(ds.Tables.Count>0);
+ Assert.True(ds.Tables.Count > 0);
}
[Fact]
@@ -68,7 +66,7 @@ namespace AccessStudy.CoreTest
[Fact]
public void DataReader_Test()
{
- Action action = () =>
+ Action action = () =>
{
var dataReader = _dbUtil.GetDataReader("Student");
while (dataReader.Read())
@@ -97,7 +95,7 @@ namespace AccessStudy.CoreTest
Assert.True(total >= 0);
}
-
+
[Fact]
public void NonQuery_Test()
{
@@ -113,8 +111,6 @@ namespace AccessStudy.CoreTest
Assert.True(total >= 0);
}
-
-
public void Dispose()
{
}
diff --git a/AccessStudy.CoreTest/StudentDalTest.cs b/AccessStudy.CoreTest/StudentDalTest.cs
new file mode 100644
index 0000000..b370c51
--- /dev/null
+++ b/AccessStudy.CoreTest/StudentDalTest.cs
@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Data.Odbc;
+
+using Xunit;
+using Shouldly;
+
+using AccessStudy.Core;
+
+namespace AccessStudy.CoreTest
+{
+ public class StudentDalTest:IDisposable
+ {
+ private readonly StudentDal _dal;
+ public StudentDalTest()
+ {
+ _dal = new StudentDal();
+ }
+
+ [Fact]
+ public void GetAll_Test()
+ {
+ var all = _dal.GetAll();
+
+ Assert.NotNull(all);
+ }
+
+ [Fact]
+ public void Add_Get_Update_Delete_Test()
+ {
+ var guidName = Guid.NewGuid().ToString().Replace("-","");
+ var student = new Student()
+ {
+ Id=0,
+ Name=guidName,
+ Age=22,
+ Address="",
+ School="Shcool",
+ };
+
+ var addResult = _dal.Add(student);
+ Assert.True(addResult);
+
+ var addStudent = _dal.Get(guidName);
+ Assert.True(addStudent.Name == student.Name && addStudent.Age == student.Age && addStudent.School == student.School);
+
+ addStudent.School = Guid.NewGuid().ToString();
+ var updateResult = _dal.Update(addStudent);
+ Assert.True(updateResult);
+
+ var delResult = _dal.Delete(addStudent.Id);
+
+ Assert.True(delResult);
+ }
+
+ public void Dispose()
+ {
+
+ }
+ }
+}