|
|
|
C# MySQL + Login รบกวนพี่ๆดูโค้ดนี่หน่อยครับ ไม่ทราบว่าผมทำผิดตรงไหน พอดีมือใหม่หัดเขียนครับ |
|
|
|
|
|
|
|
อันนี้เป็น Class DB
Code (C#)
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using MySql.Data.MySqlClient;
namespace LoginFormCS
{
class dbconnect
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
//Constructor
public dbconnect()
{
Initialize();
}
//Initialize values
private void Initialize()
{
server = "localhost";
database = "supplies";
uid = "root";
password = "12345678";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}//class
}
ส่วนอันนี้เป็น class Login
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;
using MySql.Data.MySqlClient;
using System.IO;
namespace LoginFormCS
{
public partial class frmLogin : Form
{
private dbconnect DBConn;
public frmLogin()
{
InitializeComponent();
DBConn = new dbconnect();
}
private void btnLogin_Click(object sender, EventArgs e)
{
string strSQL = null;
int intNumRows = 0;
strSQL = "SELECT COUNT(*) FROM tbl_user WHERE user_name = '" + this.txtUsername.Text + "' AND user_pass = '" + this.txtPassword.Text + "' ";
DBConn.ToString();
//ตั้งแต่บรรทัดนี้ เป็นต่อไป เิกิด Error
MySqlCommand objCmd = new MySqlCommand(strSQL,___);
intNumRows = Convert.ToInt32(objCmd.ExecuteScalar());
if (intNumRows > 0)
{
frmMain frm = new frmMain();
frm._strUser = this.txtUsername.Text;
frm.LoadInfor();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("Username or Password Incorrect");
}
}
private void btnClose_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure to exit?", "Confirm.", MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
Application.Exit();
}
}
private void frmLogin_Load(object sender, EventArgs e)
{
}
}
}
ไม่ทราบว่าผมควรปรับโค้ดตรงไหน...บ้างครับ
Tag : .NET, Win (Windows App), C#
|
|
|
|
|
|
Date :
2012-09-24 11:46:46 |
By :
Takashi_7 |
View :
1357 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|