我一直在嘗試爲移動商店(大學項目)的數據庫創建登錄表單。我有一個Employee表,其中包含Emp_name和Emp_password作爲id和密碼,然後訪問數據庫。這是代碼,但不知何故我創建了設計,當我點擊登錄按鈕時,它不會爲我寫任何消息。請告訴我如何讓這個登錄表單單獨啓動數據庫MySQL服務器用於訪問SQL服務器數據庫的登錄表錯誤
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 System.Data.Sql;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace login_form
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection();
public Form1()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=RAYMOND-PC;Initial Catalog=[mobile shop final];Integrated Security=True";
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'sTUDENTDataSet.login' table. You can move, or remove it, as needed.
//this.loginTableAdapter.Fill(this.sTUDENTDataSet.login);
SqlConnection con = new SqlConnection("Data Source=RAYMOND-PC;Initial Catalog= mobile shop final;Integrated Security=True");
con.Open();
{
}
}
private void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=RAYMOND-PC;Initial Catalog=mobile shop final;Integrated Security=True";
con.Open();
string Emp_name=textBox1.Text;
string Emp_password = textBox2.Text ;
SqlCommand cmd = new SqlCommand("select Emp_name , Emp_password from Employee where Emp_name='" + textBox1.Text + "'and Emp_password='" + textBox2.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
SqlDataReader dr;
dr = cmd.ExecuteReader();
da.Fill(dt);
if (Emp_name == textBox1.Text && Emp_password==textBox2.Text)
{
MessageBox.Show("Login sucess Welcome to Mobile shop");
System.Diagnostics.Process.Start("mobile shop final");
}
else
{
MessageBox.Show("Invalid Login please check username and password");
}
con.Close();
}
private void Button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Label1_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
}
}
}
歡迎來到Stack Overflow。你有沒有調試過你的代碼?你檢查你的所有值是否正確?順便說一句,你應該總是使用[參數化查詢](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)。這種字符串連接對於[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)攻擊是開放的。並且不要將密碼存儲爲純文本。閱讀:http://stackoverflow.com/questions/1054022/best-way-to-store-password-in-database不要忘記使用'使用'語句來處理你的數據庫連接和對象。 – 2014-12-19 07:29:16
@SonerGönül是的,我調試它,當我輸入的emp名稱和密碼什麼也沒有發生,我沒有添加連接,通過點擊連接到數據庫的工具,並添加表中的數據綁定的maing登錄表 – 2014-12-19 07:32:31
@SonerGönül我'米仍然是一個初學者,我仍然在學習c + +和數據結構算法和oop,但我想爲我的額外知識做到這一點,所以你可以請詳細解釋一下:D? – 2014-12-19 07:34:32