ADO.NET执行SQL语句时出错

时间:2014-07-28 14:58:39

标签: c# sql-server ado.net

我尝试登录并注册。当我尝试登录或以注册形式提交我有错误时,我有DB_Connectivity并查询类

  

您的sql语法出错时出错检查与您的Mysql服务器版本对应的手册,以便在[用户名]附近使用正确的语法,密码来自注册[User Name] = username1 ...... ......在第1行

在注册Exception throws"未知错误.............第1行"

这是DB_Connectivity

 class DB_Connectivity
    {
        private string myconnection;
        private string server;
        private string database;
        private string uid;
        private string password;
        public MySqlConnection conn;

        public DB_Connectivity()
        {
            Initialize();
        }

        public void Initialize()
        {
            server = "localhost";
            database = "my_project_data";
            uid = "root";
            password = "";

            myconnection = "SERVER = " + server + ";" + "DATABASE =" + database + ";" + "UID =" + uid + ";" + "PASSWORD =" + password + ";";

            conn = new MySqlConnection(myconnection);
        }

        public void openConnection()
        {
            try
            {
                conn.Open();
            }

            catch (MySqlException ms)
            {
                System.Windows.Forms.MessageBox.Show("Error Occured " + ms.Message);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(" Error Please Contact Administrator " +ex.Message);
            }
        }

        public void closeConnection()
        {
            try
            {
                conn.Close();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(" Unexpected Error " +ex.Message);
            }

        }
    }

这是查询类

class Queries :DB_Connectivity
{
        private MySqlCommand cmd;
        private MySqlDataReader datar;
        private string sql_query;

        public void insertIntoSignup(string firstname, string lastname, string address, string phone, string email, string username, string password )
        {
            try
            {
                sql_query = "Insert Into my_project_data.signup([First Name],[Last Name],Address,Phone,E-Mail,[User Name],Password) Values('"+firstname+"','"+lastname+"','"+address+"','"+phone+"','"+email+"','"+username+"','"+password+"')";

                openConnection();
                cmd = new MySqlCommand(sql_query, conn);
                datar = cmd.ExecuteReader();
                closeConnection();
            }
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Ooooops Error "  +ex.Message);
            }
        }

        public void userlogchech(string username1, string password1)
        {
            try
            {
                sql_query = "Select [User Name],Password From signup Where [User Name] = username1 AND Password = password1";

                openConnection();
                cmd = new MySqlCommand(sql_query, conn);
                datar = cmd.ExecuteReader();

                if (datar.Read())
                {
                    if (username1 != "")
                    {
                        System.Windows.Forms.MessageBox.Show("User Name Alredy Exists", "Error");
                    }
                    else if (password1 != "")
                    {
                        System.Windows.Forms.MessageBox.Show("Password Already Exists");
                    }
                    else
                    {
                        datar.Close();
                        System.Windows.Forms.MessageBox.Show("Welcome to German Moters");
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Try Again Later");
                    conn.Close();
                }
            }
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(" Unknown Error " +ex.Message);
            }
        }
    }

登录按钮

 private void Log_btn_Click(object sender, EventArgs e)
        {
            if (User_Name_txt.Text != "" && mask_Password_txt.Text != "")
            {
                Classes.Queries qua = new Classes.Queries();
                qua.userlogchech(User_Name_txt.Text, mask_Password_txt.Text);
            }
            else
            {
                MessageBox.Show("Please Enter Both User Name And Password");
            }
        }

以注册形式提交按钮

  private void Submit_btn_Click(object sender, EventArgs e)
        {
            if (UserName_txt.Text != "" && mask1_password_txt.Text != "" && FName_txt.Text != "" && LName_txt.Text != "" && Address_txt.Text != "" && Phone_txt.Text != "" && Email_txt.Text != "")
            {

                Classes.Queries qu = new Classes.Queries();
                qu.insertIntoSignup(FName_txt.Text,LName_txt.Text,Address_txt.Text,Phone_txt.Text,Email_txt.Text, UserName_txt.Text,mask1_password_txt.Text );
            }
            else
            {
                MessageBox.Show("All Fields Must Be Filled ", "Error" ,MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

1 个答案:

答案 0 :(得分:1)

您有一个包含空格的字段User Name,请使用方括号将其更改为[User Name],并为名字姓氏做贵妇人等。您应该尝试避免在字段名称中使用空格,将其替换为 UserName FirstName 等。