2014-12-19 33 views
0

我一直在尝试为移动商店(大学项目)的数据库创建登录表单。我有一个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) 
    { 

    } 
} 

}

+0

欢迎来到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

+0

@SonerGönül是的,我调试它,当我输入的emp名称和密码什么也没有发生,我没有添加连接,通过点击连接到数据库的工具,并添加表中的数据绑定的maing登录表 – 2014-12-19 07:32:31

+0

@SonerGönül我'米仍然是一个初学者,我仍然在学习c + +和数据结构算法和oop,但我想为我的额外知识做到这一点,所以你可以请详细解释一下:D? – 2014-12-19 07:34:32

回答

0

使用MySqlConnectionStringBuilder来创建连接字符串和PropertyGrid控制输入值。这将保证每个选项都是正确的。

+0

请问你能解释一下代码细节吗?因为我还是初学者:D? – 2014-12-19 16:47:20

+0

而不是像“Data Source = ...”这样简单的字符串赋值,定义MySqlConnectionStringBuilder对象并将其用于连接字符串:MySqlConnectionStringBuilder mcs = new MySqlConnectionStringBuilder()。要获得真正的连接字符串,请使用'mcs.ConnectionString'属性。在你的表单中放置一个'PropertyGrid'控件,并将其'SelectedObject'赋给'mcs'。这将是所有可能的MySQL连接选项的编辑器。注意:'MySqlConnectionStringBuilder'需要引用'MySql.Data'。 – i486 2014-12-19 22:19:50

相关问题