Contorller中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using G3.IDAL;
using G6.Model;
using Microsoft.AspNetCore.Mvc;
namespace G6.WeekOne.MVC.Controllers
{
public class UserController : Controller
{
public IActionResult Index()
{
List<UsersModel> list = new List<UsersModel>();
return View(list);
}
/// <summary>
/// 跳转到用户添加页面
/// </summary>
/// <returns></returns>
public IActionResult AddUser() => View();
/// <summary>
/// 添加用户
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public int AddUser(UsersModel model)
{
IUserDAL dal = Factory.CreateInstance("sql");
return dal.AddUser<UsersModel>(model);
}
}
DBConnection
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using MySql.Data.MySqlClient;
namespace G6.DAL
{
public abstract class DBConnection
{
private static string conSql= "Data Source=.;Initial Catalog=G6_WeekDB;Integrated Security=True";
private static string conMySql= "";
public static IDbConnection GetConnection(string type)
{
IDbConnection connection = null;
if (type.Equals("sql"))
{
connection = new SqlConnection(conSql);
}
else {
connection = new MySqlConnection(conMySql);
}
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
return connection;
}
}
}
IDAL中
using System;
using System.Collections.Generic;
using G6.Model;
namespace G3.IDAL
{
public interface IUserDAL
{
int AddUser<T>(T model) where T:class,new();
List<T> GetList<T>() where T : class, new();
}
}