c#游戏服务器注册与登录

Onida ·
更新时间:2024-11-15
· 634 次阅读

using System; using System.Collections.Generic; using System.Linq; using System.Web; using WebAccountServer.Entity; using System.Data.SqlClient; namespace WebAccountServer.DBModel { public class AccountDBModel { #region 单例 private static AccountDBModel instance; private static object lock_object = new object(); public static AccountDBModel Instance { get { if(instance == null) { lock(lock_object) { if(instance == null) { instance = new AccountDBModel(); } } } return instance; } } #endregion private const string connStr = "Data Source=127.0.0.1;Initial Catalog=DBAccount;Persist Security Info=True;User ID=sa;Password=113911cwb"; public AccountEntity Get(int id) { using(SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); SqlCommand cmd = new SqlCommand("Account_Get", conn); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@Id", id)); using(SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)) { if(dr.HasRows && dr.Read()) { AccountEntity entity = new AccountEntity(); entity.Id = dr["Id"] is DBNull ? 0 : Convert.ToInt32(dr["Id"]); entity.UserName = dr["UserName"] is DBNull ? string.Empty : dr["UserName"].ToString(); entity.Pwd = dr["Pwd"] is DBNull ? string.Empty : dr["Pwd"].ToString(); entity.YuanBao = dr["YuanBao"] is DBNull ? 0 : Convert.ToInt32(dr["YuanBao"]); entity.LastServerId = dr["LastServerId"] is DBNull ? 0 : Convert.ToInt32(dr["LastServerId"]); entity.LastServerName = dr["LastServerName"] is DBNull ? string.Empty : dr["LastServerName"].ToString(); entity.CreateTime = dr["CreateTime"] is DBNull ? DateTime.MinValue : Convert.ToDateTime(dr["CreateTime"]); entity.UpdateTime = dr["UpdateTime"] is DBNull ? DateTime.MinValue : Convert.ToDateTime(dr["UpdateTime"]); return entity; } } } return null; } public int Register(string userName, string pwd) { int userId = -1; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); SqlCommand cmd = new SqlCommand("Account_Register", conn); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@UserName", userName)); cmd.Parameters.Add(new SqlParameter("@Pwd", pwd)); userId = Convert.ToInt32(cmd.ExecuteScalar().ToString()); } return userId; } } } using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebAccountServer.Entity { public class AccountEntity { public int Id { get; set; } public string UserName { get; set; } public string Pwd { get; set; } public int YuanBao { get; set; } public int LastServerId { get; set; } public string LastServerName { get; set; } public DateTime CreateTime { get; set; } public DateTime UpdateTime { get; set; } } }
作者:天将明兮



服务器 C# 游戏服务器

需要 登录 后方可回复, 如果你还没有账号请 注册新账号