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

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; }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值