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