gpt4 book ai didi

c# - 访问SQL Server数据库的登录表单错误

转载 作者:行者123 更新时间:2023-11-29 23:18:26 24 4
gpt4 key购买 nike

我一直在尝试为移动商店(大学项目)的数据库制作一个登录表单。我有一个 Employee 表,其中包含 Emp_name 和 Emp_password 作为 ID 和密码,然后访问数据库。这是代码,但不知何故我创建了设计,当我单击登录按钮时,它不会为我写任何消息。请告诉我如何让这个登录表单单独启动mysql服务器的数据库

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)
{

}
}

}

最佳答案

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

关于c# - 访问SQL Server数据库的登录表单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27561495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com