c# - 我如何检查用户(创建或未创建)

时间:2015-03-13 19:02:47

标签: c# mysql

我正在使用MySql 5.6。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MySql.Data.MySqlClient;

    namespace MySqlRank
    {
        class Sql
        {
            private MySqlConnection connection;
            private string server;
            private string database;
            private string uid;
            private string password;

            //Constructor
            public Sql()
            {
                Initialize();
            }



            //Initialize values
            private void Initialize()
            {
                server = "localhost";
                database = "db";
                uid = "root";
                password = "pass";
                string connectionString;
                connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

                connection = new MySqlConnection(connectionString);
            }


            //open connection to database
            private bool OpenConnection()
            {
                try
                {
                    connection.Open();
                    return true;
                }
                catch (MySqlException ex)
                {
                    switch (ex.Number)
                    {
                        case 0:
                            Console.WriteLine("Cannot connect to server.  Contact administrator");
                            break;

                        case 1045:
                            Console.WriteLine("Invalid username/password, please try again");
                            break;
                    }
                    return false;
                }
            }

            private bool CloseConnection()
            {
                try
                {
                    connection.Close();
                    return true;
                }
                catch (MySqlException ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }
            }

            //Insert statement
            public void Successful(ulong Id)
            {
                //if(NotCreated)
                {
                    string query = "INSERT INTO rank(id, trades) VALUES('" + Id + "', '1')";

                    //open connection
                    if (this.OpenConnection() == true)
                    {
                        //create command and assign the query and connection from the constructor
                        MySqlCommand cmd = new MySqlCommand(query, connection);

                        //Execute command
                        cmd.ExecuteNonQuery();

                        //close connection
                        this.CloseConnection();
                    }
                }     
                //else if (created)
                {
                    string query = "UPDATE rank SET trades='1' WHERE id='" + Id + "'";

                    //Open connection
                    if (this.OpenConnection() == true)
                    {
                        MySqlCommand cmd = new MySqlCommand();
                        cmd.CommandText = query;
                        cmd.Connection = connection;

                        //Execute query
                        cmd.ExecuteNonQuery();
                        //close connection
                        this.CloseConnection();
                    }
                }
            }

            //Delete statement
            public void Delete(ulong Id)
            {
                string query = "DELETE FROM rank WHERE id='"+ Id +"'";

                if (this.OpenConnection() == true)
                {
                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    cmd.ExecuteNonQuery();
                    this.CloseConnection();
                }
            }

            //Select statement
            public List<string>[] Select()
            {
                string query = "SELECT * FROM rank";

                //Create a list to store the result
                List<string>[] list = new List<string>[3];
                list[0] = new List<string>();
                list[1] = new List<string>();

                //Open connection
                if (this.OpenConnection() == true)
                {
                    //Create Command
                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    //Create a data reader and Execute the command
                    MySqlDataReader dataReader = cmd.ExecuteReader();

                    //Read the data and store them in the list
                    while (dataReader.Read())
                    {
                        list[0].Add(dataReader["id"] + "");
                        list[1].Add(dataReader["trades"] + "");
                    }

                    //close Data Reader
                    dataReader.Close();

                    //close Connection
                    this.CloseConnection();

                    //return list to be displayed
                    return list;
                }
                else
                {
                    return list;
                }
            }
         }
    }

关于成功无效的问题。我尝试使用try-cath方法,但这不起作用。(Id是唯一键)。我不需要选择方法,但我不能。我的表name是rank(id,trade)。我需要创建更新交易+ 1.如果没有创建,请创建一个新用户。我只需创建或不创建支票。

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题是正确的,你想检查你的表中是否已存在该行?如果是这样,您可以在catch语句中检查是否存在主键冲突:

  catch(MySqlException ex)
  {
    this.CloseConnection();
    if(ex.Number == 1067)
    {
     //Handle exception
    }
  }

编辑:在我自己的机器上测试你的代码后,我发现this.OpenConnection()没有任何问题。我的代码成功运行了。您确定给予connectionString的凭据有效吗?