qq_37991550 2017-03-20 13:39 采纳率: 0%
浏览 2996

c#连接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 MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{

public partial class login : Form
{

    public login()
    {
        InitializeComponent();
    }

    static string constr = "server=localhost;User Id=root;password=;Database=ex";
    MySqlConnection mycon = new MySqlConnection(constr);
    MySqlCommand mycmd = new MySqlCommand();
    private void ui_bt_login_Click(object sender, EventArgs e)
    {
        //string constr = "server=localhost;User Id=root;password=;Database=ex";
          //MySqlConnection mycon = new MySqlConnection(constr);
        mycon.Open();
        string sql = "select * from user where Name='"+ui_text_name+"' and Password='"+ui_text_pwd +"'";
        mycmd.Connection = mycon;
        mycmd.CommandText = sql;
        mycmd.CommandType = CommandType.Text;
        MySqlDataReader dr = mycmd.ExecuteReader();//运行后这里显示给定关键字不在字典中
        dr.Read();
        if (dr.HasRows)
        {
            this.Hide();
            Form1 Form1 = new Form1();
            Form1.Show();
        }
        else
        {
            MessageBox.Show("登陆失败");
        }
        mycon.Close();
                    }
                    请问哪里错了
                    ![图片说明](https://img-ask.csdn.net/upload/201703/20/1490017187_314223.png)
  • 写回答

1条回答 默认 最新

  • kidie_ 2017-03-20 14:16
    关注

    MySQL对有些表名称、列名称会容易被当成关键字处理,比如group
    建议增加适当的前缀比如user换成ex.user,Name换成user.Name
    或者用`号括着代表名称,比如user这样写

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办