我正在尝试在数据库中插入值,但是不幸的是,当我单击按钮时,我一次又一次遇到相同的错误,该错误已在图片中显示(请在此处查看图片:http://1.bp.blogspot.com/-oCH03jEnrdM/Uua4U9ddQZI/AAAAAAAABh0/BncJe0mHqQM/s1600/Capture.PNG)。该错误是我认为是在我的Connection中的某个位置,但我是newbee,因此我对此错误的了解不多,请帮助我进行整理:

这是我的代码:

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();
        }
    }
}

最佳答案

看起来您从未调用过Initialize方法。

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

关于c# - MySQL C#值插入和connection.open()无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21390306/

10-14 18:04
查看更多