ASP.NET
查询并展示:
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa ;pwd=; database=school";
try
{
con.Open();
}catch
{
Response.Write("数据库连接失败!");
con.Close();
}
if(con.State==ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select count(*) from tb_xinxi where stu_num='" + TextBox6.Text + "'";
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i == 1)
{
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con;
cmd2.CommandType = CommandType.Text;
cmd2.CommandText = "select * from tb_xinxi where stu_num='" + TextBox6.Text + "'";
SqlDataReader dr = cmd2.ExecuteReader();
while(dr.Read())
{
TextBox1.Text = (dr.GetString(0));
TextBox2.Text = (dr.GetString(1));
TextBox3.Text = (dr.GetString(2));
TextBox4.Text =Convert.ToString( (dr.GetValue(3)));
TextBox5.Text = (dr.GetString(4));
}
}
}
else
{
Response.Write("<script>alert('记录不存在!')</script>");
TextBox1.Focus();
}
登录:
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);database=school;trusted_connection=true";
try
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select count(*) from tb_user where user_pwd='" + TextBox1.Text + "' and user_pwd='" + TextBox2.Text + "'";
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i == 0)
{
Response.Write("<script>alert('用户名错误,请重新出入!')</script>");
TextBox1.Text = "";
TextBox2.Text = "";
}
else
{
Response.Redirect("chaxun.aspx");
}
}
catch {
Response.Write("数据库连接失败123!");
con.Close();
}
注册:
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);database=school;trusted_connection=true";
try
{
if(TextBox1.Text!=""&&TextBox2.Text!="")
{
con.Open();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = con;
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "select count(*) from tb_user where user_name='" + TextBox1.Text + "'and user_pwd='" + TextBox2.Text + "'";
int i = Convert.ToInt32(cmd1.ExecuteScalar());
if (i == 1)
{
Response.Write("<script>alert('用户已经注册,请重新出入!')</script>");
TextBox1.Text = "";
TextBox2.Text = "";
}
else
{
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con;
cmd2.CommandType = CommandType.Text;
cmd2.CommandText = "insert into tb_user values('"+TextBox1.Text+ "','" + TextBox2.Text + "')";
cmd2.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('注册成功!')</script>");
//TextBox1.Text = "";
//TextBox2.Text = "";
}
}
}
catch
{
Response.Write("数据库连接失败!");
con.Close();
}