hasrows

很明显,从字面意思理解就可以了
HasRows表示的是从数据库读出来的数据集dataReader中是否存在数据,它的作用是用来判断数据集是否为空,执行的结果为 true/false
Read才是真正的读数据,采用的是一条一条不可回的顺序读法,每执行一次Read,dataReader的当前数据往后移了,所以要通过循环来读取比如:
while(dataReader.Read() != null)
{
        .... ....
}
protected void Register_Click(object sender, EventArgs e) { { if (TextBox_emailreg.Text == "") { Response.Write("<script>window.alert('没有输入邮箱');</script>"); return; } else if (TextBox_name.Text == "") { Response.Write("<script>window.alert('没有输入用户名');</script>"); return; } else if (TextBox_password.Text == "") { Response.Write("<script>window.alert('没有输入密码');</script>"); return; } SqlConnection conn = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=yijia;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT *FROM user_info where email = '"+ TextBox_emailreg.Text.Trim() +"'", conn); SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); int x = 0; if (sdr.HasRows) { Response.Write("<script>window.alert('邮箱已经存在,不可以重复注册');</script>"); return; } else if (TextBox_yanzhengma.Text != (String)Session["yanzheng"]) { Label1.ForeColor = System.Drawing.Color.Red; Label1.Text = "验证码不正确,请检查你的邮件"; return; } else if (x == 0) { sdr.Close(); string time = DateTime.Now.ToString(); String MD5 = public_function.Md5Hash(TextBox_password.Text); cmd.CommandText = "insert into user_info (email,name,password,time) values ('" + TextBox_emailreg.Text + "','" + TextBox_name.Text + "','" + MD5 + "','" + time + "')"; string rows_effedted = cmd.ExecuteNonQuery().ToString(); conn
06-09
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace Demo { public partial class deng_ru : Form { public deng_ru() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //数据库链接的配置 string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Desktop\Demo\Test.accdb"; using (OleDbConnection connection = new OleDbConnection(connectionString)) { connection.Open(); //打开数据库连接 string username = textBox1.Text; //待验证的用户名 string password = textBox2.Text; //待验证的密码 // 获取需要查询的字段的值 string fieldValue = textBox1.Text; //查询语句,使用参数化查询,避免SQL注入攻击 string sql = "SELECT * FROM USERS WHERE User_ID=@username AND password=@password"; //使用using指令,确保在代码执行完毕后,command对象会被正确释放 using (OleDbCommand command = new OleDbCommand(sql, connection)) { //为查询语句中的参数赋值 command.Parameters.AddWithValue("@username", username); command.Parameters.AddWithValue("@password", password); //使用ExecuteReader方法执行查询语句,并将查询结果保存到reader对象中 using (OleDbDataReader reader = command.ExecuteReader()) { if (reader.HasRows) //登录成功的处理逻辑 { reader.Read(); //读取查询结果 //获取查询结果的第一个字段(ID)的值,即ID int id = reader.GetInt32(0); //获取名为student的窗口 deng_ru studentForm = Application.OpenForms["student"] as deng_ru; if (studentForm != null) //确保窗口存在 { studentForm.SetID(id); //调用窗口的SetID方法,将ID传递给它 } //从 登入界面 跳转到 学生界面 student xs = new student(); this.Hide(); xs.Show(); } else //登录失败的处理逻辑 { MessageBox.Show("登入失败,请重新输入!"); } } } } } } }C#报错找不到SetID的定义
05-24
protected void zhaohuimi_Click(object sender, EventArgs e) { if (zhaohuimi.Text == "忘记密码") { Label4.Text = "请输入新密码:"; Label5.Visible = true; TextBox_yanzhengma.Visible = true; zhaohuimi.Text = "确认修改"; Button_yanzhengma.Visible = true; Login.Text = "去登录"; goreg.Visible = false; } else { if (TextBox_email.Text == "") { Response.Write("<script>window.alert('没有输入邮箱');</script>"); return; } else if (TextBox_password.Text == "") { Response.Write("<script>window.alert('没有输入密码');</script>"); return; } else if (TextBox_yanzhengma.Text != (String)Session["yanzheng"]) { Label1.ForeColor = System.Drawing.Color.Red; Label1.Text = "验证码不正确,请检查你的邮件"; return; } SqlConnection conn = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=yijia;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM user_info where email = '" + TextBox_email.Text.Trim() + "'", conn); SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); int x = 0; if (sdr.HasRows) { sdr.Close(); string time = DateTime.Now.ToString(); String MD5 = public_function.Md5Hash(TextBox_password.Text); SqlCommand sqlCmd = new SqlCommand("update user_info set password ='" + MD5 + "' where email='" + TextBox_email.Text + "'", conn); int rowCount = sqlCmd.ExecuteNonQuery(); conn.Close(); conn.Dispose(); Response.Write("<script>window.alert('密码修改成功,可以登录了');</script>"); } else { Response.Write("<script>window.alert('该邮箱还没有注册');</script>"); } Label4.Text = "请输入密码:"; Label5.Visible = false ; TextBox_yanzhengma.Visible = false ; zhaohuimi.Text = "忘记密码"; Button_yanzhengma.Visible = false ; Login.Text = "登录"; goreg.Visible = true ; Label1.Text = ""; } }
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值