ขอโค้ด ล็อกอิน Login Visual C# .NET ใช้ SQL 2008 เป็นฐานข้อมูลค๊า
ต่อจากข้างบนค่ะ
ใช้ Visual C# 2005 ค่ะ
กับ SQL 2008
Date :
2010-04-28 09:09:49
By :
oomi2830
Code (C#)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class LoginControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["User"] != null)
{
Response.Write("session has valu");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ฐานข้อมูลของคุณ);
try
{
conn.Open();
string SqlText = "SELECT UserID, Username, Position FROM [User] WHERE [email protected] AND Password[email protected] ";
SqlCommand cmd = new SqlCommand(SqlText, conn);
cmd.Parameters.AddWithValue("@Username", TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@Password", TextBox2.Text.Trim());
SqlDataReader dr = cmd.ExecuteReader();
try
{
if (dr != null && dr.HasRows == true)
{
// Response.Write("login true");
if (dr.Read())
{
Session["User"] = dr["Username"].ToString();
Session["UserID"] = dr["UserID"].ToString();
Session["Position"] = dr["Position"].ToString();
Session.Timeout = 10;
}
dr.Close();
}
else
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallJs", "alert('Password ไม่ถูกต้องกรูณา Login ใหม่อีกครั้ง');", true);
}
}
finally
{
conn.Close();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
Date :
2010-04-28 18:51:31
By :
bloodeye
ลองก่อนนะค่ะ
Date :
2010-04-29 09:25:24
By :
oomi2830
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace MSEquipment
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
SqlConnection Conn = new SqlConnection();
private void frmLogin_Load(object sender, EventArgs e)
{
string strConn = DBConnString.strConn;
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
}
private void cmdLogin_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("SELECT UserName,Password FROM MEMBER");
sb.Append(" WHERE ([email protected] )");
sb.Append(" AND ([email protected] )");
SqlParameter UserName = new SqlParameter("UserName", SqlDbType.VarChar);
SqlParameter Password = new SqlParameter("Password", SqlDbType.VarChar);
string sqlLogin;
sqlLogin = sb.ToString();
SqlCommand com = new SqlCommand();
SqlDataReader dr;
com.CommandType = CommandType.Text;
com.CommandText = sqlLogin;
com.Connection = Conn;
com.Parameters.Add(UserName).Value = txtUserName.Text;
com.Parameters.Add(Password).Value = txtPassword.Text;
dr = com.ExecuteReader();
if (dr.HasRows)
{
frmMain frm = new frmMain();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("UserName หรือ Password ที่คุณป้อน ไม่ถูกต้อง !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtUserName.Text = "";
txtUserName.Focus();
txtPassword.Text = "";
}
dr.Close();
dr.Dispose();
}
private void cmdEnd_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Date :
2010-04-29 11:10:02
By :
worlord
ในฐานข้อมูลมี 2 ฟิลด์ใช่มั้ยค่ะ
มี Username กับ Password
Date :
2010-04-29 12:14:31
By :
oomi2830
เอา Mail มาละกันครับ เดี๋ยวส่ง CODE ให้ ขี้เกียจเขียน
หรือ Mail มาละกัน [email protected]
Date :
2010-04-30 10:27:12
By :
wichian
---------------------^
--------------------
เขียนหน่อยจะได้ดูด้วย------------------
Date :
2010-05-04 20:27:13
By :
simrun810
ไม่เห็นต้องเขียน copy paste ก็ได้ คนอื่นจะได้ดูด้วย
แต่คนแถวนี้ชอบถามก่อนหา ไม่ต้องก็ได้มั้ง
Date :
2010-05-05 00:37:07
By :
แวะมาดู
ฮ่าๆๆๆๆ
Date :
2011-09-23 19:10:44
By :
8owling
ขอโค้ด ติดต่อถานข้อมูล SQL ที่สามาดสะแดงรูปออกมาด้วย
Date :
2012-01-12 21:46:26
By :
lady M
Code (C#)
Console.WriteLine("Visual Studio C# 2010");
Date :
2012-09-09 10:41:11
By :
Microsoft.Admin
Code (C#)
textBox1.Text="Text textBox";
Date :
2012-09-09 10:42:59
By :
Microsoft.Admin
รบกวน บอกโค้ด การทำบิล ร้านอาหารให้หน่อยน่ะ ค่ะ เป็น visaul c# ตอ้งส่ง อาจารย์ พรุ่งนี้แล้วค่ะ :"(
Date :
2012-09-23 12:01:34
By :
yok
รบกวน บอกโค้ด การทำบิล ร้านอาหารให้หน่อยน่ะ ค่ะ เป็น visaul c# ตอ้งส่ง อาจารย์ พรุ่งนี้แล้วค่ะ :"(
Date :
2012-09-23 12:02:51
By :
yok
พี่ค่ะ string strConn = DBConnString.strConn;
มีปัญหาตรง DBConnString ทำยังไงดีค่ะถึงจะแก้ไขได้ค่ะ
Date :
2012-10-07 00:54:18
By :
ammary
Date :
2017-05-14 15:20:03
By :
aa
ถ้ามี statusด้วยเขียนยังไงครับ
ถ้า 1 = พนักงาน
2 = ผู้จัดการ
Date :
2017-05-14 15:23:44
By :
job
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Hospital
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
DBHospitalDataContext db = new DBHospitalDataContext();
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUser.Text.Trim() == "" && txtPassword.Text.Trim() == "")
{
MessageBox.Show("กรุณากรอกชื่อผู้ใช้และรหัสผ่าน", "Login ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUser.Focus();
return;
}
if (chkLogin(txtUser.Text, txtPassword.Text))
{
MDIParent1 h = new MDIParent1();
h.Username = txtUser.Text.Trim();
h.Show();
this.Hide();
}
}
private bool chkLogin(string user, string pass)
{
try
{
var q = from e in db.Employees
where e.UserName == txtUser.Text
&& e.Pass == txtPassword.Text
select e;
if (q.Any())
{
MessageBox.Show("ยินดีต้อนรับเข้าสู่ระบบ", "Login ถูกต้อง", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
else
{
MessageBox.Show("ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง", "Login ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
clearForm();
return false;
}
}
catch { }
}
private void clearForm()
{
txtUser.Text = "";
txtPassword.Text = "";
txtUser.Focus();
}
}
}
Date :
2018-02-08 16:50:01
By :
จ่อยผู้เร้าใจ
Code (C#)แก้ครับมือใหม่หัดโพส
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Hospital
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
DBHospitalDataContext db = new DBHospitalDataContext();
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUser.Text.Trim() == "" && txtPassword.Text.Trim() == "")
{
MessageBox.Show("กรุณากรอกชื่อผู้ใช้และรหัสผ่าน", "Login ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUser.Focus();
return;
}
if (chkLogin(txtUser.Text, txtPassword.Text))
{
MDIParent1 h = new MDIParent1();
h.Username = txtUser.Text.Trim();
h.Show();
this.Hide();
}
}
private bool chkLogin(string user, string pass)
{
try
{
var q = from e in db.Employees
where e.UserName == txtUser.Text
&& e.Pass == txtPassword.Text
select e;
if (q.Any())
{
MessageBox.Show("ยินดีต้อนรับเข้าสู่ระบบ", "Login ถูกต้อง", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
else
{
MessageBox.Show("ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง", "Login ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
clearForm();
return false;
}
}
catch { }
}
private void clearForm()
{
txtUser.Text = "";
txtPassword.Text = "";
txtUser.Focus();
}
Date :
2018-02-08 16:55:26
By :
จ่อยผู้เร้าใจ
Date :
2022-03-15 02:55:21
By :
น้ำใจ มะลิวัน
Load balance : Server 05