using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;namespace WindowsApplication1
{
/// <summary>
/// forLogin の概要の説明です。
/// </summary>
public class frmLogin : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtPawd;
private System.Windows.Forms.TextBox txtName;
private System.Data.OleDb.OleDbConnection myCon;
private System.Data.OleDb.OleDbCommand myCom;
private System.Data.OleDb.OleDbDataAdapter myDA; /// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Button btnExit;

// スタートのページを設ける
[STAThread]
static void Main() 
{
Application.Run(new frmLogin());
} public frmLogin()
{
// Windows フォーム デザイナ サポートに必要です。
InitializeComponent();
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows フォーム デザイナで生成されたコード 
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.txtPawd = new System.Windows.Forms.TextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.btnExit = new System.Windows.Forms.Button();
this.btnLogin = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// label2
// 
this.label2.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.label2.Font = new System.Drawing.Font("MS UI Gothic", 11.25F);
this.label2.Location = new System.Drawing.Point(56, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 23);
this.label2.TabIndex = 12;
this.label2.Text = "パスワード:";
// 
// label1
// 
this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.label1.Font = new System.Drawing.Font("MS UI Gothic", 11.25F);
this.label1.Location = new System.Drawing.Point(56, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 23);
this.label1.TabIndex = 11;
this.label1.Text = " ユーザID:";
// 
// txtPawd
// 
this.txtPawd.Location = new System.Drawing.Point(184, 72);
this.txtPawd.MaxLength = 8;
this.txtPawd.Name = "txtPawd";
this.txtPawd.PasswordChar = '*';
this.txtPawd.Size = new System.Drawing.Size(120, 19);
this.txtPawd.TabIndex = 2;
this.txtPawd.Text = "";
// 
// txtName
// 
this.txtName.Location = new System.Drawing.Point(184, 32);
this.txtName.MaxLength = 8;
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(120, 19);
this.txtName.TabIndex = 1;
this.txtName.Text = "";
// 
// btnExit
// 
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.Font = new System.Drawing.Font("MS UI Gothic", 9F);
this.btnExit.Location = new System.Drawing.Point(216, 112);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(72, 24);
this.btnExit.TabIndex = 7;
this.btnExit.Text = "終了";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
// 
// btnLogin
// 
this.btnLogin.Font = new System.Drawing.Font("MS UI Gothic", 9F);
this.btnLogin.Location = new System.Drawing.Point(88, 112);
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(72, 24);
this.btnLogin.TabIndex = 6;
this.btnLogin.Text = "ログイン";
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
// 
// frmLogin
// 
this.AcceptButton = this.btnLogin;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(384, 149);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtPawd);
this.Controls.Add(this.txtName);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmLogin";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "C# 研修システム【ログイン】";
this.Load += new System.EventHandler(this.forLogin_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
///
//
private void forLogin_Load(object sender, System.EventArgs e)
{
//フォーカスをセットする
txtName.Focus();
}

//ログイン
private void btnLogin_Click(object sender, System.EventArgs e)
{
//変数を宣言する
string strSelectSQL,Sqlvalue; //ユーザIDの取得
if (txtName.Text == "")
{
//エラーメッセージを表示する
MessageBox.Show("ユーザIDは必須入力です!");
//フォーカスをセットする
txtName.Focus();
}
//パスワードDの取得
else if(txtPawd.Text == "")
{
//エラーメッセージを表示する
MessageBox.Show("パスワードは必須入力です!");
//フォーカスをセットする
txtPawd.Focus();
}
//ユーザID,パスワードの取得
else if(txtName.Text == "" || txtPawd.Text == "")
{
//エラーメッセージを表示する
MessageBox.Show("ユーザIDとパスワードは必須入力です!");
//フォーカスをセットする
txtName.Focus();
}
else
{
strSelectSQL = " SELECT pawd FROM user_lcw WHERE name = '"+txtName.Text.ToString()+"' AND pawd = '"+txtPawd.Text.ToString()+"'";
string strCon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = lcw.mdb " ;
myCon = new OleDbConnection();
myCon.ConnectionString = strCon; try
{
myCon.Open ();
myCom = new OleDbCommand ();
myCom.CommandText = strSelectSQL;
myCom.Connection  = myCon;
Sqlvalue = (string)myCom.ExecuteScalar();

if (Sqlvalue != null)
{
this.Visible = false;
frmWindows newForm = new frmWindows();
newForm.ShowDialog();
this.Close();
}
//存在しない場合
else
{
//エラーメッセージを表示する
MessageBox.Show("ユーザIDとパスワードは誤りです!");
//フォーカスをセットする
txtName.Focus();
}
}
catch(System.Exception err)
{
//エラー処理
Console.Write("Connection data is error!");
Console.Write(err.Message.ToString());
}
finally
{
if (myCon.State == ConnectionState.Open )
{
//接続を閉鎖する
myCon.Close();
}
}
}
}
//
private void btnExit_Click(object sender, System.EventArgs e)
{
//ウィンドウを閉じる
this.Close();
}
}
}

解决方案 »

  1.   

    copy dao bian yi qi kan
      

  2.   

    private void ButtonRegister_Click(object sender, System.EventArgs e)
    {
    string PassWord=this.TextBoxPasswd.Text.Trim();

    string myPassWord=Elead.Common.ProjectMisc.EncryptMD5(PassWord);
    string UserID=this.TextBoxUserId.Text.Trim();
    Elead.BusinessFacade.UserSystem theSystem = new Elead.BusinessFacade.UserSystem();
    UserInfoData theData=theSystem.GetDataByUserID(UserID);
    if(theData.Tables[UserInfoData.TABLE_NAME].Rows.Count>0)
    {
    DataRow Row = theData.Tables[UserInfoData.TABLE_NAME].Rows[0];
    string myPWD=Row[UserInfoData.USERPASSWORD_FIELD].ToString().Trim();
    //string myUserID=theData.Tables[UserInfoData.USERPASSWORD_FIELD].Rows[0].ToString().Trim();
    if(myPassWord==myPWD)
    {
    Session["UserId"]=UserID;
    Session["NickName"]=Row[UserInfoData.NICKNAME_FIELD].ToString().Trim();
    }
    else
    {
    Response.Write("<script>alert('您的密码错误,请重新输入密码!')</script>");
    TextBoxPasswd.Text="";

    }
    //Response.Write("<script> alert('请输入用户ID!') </script>");
    }
    else
    {
    Response.Write("<script>alert('您的密码错误,请重新输入密码!')</script>");
    TextBoxPasswd.Text="";

    }
    Response.Redirect(Request.Url.ToString());
        
    }
    这个应该很简单啊,我缺分啊,给点分啊,呵呵
      

  3.   

    我需要的是web页面上的用户登陆代码!
      

  4.   

    string strSQL;
    private void btnLogin_Click(object sender, System.EventArgs e)
    {
    string flag;
    string username, password, strCmd;
    username = txtName.Text.Trim();
    password = txtPswd.Text.Trim(); strCmd = "SELECT Password FROM Login WHERE UserName LIKE "+username+" AND Password LIKE "+password; ExecuteLogin(strCmd);
    flag = strSQL; if(flag != null)
    {
    lblMessage.Text = "Success!";
    }
    else
    {
    lblMessage.Text = "Error!";
    }
    strSQL = "";
    }
    //
    private void ExecuteLogin(string SQL)
    {
    string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;";
    //strCon += MapPath("Demo.mdb"); 
    strCon += @" Data Source=D:Demo.mdb";
    myCon = new OleDbConnection ();
    myCon.ConnectionString = strCon; myCon.Open ();
    myCom = new OleDbCommand ();
    myCom.CommandText = SQL;
    myCom.Connection  = myCon;
    try
    {
    strSQL=myCom.ExecuteScalar().ToString();
    }
    catch
    {
    lblMessage.Text = "Connection data is error!";
    }
    finally
    {
    if (myCon.State == ConnectionState.Open )
    {
    //接続を閉鎖する
    myCon.Close();
    }
    }
    }
      

  5.   

    我拖了2个文本框一个用户名(txtName),一个密码(password),一个按钮(btnLogin),另我把数据库放到了D盘根目录,把那个代码考上去就差不多了,其余就没什么了。
    还有using system.data和 using system.data.oledb,我调试通过了,你自己慢慢弄就会了,再不会,偶也没办法了
      

  6.   

    Plus:在页面最上面加了个标签(lblMessage),用来显示信息
      

  7.   

    usepc(usepc):谁一生下来就什么都明白的啊,我承认我是新手,当然有很多不懂的啊!
      

  8.   

    chenxing80(大漠飞鸿)
    你说得很仔细,这些我都懂的。非常感谢你来关注我的这个帖子。
    程序运行后也没有错误,只是效果出不来!
    表名:admin   里面有三个字段:id(自动编号)、name(文本)、password(文本)
    里面已经存在了一个用户了,点击按钮事件却提示:Error!