学了那么久,是时候做一个可以让自己觉得满意的网站。但是因为要考四级,没太多时间,所以先写一部分。决定先写个用户注册系统。只是把想要复习的都用上,并没有考虑什么性能效率,也许不会
打算用到的技术有c# ,ajax,js,css。
先把所有的数据库操作写成一个类。 数据库用access
新建users.cs
打算用到的技术有c# ,ajax,js,css。
先把所有的数据库操作写成一个类。 数据库用access
新建users.cs
//引入access数据库命名空间
using System.Data.OleDb;
/// <summary>
/// users 的摘要说明
/// </summary>
public class users
{
//数据库操作;
//创建数据库链接;
public static OleDbConnection conn()
{
string myconnectionstring = ConfigurationManager.ConnectionStrings["schu"].ConnectionString;//使用web.config中的schu数据库链接
myconnectionstring += System.Web.HttpContext.Current.Server.MapPath("~/app_data/schu.mdb");//获取数据库物理地址
//在使用Server.MapPath时发现在单独的类文件中,不能直接用
OleDbConnection myconn = new OleDbConnection(myconnectionstring);
return myconn;
}
public static void CloseConn(OleDbConnection conn)
{
if (conn.State.ToString().ToUpper() == "OPEN")//state是获取数据库的链接状态
{
//如果打开了
conn.Close();//关闭数据库
conn.Dispose();//释放资源
}
else
{
conn.Dispose();
}
}
//链接数据库操作语句
private static readonly string sql_getusers = "SELECT * FROM users";//查找所有用户资料
private static readonly string sql_getsingleuser = "SELECT * FROM users WHERE uid=";//查找单一用户资料
private static readonly string sql_adduser = "INSERT INTO users(uid,name,psw,ask,answer,email,logtime) VALUES";//增加新用户初始资料
private static readonly string sql_updateuser = "UPDATE userbase SET ";//更新已注册用户资料
private static readonly string sql_deleteuser = "DELETE users WHERE uid=";//删除用户
private static readonly string sql_userlogin = "SELECT count(*) FROM users where name =";//用户登录查找
//数据库操作方法
public static string Scalar(string sql)
{//此处用于查询符合结果的一条记录
OleDbConnection myconn = conn();
string count = "-1";
OleDbCommand mycomm = new OleDbCommand(sql, myconn);
try
{
///打开链接
myconn.Open();
count = mycomm.ExecuteScalar().ToString();//返回符合结果的第一条数据
}
catch (Exception e)
{
//抛出异常
//throw new Exception(ex.Message, ex);
Console.Write(e.Message);
}
finally
{
//关闭链接
CloseConn(myconn);
}
//返回结果
return count;
}
public static int NonQuery(string sql)
{
//此处用于执行insert,delete,update的数据库语句操作;
OleDbConnection myconn = conn();
int count = -1;
OleDbCommand mycomm = new OleDbCommand(sql, myconn);
try
{
myconn.Open();//打开数据库
count = mycomm.ExecuteNonQuery();//执行sql语句并返回受影响的行数,
}
catch (OleDbException ex)
{
//抛出异常
throw new Exception(ex.Message, ex);
}
finally
{
//关闭链接
CloseConn(myconn);
}
return count;
}
public static void DdlReader(string sql, DropDownList ddl, string ddlvalue, string ddltext)
{
//用于DropDownList控件绑定数据
OleDbConnection myconn = conn();
OleDbDataReader things;//定义一个DataReader对象
OleDbCommand mycomm = new OleDbCommand(sql, myconn);
try
{
myconn.Open();//打开数据库
things = mycomm.ExecuteReader();//绑定数据
ddl.DataTextField = ddltext;//DropDownList的text属性
ddl.DataValueField = ddlvalue;//DropDownList的value属性
ddl.DataSource = things;//绑定数据源
ddl.DataBind();
}
catch (OleDbException ex)
{
//抛出异常
throw new Exception(ex.Message, ex);
}
finally
{
//关闭链接
CloseConn(myconn);
}
}
public static bool UserLogin(string username, string password)
{
//此方法用于验证用户登录
string selectuser = sql_userlogin + "'" + username + "'";
string selectusers = sql_userlogin + "'" + username + "',psw='" + password + "'";
int count = Convert.ToInt16(Scalar(selectuser));
int countup = Convert.ToInt16(Scalar(selectusers));//检索看有此用户密码是否正确
if (count == 1 && countup == 1)
{
return true;
}
else
{
return false;
}
}
public static string Uid_Random(Int32 x, Int32 y)
{//创建一个指定范围(x,y)的随机数
Random uid_ran = new Random(10000);
string uid = uid_ran.Next(x, y).ToString();
return uid;
}
public static bool AddUser(string name, string psw, string ask, string answer, string email)
{
//创建新用户的基本资料
//注册时间与随机uid由系统自动生成
int uid_min = Convert.ToInt16(Scalar("SELECT zaxiang FROM other WHERE id=1"));
int uid_max = Convert.ToInt16(Scalar("SELECT zaxiang FROM other WHERE id=2"));
string uid = Uid_Random(uid_min, uid_max);//从数据库读取随机uid的范围
string logtime = DateTime.Now.ToString("yy-mm-dd");
string insertuser = sql_adduser + "('" + uid + "','" + name + "','" + psw + "','" + ask + "','" + answer + "','" + email + "','" + logtime + "')";
//检查插入是否成功
if (NonQuery(insertuser) > 0)
{
return true;
}
return false;
}
public static bool DelUser(string uid)
{
//根据传入的uid删除已注册用户资料
string deleteuser = sql_deleteuser + "'" + uid + "'";
//检查删除是否成功
if (NonQuery(deleteuser) > 0)
{
return true;
}
return false;
}
public static bool UpUser(string sex, string birthday, string province, string city, string qq, string msn, string face, string words)
{
//写入已注册用户的详细资料,此方法不做更新用,只用在用户第一次注册时;
string updateuser = sql_updateuser + "sex='" + sex + "',birthday='" + birthday + "',province='" + province + "',city='" + city + "',qq='" + qq + "',msn='" + msn + "',face='" + face + "',words='" + words + "'";
if (NonQuery(updateuser) > 0)
{
return true;
}
return false;
}
}
暂时写那么多,还不太会用博客,感觉痛苦,下次给出验证码
using System.Data.OleDb;
/// <summary>
/// users 的摘要说明
/// </summary>
public class users
{
//数据库操作;
//创建数据库链接;
public static OleDbConnection conn()
{
string myconnectionstring = ConfigurationManager.ConnectionStrings["schu"].ConnectionString;//使用web.config中的schu数据库链接
myconnectionstring += System.Web.HttpContext.Current.Server.MapPath("~/app_data/schu.mdb");//获取数据库物理地址
//在使用Server.MapPath时发现在单独的类文件中,不能直接用
OleDbConnection myconn = new OleDbConnection(myconnectionstring);
return myconn;
}
public static void CloseConn(OleDbConnection conn)
{
if (conn.State.ToString().ToUpper() == "OPEN")//state是获取数据库的链接状态
{
//如果打开了
conn.Close();//关闭数据库
conn.Dispose();//释放资源
}
else
{
conn.Dispose();
}
}
//链接数据库操作语句
private static readonly string sql_getusers = "SELECT * FROM users";//查找所有用户资料
private static readonly string sql_getsingleuser = "SELECT * FROM users WHERE uid=";//查找单一用户资料
private static readonly string sql_adduser = "INSERT INTO users(uid,name,psw,ask,answer,email,logtime) VALUES";//增加新用户初始资料
private static readonly string sql_updateuser = "UPDATE userbase SET ";//更新已注册用户资料
private static readonly string sql_deleteuser = "DELETE users WHERE uid=";//删除用户
private static readonly string sql_userlogin = "SELECT count(*) FROM users where name =";//用户登录查找
//数据库操作方法
public static string Scalar(string sql)
{//此处用于查询符合结果的一条记录
OleDbConnection myconn = conn();
string count = "-1";
OleDbCommand mycomm = new OleDbCommand(sql, myconn);
try
{
///打开链接
myconn.Open();
count = mycomm.ExecuteScalar().ToString();//返回符合结果的第一条数据
}
catch (Exception e)
{
//抛出异常
//throw new Exception(ex.Message, ex);
Console.Write(e.Message);
}
finally
{
//关闭链接
CloseConn(myconn);
}
//返回结果
return count;
}
public static int NonQuery(string sql)
{
//此处用于执行insert,delete,update的数据库语句操作;
OleDbConnection myconn = conn();
int count = -1;
OleDbCommand mycomm = new OleDbCommand(sql, myconn);
try
{
myconn.Open();//打开数据库
count = mycomm.ExecuteNonQuery();//执行sql语句并返回受影响的行数,
}
catch (OleDbException ex)
{
//抛出异常
throw new Exception(ex.Message, ex);
}
finally
{
//关闭链接
CloseConn(myconn);
}
return count;
}
public static void DdlReader(string sql, DropDownList ddl, string ddlvalue, string ddltext)
{
//用于DropDownList控件绑定数据
OleDbConnection myconn = conn();
OleDbDataReader things;//定义一个DataReader对象
OleDbCommand mycomm = new OleDbCommand(sql, myconn);
try
{
myconn.Open();//打开数据库
things = mycomm.ExecuteReader();//绑定数据
ddl.DataTextField = ddltext;//DropDownList的text属性
ddl.DataValueField = ddlvalue;//DropDownList的value属性
ddl.DataSource = things;//绑定数据源
ddl.DataBind();
}
catch (OleDbException ex)
{
//抛出异常
throw new Exception(ex.Message, ex);
}
finally
{
//关闭链接
CloseConn(myconn);
}
}
public static bool UserLogin(string username, string password)
{
//此方法用于验证用户登录
string selectuser = sql_userlogin + "'" + username + "'";
string selectusers = sql_userlogin + "'" + username + "',psw='" + password + "'";
int count = Convert.ToInt16(Scalar(selectuser));
int countup = Convert.ToInt16(Scalar(selectusers));//检索看有此用户密码是否正确
if (count == 1 && countup == 1)
{
return true;
}
else
{
return false;
}
}
public static string Uid_Random(Int32 x, Int32 y)
{//创建一个指定范围(x,y)的随机数
Random uid_ran = new Random(10000);
string uid = uid_ran.Next(x, y).ToString();
return uid;
}
public static bool AddUser(string name, string psw, string ask, string answer, string email)
{
//创建新用户的基本资料
//注册时间与随机uid由系统自动生成
int uid_min = Convert.ToInt16(Scalar("SELECT zaxiang FROM other WHERE id=1"));
int uid_max = Convert.ToInt16(Scalar("SELECT zaxiang FROM other WHERE id=2"));
string uid = Uid_Random(uid_min, uid_max);//从数据库读取随机uid的范围
string logtime = DateTime.Now.ToString("yy-mm-dd");
string insertuser = sql_adduser + "('" + uid + "','" + name + "','" + psw + "','" + ask + "','" + answer + "','" + email + "','" + logtime + "')";
//检查插入是否成功
if (NonQuery(insertuser) > 0)
{
return true;
}
return false;
}
public static bool DelUser(string uid)
{
//根据传入的uid删除已注册用户资料
string deleteuser = sql_deleteuser + "'" + uid + "'";
//检查删除是否成功
if (NonQuery(deleteuser) > 0)
{
return true;
}
return false;
}
public static bool UpUser(string sex, string birthday, string province, string city, string qq, string msn, string face, string words)
{
//写入已注册用户的详细资料,此方法不做更新用,只用在用户第一次注册时;
string updateuser = sql_updateuser + "sex='" + sex + "',birthday='" + birthday + "',province='" + province + "',city='" + city + "',qq='" + qq + "',msn='" + msn + "',face='" + face + "',words='" + words + "'";
if (NonQuery(updateuser) > 0)
{
return true;
}
return false;
}
}