1、conn(DAO)项目引用Dapper.dll (测试版本1.50.2.0)
2、StudentDAO.cs 代码附上
public class StudentDAO { public List<StudentModel> SearchStudentList() { List<StudentModel> Studentlist = new List<StudentModel>(); using (IDbConnection conn = SqlConn.GetSqlConnection()) { string sqlcommandText = "SELECT * FROM dbo.Students "; return Studentlist = conn.Query<StudentModel>(sqlcommandText, null).ToList(); } } public bool AddList(string name) { using (IDbConnection conn = SqlConn.GetSqlConnection()) { string sqlcommandText = "insert into dbo.Students values(@Name)"; return conn.Execute(sqlcommandText, new { Name = name }) > 0 ? true : false; } } public bool UpdateList(int id, string name) { using (IDbConnection conn = SqlConn.GetSqlConnection()) { string sqlcommandText = "Update dbo.Students set Name = @name where Id = @Id"; return conn.Execute(sqlcommandText, new { Id = id, Name = name }) > 0 ? true : false; } } public bool DeleteList(int id) { using (IDbConnection conn = SqlConn.GetSqlConnection()) { string sqlcommandText = "Delete dbo.Students where Id = @Id"; return conn.Execute(sqlcommandText, new { Id = id }) > 0 ? true : false; } } }
Conn.cs 代码附上 (配置数据库字符串)
public static class SqlConn { //配置数据库字符串 private static string sqlstr = ConfigurationManager.ConnectionStrings["SqlServerDB"].ConnectionString; //连接数据库 public static SqlConnection GetSqlConnection() { SqlConnection conn = new SqlConnection(sqlstr); conn.Open(); return conn; } }
StudentModel.cs 代码附上 (数据库一样的结构,这里就不贴出来)
public class StudentModel { public int Id { get; set; } public string Name { get; set; } }
app.config 配置连接字符串
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <connectionStrings> <add name="SqlServerDB" connectionString="Data Source=DESKTOP-V0V6TJ9MSSQLSERVER2016;Initial Catalog=TextDB;Persist Security Info=True;User ID=sa;Password=123;Max Pool Size=512;Connection Timeout=60"/> </connectionStrings> </configuration>
Program.cs 代码附上(测试流程)
class Program { static void Main(string[] args) { //初始状态 int status = 0; //操作 while (status >= 0) { Console.WriteLine(""); Console.WriteLine("请输入指令:1、查询 2、增加 3、修改 4、删除 5、退出 !"); int num = Convert.ToInt32(Console.ReadLine()); switch (num) { case 1: //查询方法 SearchList(); break; case 2: Console.WriteLine("请输入姓名!"); string strName = Console.ReadLine(); //增加方法 AddList(strName); break; case 3: //修改方法 Console.WriteLine("请输入要修改的Id!"); int id = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入新名字"); string name = Console.ReadLine(); UpdateList(id, name); break; case 4: //删除方法 Console.WriteLine("请输入要删除的Id!"); int idcopy = Convert.ToInt32(Console.ReadLine()); DeleteList(idcopy); break; case 5: status = -1; break; default: Console.WriteLine("指令输入错误!"); break; } } Environment.Exit(0); } static StudentDAO sddao = new StudentDAO(); public static void SearchList() { //查询数据 List<StudentModel> ListPerson = sddao.SearchStudentList(); foreach (StudentModel p in ListPerson) { Console.WriteLine(p.Id + p.Name); } } public static void AddList(string name) { //添加数据 sddao.AddList(name); } public static void UpdateList(int id, string name) { //修改数据 if (sddao.UpdateList(id, name)) { Console.WriteLine("修改成功!"); } else { Console.WriteLine("修改失败!"); }; } public static void DeleteList(int id) { //删除数据 if (sddao.DeleteList(id)) { Console.WriteLine("删除成功!"); } else { Console.WriteLine("删除失败!"); }; } }