你最好封装一个简单的数据库访问类,如: public class DataAccess { private OleDbConnection oleDbConnection; public DataAccess() { // oleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=se.mdb;"); if (oleDbConnection.State != ConnectionState.Open) { oleDbConnection.Open(); } } public int ExecuteSql(string sql) { OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection); return cmd.ExecuteNonQuery(); } public int ExecuteSql(string sql, ref OleDbParameter[] oleDbParameters) { OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection); cmd.Parameters.AddRange(oleDbParameters); return cmd.ExecuteNonQuery(); } 然后写sql语句,调用里面的方法执行就可以了。
public DataSet ExecuteDataSet(string sql) { OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection); OleDbDataAdapter oda = new OleDbDataAdapter(cmd); DataSet ds = new DataSet(); oda.Fill(ds); return ds; } } 然后就是调用逻辑了。这是注册部分。 重置按钮就更简单了,直接把几个textbox.Text="";即可。
这是登录接着你那个写的 if (tb_id.Text == "" || tb_pwd.Text == "") { Response.Write("输入登录信息不完整,请重新输入!"); } SqlConnection con = new SqlConnection("Server = ygu-PC\\SQLEXPRESS;User Id = YGU-PC\\ygu;DataBase = rcloud"); SqlConmmand cmd = new SqlCommand(string.Format("SELECT COUNT(*)FROM rcloud WHERE username = '{0}'AND userpassword = '{1}'", this.tb_id.Text.Trim(), this.tb_pwd.Text.Trim()), con); int i = int.Parse(cmd.ExecuteScalar().ToString()); if (i > 0)
Response.Redirect("主页.aspx"); else { Response.Write("<script>alert('信息错误,登录失败!')</script>"); } con.Close();注册SqlConnection con = new SqlConnection("Server = ygu-PC\\SQLEXPRESS;User Id = YGU-PC\\ygu;DataBase = rcloud"); if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" &7 TextBox6.Text != "") { string sql=""; //要执行的 sql 语句 SqlConmmand cmd = new SqlCommand(sql, con);//同登录 int i = int.Parse(cmd.ExecuteNonQuery().ToString()); if (i > 0) { Response.Write("<script>alert('恭喜您,注册成功!')</script>"); }
public class DataAccess
{
private OleDbConnection oleDbConnection; public DataAccess()
{
//
oleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=se.mdb;");
if (oleDbConnection.State != ConnectionState.Open)
{
oleDbConnection.Open();
}
}
public int ExecuteSql(string sql)
{
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection);
return cmd.ExecuteNonQuery();
} public int ExecuteSql(string sql, ref OleDbParameter[] oleDbParameters)
{
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection);
cmd.Parameters.AddRange(oleDbParameters);
return cmd.ExecuteNonQuery();
}
然后写sql语句,调用里面的方法执行就可以了。
{
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection);
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
return ds;
}
}
然后就是调用逻辑了。这是注册部分。
重置按钮就更简单了,直接把几个textbox.Text="";即可。
{
Response.Write("输入登录信息不完整,请重新输入!");
}
SqlConnection con = new SqlConnection("Server = ygu-PC\\SQLEXPRESS;User Id = YGU-PC\\ygu;DataBase = rcloud");
SqlConmmand cmd = new SqlCommand(string.Format("SELECT COUNT(*)FROM rcloud WHERE username = '{0}'AND userpassword = '{1}'", this.tb_id.Text.Trim(), this.tb_pwd.Text.Trim()), con);
int i = int.Parse(cmd.ExecuteScalar().ToString()); if (i > 0)
Response.Redirect("主页.aspx");
else
{
Response.Write("<script>alert('信息错误,登录失败!')</script>");
}
con.Close();注册SqlConnection con = new SqlConnection("Server = ygu-PC\\SQLEXPRESS;User Id = YGU-PC\\ygu;DataBase = rcloud");
if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" &7 TextBox6.Text != "")
{
string sql=""; //要执行的 sql 语句
SqlConmmand cmd = new SqlCommand(sql, con);//同登录
int i = int.Parse(cmd.ExecuteNonQuery().ToString());
if (i > 0)
{
Response.Write("<script>alert('恭喜您,注册成功!')</script>");
}
}
else
{
Response.Write("<script>alert('用户名或密码不能为空!')</script>");
}
大概就是这个样子的吧,刚接触可以用这种方法来做,以后还是建议按照1楼的方法,封装一个简单的数据库访问类,
http://www.cnblogs.com/insus/archive/2013/05/01/3052604.html