简体   繁体   English

MySQL C#值插入和connection.open()无法正常工作

[英]MySQL C# value inserting and connection.open() is not working properly

I am trying to insert values in my database but unfortunately i am getting the same error again and again which i have shown in the picture(Please see the picture here: http://1.bp.blogspot.com/-oCH03jEnrdM/Uua4U9ddQZI/AAAAAAAABh0/BncJe0mHqQM/s1600/Capture.PNG ) when i click the button. 我正在尝试在数据库中插入值,但不幸的是,我一次又一次遇到相同的错误,该错误已在图片中显示(请参阅此处的图片: http : //1.bp.blogspot.com/-oCH03jEnrdM/Uua4U9ddQZI /AAAAAAAABh0/BncJe0mHqQM/s1600/Capture.PNG ),当我单击该按钮时。 The error is somewhere in my Connection i think so but i am newbee therefore i dont have much idea about this error please help me in sorting out: 该错误是我认为是在我的Connection中的某个位置,但是我是newbee,因此我对此错误的了解不多,请帮助我进行整理:

Here is my code: 这是我的代码:

public partial class Test : Form
{
    private MySqlConnection connection;
    private string server;
    private string database;
    private string uid;
    private string password;
    public Test()
    {
        InitializeComponent();
    }
    //Initialize values
    private void Initialize()
    {
        server = "localhost";
        database = "ADC";
        uid = "**********";
        password = "********";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
        connection = new MySqlConnection(connectionString);
    }

    //EndDatabase

    //Creating Connection

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

                case 1045:
                    MessageBox.Show("Invalid username/password, please try again");
                    break;
                case 1062:
                    MessageBox.Show("Duplicate Entry. Please try again with different cardentials");
                    break;
            }
            return false;
        }
    }

    //Close connection
    private bool CloseConnection()
    {

        try
        {
            connection.Close();
            return true;
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
    }
    //End Connection


    private void Test_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        var Pin = "1124";
        var Sq= "What is your Name?";
        var Sqa= "My Name is ";
        var Bg= "B+";
        var Int = "Programming";
        var Skills= "Develepoment";
        var webp = "xyz.com";
        var addr= "DHA lahore";
        var cnumber= "12324123";


        string query = "INSERT INTO userprofilemoreinformation (Pin,SecurityQuestion,SecurityQuestionAnswer,BloodGroup,Interest,Skills,WebPage,Address,ContactNumber) VALUES(@Pin,@Sq,@Sqa,@Bg,@Int,@Skills,@webp,@addr,@cnumber)";

        //open connection
        if (this.OpenConnection() == true)
        {
            //create command and assign the query and connection from the constructor
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.Parameters.AddWithValue("@Pin", Pin);
            cmd.Parameters.AddWithValue("@Sq", Sq);
            cmd.Parameters.AddWithValue("@Sqa", Sqa);
            cmd.Parameters.AddWithValue("@Bg", Bg);
            cmd.Parameters.AddWithValue("@Int", Int);
            cmd.Parameters.AddWithValue("@Skills", Skills);
            cmd.Parameters.AddWithValue("@webp", webp);
            cmd.Parameters.AddWithValue("@addr", addr);
            cmd.Parameters.AddWithValue("@cnumber", cnumber);
            //Execute command
            cmd.ExecuteNonQuery();

            //close connection
            this.CloseConnection();
        }
    }
}

It doesn't look like you ever call your Initialize method. 看起来您从未调用过Initialize方法。

public Test()
{
    InitializeComponent();
    Initialize();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM