將用戶(hù)名,密碼,用戶(hù)類(lèi)型存在數(shù)據(jù)庫(kù)中,登陸時(shí)比較輸入的值是否與數(shù)據(jù)庫(kù)相同即可。
為連江等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及連江網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站制作、成都做網(wǎng)站、連江網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
做不到,access數(shù)據(jù)庫(kù)是本地?cái)?shù)據(jù)庫(kù),不支持網(wǎng)絡(luò)訪(fǎng)問(wèn),建議你換成sql server或者mysql之類(lèi)的數(shù)據(jù)庫(kù)。
注:如果一定要遠(yuǎn)程訪(fǎng)問(wèn)access也不是不行,有兩種辦法,一個(gè)是服務(wù)器如果跟你在同一個(gè)局域網(wǎng),可以把數(shù)據(jù)庫(kù)文件夾共享出來(lái),當(dāng)做共享文件訪(fǎng)問(wèn)。另一種是在那臺(tái)服務(wù)器上裝個(gè)sql server數(shù)據(jù)庫(kù),sql server可以創(chuàng)建出鏈接數(shù)據(jù)庫(kù)鏈接到那個(gè)access.
Private?Sub?BtOk_Click(ByVal?sender?As?Object,?ByVal?e?As?System.EventArgs)?Handles?Btok.Click
Dim?con?As?OleDbConnection?=?New?OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data?Source=Admin.accdb;Persist?Security?Info=True")
Dim?com?As?OleDbCommand?=?New?OleDbCommand("select?*?from?用戶(hù)名",?con)
con.Open()
Dim?h1?As?OleDbDataReader
Dim?table?As?New?DataTable
h1?=?com.ExecuteReader()
table.Load(h1)
If?txtName.Text?=?""?Then
MsgBox("請(qǐng)輸入用戶(hù)名",?MsgBoxStyle.OkCancel?+?MsgBoxStyle.Exclamation,?"錯(cuò)誤提示")
ElseIf?txtName.Text??""?And?txtPasswd.Text?=?""?Then
MsgBox("請(qǐng)輸入密碼",?MsgBoxStyle.OkCancel?+?MsgBoxStyle.Exclamation,?"錯(cuò)誤提示")
ElseIf?table.Select("用戶(hù)名='"??txtName.Text??"'").Length??0?Then
If?table.Select("用戶(hù)名='"??txtName.Text??"'")(0)("密碼").ToString?=?txtPasswd.Text?Then
MsgBox("成功登陸",?MsgBoxStyle.OkCancel?+?MsgBoxStyle.Exclamation,?"錯(cuò)誤提示")?Then
Form1.Show()
Me.Visible?=?False
Exit?Sub
Else
MsgBox("密碼錯(cuò)誤",?MsgBoxStyle.OkCancel?+?MsgBoxStyle.Exclamation,?"錯(cuò)誤提示")
End?If
Else
MsgBox("用戶(hù)名不存在",?MsgBoxStyle.OkCancel?+?MsgBoxStyle.Exclamation,?"錯(cuò)誤提示")
Exit?Sub
End?If
table.Clear()
h1.Close()
con.Close()
End?Sub
代碼如下:
/// summary
/// 連接到窗體:通過(guò)密碼保護(hù)信息找回密碼!
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void lbl_mibao_Click(object sender, EventArgs e)
{
Getbackpwd getbackpwd = new Getbackpwd();
getbackpwd.Show();
}
/// summary
/// 當(dāng)該窗體加載時(shí)從xml文件中讀取用戶(hù)信息并加載到combox的Items中
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void Addresslist_Load(object sender, EventArgs e)
{
XmlTextReader reader = new XmlTextReader(@"E:\面向?qū)ο?-C#練習(xí)\通訊錄\address list\address list\user.xml");
while (reader.Read())
{
//if (reader.LocalName.Equals("Name") || reader.LocalName.Equals("Number"))
if (reader.LocalName.Equals("username"))
{
this.cmbUserName.Items.Add(reader.ReadString());
}
//if (reader.LocalName.Equals("Number"))
//{
// this.label2.Text += reader.ReadString() + "\n";\
//}
}
reader.Close();
}
/// summary
/// 保存用戶(hù)名到user.xml
/// /summary
//在listcontol上更改SelectedValue時(shí)執(zhí)行從數(shù)據(jù)庫(kù)讀取密碼的事件
private void cmbUserName_SelectedValueChanged(object sender, EventArgs e)
{
string username = cmbUserName.Text.Trim();
string sql = string.Format("select pwd from Admin where Username='{0}'", username);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
txtpwd.Text = (string)dataReader["pwd"];
checkBoxpwd.Checked = true;
}
}
catch
{
MessageBox.Show("數(shù)據(jù)庫(kù)操作出錯(cuò)!");
}
finally
{
DBHelper.connection.Close();
}
}
/// summary
/// 記住密碼操作
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void checkBoxpwd_Enter(object sender, EventArgs e)
{
bool check = true;
check = checkinput(cmbUserName.Text.Trim());
if ((string)cmbUserName.Text.Trim() == "")
{
MessageBox.Show("請(qǐng)輸入用戶(hù)名", "輸入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
cmbUserName.Focus();
}
else
{
if (txtpwd.Text.Trim() == "")
{
MessageBox.Show("請(qǐng)輸入密碼", "輸入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtpwd.Focus();
}
else
{
bool isValidUser = false; // 標(biāo)識(shí)是否為合法用戶(hù)
string message = ""; // 如果登錄失敗,顯示的消息提示
// 如果驗(yàn)證通過(guò),就顯示相應(yīng)的用戶(hù)窗體,并將當(dāng)前窗體設(shè)為不可見(jiàn)
if (ValidateInput())
{
// 調(diào)用用戶(hù)驗(yàn)證方法
isValidUser = ValidateUser(cmbUserName.Text, txtpwd.Text, ref message);
// 如果是合法用戶(hù),顯示相應(yīng)的窗體
if (isValidUser)
{
if (check == true)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"E:\面向?qū)ο?-C#練習(xí)\通訊錄\address list\address list\user.xml");//(@"E:\面向?qū)ο?-C#練習(xí)\通訊錄\address list\address list\user.xml");
XmlElement node = doc.CreateElement("user");
XmlNode xnode = (XmlNode)doc.CreateElement("username");
xnode.InnerText = cmbUserName.Text.Trim();
node.AppendChild(xnode);
doc.DocumentElement.InsertAfter(node, doc.DocumentElement.LastChild);
doc.Save(@"E:\面向?qū)ο?-C#練習(xí)\通訊錄\address list\address list\user.xml");
//doc.Load (@"E:\面向?qū)ο?-C#練習(xí)\通訊錄\address list\address list\user.xml");
}
}
// 如果登錄失敗,顯示相應(yīng)的消息
else
{
MessageBox.Show(message, "記住密碼失敗!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
/// summary
/// 驗(yàn)證當(dāng)前combox中內(nèi)容是否已經(jīng)存在于xml文件中
/// /summary
/// param name="text"/param
/// returns/returns
private bool checkinput(string text)
{
int count;
bool c = true;
for (count = 0; count cmbUserName.Items.Count;count ++ )
{
if (text ==(string )cmbUserName .Items [count])
{
c=false;
}
}
return c;
}
xml文件內(nèi)容如下:?xml version="1.0" encoding="utf-8"?
person
user
3個(gè)表中用戶(hù)名和密碼及權(quán)限都放到一個(gè)表如User中吧 方便些。用戶(hù)名用下拉框,選擇姓名后直接彈出權(quán)限(Lable).在下拉框SelectedIndexChanged事件中寫(xiě)即可。