C#登陆验证
答案:2 悬赏:70 手机版
解决时间 2021-03-25 13:56
- 提问者网友:鐵馬踏冰河
- 2021-03-25 02:08
C#登陆验证
最佳答案
- 五星知识达人网友:低音帝王
- 2021-03-25 02:19
//登录
private void button1_Click(object sender, EventArgs e)
{
//查询
INname = textBox1.Text;
INpad = textBox2.Text;
SqlConnection sc = new SqlConnection("data source=.;initial catalog=Library_Manager;user id=sa;password=123;");
sc.Open();
SqlCommand logincon = new SqlCommand("select * from userss where 读者编号='" + textBox1.Text.Trim() + "'", sc);
SqlDataReader loginer = logincon.ExecuteReader();
while (loginer.Read())
{
//loginer.Read();
if ((textBox2.Text).Equals(loginer.GetString(1).Trim()))
{
sc.Close();
MessageBox.Show("登录成功!");
// this.Hide();
// new Book(this).Show(this);
return;
}
else
{
MessageBox.Show("密码错误!");
textBox1.Text = "";
textBox1.Focus();
return;
}
}
MessageBox.Show("无此用户名,请注册后再登陆!");
textBox2.Text = "";
textBox1.Text = "";
textBox1.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//添加注册
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0 && textBox2.Text.Length != 0)
{
SqlConnection sc = new SqlConnection("data source=.;initial catalog=Cexercise;user id=sa;password=123;");
try
{
sc.Open();
SqlCommand scd = new SqlCommand("select * from userss where name='" + textBox1.Text.Trim() + "'", sc);
SqlDataReader sdr = scd.ExecuteReader();
if (!sdr.HasRows)
{
sdr.Close();
SqlCommand scon = new SqlCommand("insert into userss values('" + textBox1.Text + "','" + textBox2.Text + "')", sc);
int res = scon.ExecuteNonQuery();
MessageBox.Show("注册成功," + textBox1.Text + "欢迎你!");
}
else
{
MessageBox.Show("注册失败,已经有该用户,请重新输入注册信息^ _^");
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show("操作失败,请检查网络连接是否正常!");
}
finally
{
if (sc.State == ConnectionState.Open)
sc.Close();
}
}
}
//查询
private void button3_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection("data source=127.0.0.1;initial catalog=Cexercise;user id=sa;password=123;");
sc.Open();
SqlCommand scd = new SqlCommand("select* from userss", sc);
SqlDataReader sdr = scd.ExecuteReader();
try
{
do
{
sdr.Read();
ONname = sdr.GetString(0);
ONpad = sdr.GetString(1);
} while (!textBox1.Text.Equals(sdr.GetString(0).Trim()));
textBox3.Text = sdr.GetString(0);
textBox4.Text = sdr.GetString(1);
}
catch (Exception ee)
{
MessageBox.Show("该用户名不存在,请重新输入用户名!", "消息框", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
sdr.Close();
sc.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
//查询
INname = textBox1.Text;
INpad = textBox2.Text;
SqlConnection sc = new SqlConnection("data source=.;initial catalog=Library_Manager;user id=sa;password=123;");
sc.Open();
SqlCommand logincon = new SqlCommand("select * from userss where 读者编号='" + textBox1.Text.Trim() + "'", sc);
SqlDataReader loginer = logincon.ExecuteReader();
while (loginer.Read())
{
//loginer.Read();
if ((textBox2.Text).Equals(loginer.GetString(1).Trim()))
{
sc.Close();
MessageBox.Show("登录成功!");
// this.Hide();
// new Book(this).Show(this);
return;
}
else
{
MessageBox.Show("密码错误!");
textBox1.Text = "";
textBox1.Focus();
return;
}
}
MessageBox.Show("无此用户名,请注册后再登陆!");
textBox2.Text = "";
textBox1.Text = "";
textBox1.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//添加注册
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0 && textBox2.Text.Length != 0)
{
SqlConnection sc = new SqlConnection("data source=.;initial catalog=Cexercise;user id=sa;password=123;");
try
{
sc.Open();
SqlCommand scd = new SqlCommand("select * from userss where name='" + textBox1.Text.Trim() + "'", sc);
SqlDataReader sdr = scd.ExecuteReader();
if (!sdr.HasRows)
{
sdr.Close();
SqlCommand scon = new SqlCommand("insert into userss values('" + textBox1.Text + "','" + textBox2.Text + "')", sc);
int res = scon.ExecuteNonQuery();
MessageBox.Show("注册成功," + textBox1.Text + "欢迎你!");
}
else
{
MessageBox.Show("注册失败,已经有该用户,请重新输入注册信息^ _^");
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show("操作失败,请检查网络连接是否正常!");
}
finally
{
if (sc.State == ConnectionState.Open)
sc.Close();
}
}
}
//查询
private void button3_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection("data source=127.0.0.1;initial catalog=Cexercise;user id=sa;password=123;");
sc.Open();
SqlCommand scd = new SqlCommand("select* from userss", sc);
SqlDataReader sdr = scd.ExecuteReader();
try
{
do
{
sdr.Read();
ONname = sdr.GetString(0);
ONpad = sdr.GetString(1);
} while (!textBox1.Text.Equals(sdr.GetString(0).Trim()));
textBox3.Text = sdr.GetString(0);
textBox4.Text = sdr.GetString(1);
}
catch (Exception ee)
{
MessageBox.Show("该用户名不存在,请重新输入用户名!", "消息框", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
sdr.Close();
sc.Close();
}
}
全部回答
- 1楼网友:封刀令
- 2021-03-25 02:31
如果用oracle的话
你定义两个字符串
比如
string zhanghao,mima;
OracleConnection a = new OracleConnection("server=orcl;user id=dszzp;password=1234");
a.Open();
//server是你的服务器,user id是你的账号,password是你的密码
OracleCommand b = new OracleCommand(""select * from dept where zhanghao='" + textName.Text.ToString().Trim() + "' and mima='" + textPassword.Text + "'", a);
//dept是你的表,一般来说的话,这张表就是两个属性啦
OracleDataReader c = b.ExecuteReader();
while (c.Read())
{
nextform haha = new nextform();
haha1.Show();
this.hide()
break;
}
你定义两个字符串
比如
string zhanghao,mima;
OracleConnection a = new OracleConnection("server=orcl;user id=dszzp;password=1234");
a.Open();
//server是你的服务器,user id是你的账号,password是你的密码
OracleCommand b = new OracleCommand(""select * from dept where zhanghao='" + textName.Text.ToString().Trim() + "' and mima='" + textPassword.Text + "'", a);
//dept是你的表,一般来说的话,这张表就是两个属性啦
OracleDataReader c = b.ExecuteReader();
while (c.Read())
{
nextform haha = new nextform();
haha1.Show();
this.hide()
break;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯