简体   繁体   English

错误:ExecuteNonQuery:连接属性尚未初始化

[英]Error: ExecuteNonQuery: Connection property has not been initialized

Please help me, I'm currently stuck in this error: 请帮助我,我目前陷入此错误:

ExecuteNonQuery: Connection property has not been initialized

I read many of the related questions but none of them work. 我阅读了许多相关的问题,但是没有一个起作用。 I also searched in Google hoping for the answer but sadly I didn't found any. 我也在Google上搜索,希望找到答案,但可惜我没有找到答案。 :( :(

Here is my code. 这是我的代码。

namespace jollibee4
{
    public partial class AccountRegister : Form
    {
        public AccountRegister()
        {
            InitializeComponent();
        } 

        private void add_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection con = new OleDbConnection();
                con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\dbms\jollibee.accdb";
                con.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = con;

                cmd = new OleDbCommand("insert into Employee_Details ([username],[password],[Name],[Middle_Name],[Surname],[address],[account_Type],[Mobile_Number],[BirthDate]) values (?,?,?,?,?,?,?,?,?);");
                cmd.Parameters.AddWithValue("@username", username.Text);
                cmd.Parameters.AddWithValue("@password", password.Text);
                cmd.Parameters.AddWithValue("@Name", name.Text);
                cmd.Parameters.AddWithValue("@Middle_Name", middlename.Text);
                cmd.Parameters.AddWithValue("@Surname", surname.Text);
                cmd.Parameters.AddWithValue("@address", address.Text);
                cmd.Parameters.AddWithValue("@account_Type", accountType.Text);
                cmd.Parameters.AddWithValue("@BirthDate", birthdate.Text);
                cmd.Parameters.AddWithValue("@Mobile_Number", mobilenumber.Text);

                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("done");
            }
            catch (Exception ex)
            {
                 MessageBox.Show(ex.Message);
            }
        }
    }
}

You are creating the command twice, causing the second instance to lose the connection: 您两次创建命令,导致第二个实例断开连接:

    private void add_Click(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\dbms\jollibee.accdb";
            con.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;

            //THIS LINE IS THE PROBLEM
            cmd = new OleDbCommand("insert into Employee_Details ([username],[password],[Name],[Middle_Name],[Surname],[address],[account_Type],[Mobile_Number],[BirthDate]) values (?,?,?,?,?,?,?,?,?);");
            cmd.Parameters.AddWithValue("@username", username.Text);
            cmd.Parameters.AddWithValue("@password", password.Text);
            cmd.Parameters.AddWithValue("@Name", name.Text);
            cmd.Parameters.AddWithValue("@Middle_Name", middlename.Text);
            cmd.Parameters.AddWithValue("@Surname", surname.Text);
            cmd.Parameters.AddWithValue("@address", address.Text);
            cmd.Parameters.AddWithValue("@account_Type", accountType.Text);
            cmd.Parameters.AddWithValue("@BirthDate", birthdate.Text);
            cmd.Parameters.AddWithValue("@Mobile_Number", mobilenumber.Text);

            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("done");
        }
        catch (Exception ex)
        {
             MessageBox.Show(ex.Message);
        }
    }

instead it should be: 相反,它应该是:

cmd.CommandText = "insert into Employee_Details ([username],[password],[Name],[Middle_Name],[Surname],[address],[account_Type],[Mobile_Number],[BirthDate]) values (?,?,?,?,?,?,?,?,?);"

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

相关问题 错误ExecuteNonQuery:尚未初始化Connection属性 - Error ExecuteNonQuery: Connection property has not been initialized 由于尚未初始化ExecuteNonQuery:Connection属性而收到错误 - get an error as ExecuteNonQuery:Connection property has not been initialized SQL Server错误:ExecuteNonQuery:连接属性尚未初始化 - SQL Server error: ExecuteNonQuery: Connection property has not been initialized 错误 ExecuteNonQuery:连接属性尚未初始化 C#(访问) - Error ExecuteNonQuery: Connection property has not been initialized C# (Access) 如何解决“ ExecuteNonQuery:连接属性尚未初始化?”的错误? - how to solve error of “ ExecuteNonQuery: Connection property has not been initialized?” ExecuteNonQuery:连接属性尚未初始化 - ExecuteNonQuery: Connection property has not been initialized 'ExecuteNonQuery:连接属性尚未初始化。' - 'ExecuteNonQuery: Connection property has not been initialized.' ExecuteNonQuery:连接属性尚未初始化 - ExecuteNonQuery: Connection property has not been initialized “ ExecuteNonQuery:连接属性尚未初始化。” - “ExecuteNonQuery: Connection property has not been initialized.” ExecuteNonQuery:连接属性尚未初始化。 - ExecuteNonQuery: Connection property has not been initialized.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM