//新增单条delete update同理 public static int Insert(TestAccounts obj) { string sql = "INSERT INTO TestAccounts (GameID,nickname) Values (@gameid,@nname);"; using (IDbConnection connection = new SqlConnection(connectionString)) { return(connection.Execute(sql, new { gameid = obj.Gameid, nname = obj.NickName })); } }
public static void Insert(TestAccounts obj) { //这是以case then的方式去检查是否存在 1是0 否(True 和false) var get = res.TableNoTracking.Any(x => x.Gameid == obj.Gameid); if (!get) { res.Insert(obj); } }
//按主键去修改,会把没有加的熟悉设置成NULL public static void update2(Context db) { TestAccounts u = new TestAccounts(); u.NickName = "葡京殷商"; u.UserID = 10; u.Gameid = 999999; db.Entry(u).State = Microsoft.EntityFrameworkCore.EntityState.Modified; db.SaveChanges(); }
//返回多表 public static void Multi() { string sql = "select * from TestAccounts where gameid=@gameid;select * from agent_user"; using (IDbConnection connection = new SqlConnection(connectionString)) { var a = connection.QueryMultiple(sql, new { gameid = 999999 }); TestAccounts b = a.Read <TestAccounts>().AsList <TestAccounts>()[0]; List <Agent_User> c = a.Read <Agent_User>().AsList(); } }
//删除 public static void Delete(Context db, int UsERid) { TestAccounts data = db._UserInfo.FirstOrDefault(b => b.UserID == UsERid) as TestAccounts; if (data != null) { // db.Remove(data); // db.Set<TestAccounts>().Remove(data); db._UserInfo.Remove(data); db.SaveChanges(); } }
//修改 //先查再改 public static void Update(Context db, TestAccounts obj) { TestAccounts data = db._UserInfo.FirstOrDefault(b => b.UserID == obj.UserID) as TestAccounts; if (data != null) { data.Gameid = obj.Gameid; data.NickName = obj.NickName; db.SaveChanges(); } }
//新增 public static void Insert(Context db, TestAccounts OBJ) { if (db._UserInfo.Any(x => x.Gameid == OBJ.Gameid)) { return; } db._UserInfo.Add(OBJ); // db.Set<TestAccounts>().Add(OBJ); // db.Add(OBJ); db.SaveChanges(); }
public static TestAccounts querySingle1() { using (IDbConnection connection = new SqlConnection(connectionString)) { string sql = "select * from TestAccounts where gameid=@gameid"; //var query = connection.Query<TestAccounts>(sql, // new { GameID = 2222227 }); // TestAccounts b= SqlMapper.Query<TestAccounts>(connection, sql, new { gameid = 2222227 }).AsList()[0]; TestAccounts obj = GetModel <TestAccounts>("TestAccounts", "gameid", "2222227"); //foreach (var item in query) //{ // Console.WriteLine(item.NickName ); //} return(obj); } }