[問題] 資料庫帳號驗證的問題請教
看板C_Sharp (C#)作者neo5277 (I am an agent of chaos)時間10年前 (2014/12/02 15:31)推噓3(3推 0噓 1→)留言4則, 3人參與討論串1/1
最近練習登入的部分
主要功能是要
檢驗有無重複的帳號不過總是會有小問題程式碼如下
------------------------------------------------------------
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://www.ptt.cc/bbs/C_Sharp/M.1417505473.A.BC2.html
推
12/02 16:17, , 1F
12/02 16:17, 1F
抱歉 應該要再詳細點
F5後 我點選 button3 (註冊用) 如果text 不為空值則連線資料庫
然後比對 資料庫中member表中有無相同資料 如果有則顯現 "請換一個"
然後問題在於 我是使用 sqldatareader
他一直顯示
ExecuteReader 沒有連線 沒有connection 不過我一早用using 寫完connection
字串了 所以一直卡在這裡
是我開連線的位置不對嗎 ?
※ 編輯: neo5277 (125.227.255.79), 12/02/2014 17:04:09
推
12/02 18:09, , 2F
12/02 18:09, 2F
→
12/02 18:10, , 3F
12/02 18:10, 3F
好的 我去試試看 謝謝你
※ 編輯: neo5277 (125.227.255.79), 12/02/2014 19:49:30
推
12/06 12:44, , 4F
12/06 12:44, 4F
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章