作者neo5277 (I am an agent of chaos)
看板C_Sharp
标题[问题] 资料库帐号验证的问题请教
时间Tue Dec 2 15:31:09 2014
最近练习登入的部分
主要功能是要
检验有无重复的帐号不过总是会有小问题程式码如下
------------------------------------------------------------
namespace LOGIN
{
public partial class FrmLogin : Form
{
byte[] pic = new byte[2];
bool manager;
public FrmLogin()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
if (manager == false)
{
try
{
using (SqlConnection conn = new
SqlConnection(Settings.Default.ILWCon))
{
using (SqlCommand command = new SqlCommand())
{
string UserName = textBox1.Text;
string Password = FormsAuthentication.
HashPasswordForStoringInConfigFile
(this.textBox2.Text,"sha1");
if (UserName == "" || Password == "")
//=====================
{
MessageBox.Show("请输入帐号(信箱),及密码");
}
else
{
System.Security.Cryptography.RNGCryptoServiceProvider rng = new
System.Security.Cryptography.RNGCryptoServiceProvider();
byte[] buf = new byte[15];//用一个阵列来存取使用者输入的密码
rng.GetBytes(buf); //要将在密码编译方面随机位元组填入的阵列。
string salt = Convert.ToBase64String(buf);
Password =FormsAuthentication.HashPasswordForStoringInConfigFile(Password + salt,
"sha1");
command.CommandText = "Insert into Member(MemEmail, MemPW, Salt) values
(@MemEmail, @MemPW, @Salt)";
command.Parameters.Add("@MemEmail",SqlDbType.NVarChar, 50).Value = UserName;
command.Parameters.Add("@MemPW",SqlDbType.NVarChar, 80).Value = Password;
command.Parameters.Add("@Salt",SqlDbType.NVarChar, 50).Value = salt;
觉得问题出在这里可是不太知道是哪里打错了
conn.Open();
SqlCommand bb = new SqlCommand("Select * FromMember Where Mememail=
'" + this.textBox1.Text + "'", conn);
SqlDataReader ha = bb.ExecuteReader();
if (ha.Read())
{
MessageBox.Show("※请换一个");
this.close();
}
else {
command.ExecuteNonQuery();
MessageBox.Show("Insert successfully");
}
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
try
{
using (SqlConnection conn = new SqlConnection(Settings.Default.ILWCon))
{
using (SqlCommand command = new SqlCommand())
{
string UserName = textBox1.Text;
string Password =FormsAuthentication.HashPasswordForStoringIn
ConfigFile(this.textBox2.Text,"sha1");
if (UserName == "" || Password == "")
//=====================
{
MessageBox.Show("请输入帐号(信箱),及密码");
}
else
{
System.Security.Cryptography.RNGCryptoServiceProvider rng = new
System.Security.Cryptography.RNGCryptoServiceProvider();
byte[] buf = new byte[15];
//用一个阵列来存取使用者输入的密码
rng.GetBytes(buf); //要将在密码编译方面随机位元组填入的阵列。
string salt = Convert.ToBase64String(buf);
//保密方法
Password =FormsAuthentication.HashPasswordForStoringInConfigFile(Password + salt,
"sha1");
command.CommandText = "Insert into Manager
(EmpAccount, EmpPW,EmpSalt) values (@EmpAccount, @EmpPW, @EmpSalt) command.Connection = conn;
command.Parameters.Add("@EmpAccount", SqlDbType.NVarChar, 50).Value =
UserName;
command.Parameters.Add("@EmpPW",SqlDbType.NVarChar, 50).Value = Password;
command.Parameters.Add("@EMPSalt",SqlDbType.NVarChar, 50).Value = salt;
conn.Open();
SqlCommand bb = new SqlCommand("Select * FromMember Where Mememail=
'" + this.textBox1.Text + "'", conn);
SqlDataReader ha = bb.ExecuteReader();
if (ha.Read())
{
MessageBox.Show("※请换一个");
}
else
{
command.ExecuteNonQuery();
MessageBox.Show("Insert successfully");
}
bb.Dispose();
ha.Dispose();
}
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
---------------------------
希望各位可以指点一下 感谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 125.227.255.79
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1417505473.A.BC2.html
1F:推 Peruheru: 纯粹建议:你应该告诉大家你遇到的"小问题"是什呢情况 12/02 16:17
抱歉 应该要再详细点
F5後 我点选 button3 (注册用) 如果text 不为空值则连线资料库
然後比对 资料库中member表中有无相同资料 如果有则显现 "请换一个"
然後问题在於 我是使用 sqldatareader
他一直显示
ExecuteReader 没有连线 没有connection 不过我一早用using 写完connection
字串了 所以一直卡在这里
是我开连线的位置不对吗 ?
※ 编辑: neo5277 (125.227.255.79), 12/02/2014 17:04:09
2F:推 CrazyAngel: 先试着修正一些问题 1. command没有指定connection 12/02 18:09
3F:→ CrazyAngel: 2. sqldatareader用完要close 12/02 18:10
好的 我去试试看 谢谢你
※ 编辑: neo5277 (125.227.255.79), 12/02/2014 19:49:30
4F:推 Litfal: 应该先修正那个bb,用使用者输入资料串的SqlCommand... 12/06 12:44