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; }
}
}