简体   繁体   English

“ ExecuteNonQuery:连接属性尚未初始化。”

[英]“ExecuteNonQuery: Connection property has not been initialized.”

Here is the code: 这是代码:

 string str = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/charlyn_dale/Documents/Visual Studio 2010/Projects/LMS/WindowsFormsApplication2/Accounts.accdb;Persist Security Info=False");
            OleDbCommand conn = new OleDbCommand(str);
            con.Open();
            string query  = "insert into Account ([Username],[Password],FirstName,MiddleName,LastName,Age,Section,Gender,Address,AccountStatus) values('" + txt1.Text + "','" + txt2.Text + "','" + txt4.Text + "','" + txt5.Text + "','" + txt6.Text + "','" + txt7.Text + "','" + txt8.Text + "','" + cmb2.Text + "','" + txt9.Text + "','" + cmb1.Text + "')";
            OleDbCommand cmd = new OleDbCommand(query, con);
            conn.ExecuteNonQuery();
            MessageBox.Show("Registration Success!");
            con.Close();

and the error is: 错误是:

Connection property has not been initialized 连接属性尚未初始化

There are 3 main issues in your Access DB connection: Access DB连接中存在3个主要问题:

  1. OleDbConnection connection string property has not initialized when opening OLE DB connection (note that con is different from conn in this context). 打开OLE DB连接时, OleDbConnection连接字符串属性尚未初始化(请注意con在此情况下与conn不同)。

  2. The connection string wrongly assigned to variable conn which declared as OleDbCommand , use OleDbConnection instead. 错误地分配给声明为OleDbCommand变量conn的连接字符串,请改用OleDbConnection

  3. The connection string data source path seems invalid by using slash sign for directory separator (assuming target file exists in Windows folder), use backslash escape sequence ( \\\\ ) or single backslash with literal string instead (eg @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\......" ). 通过使用斜杠符号作为目录分隔符(假设目标文件存在于Windows文件夹中),使用反斜杠转义序列( \\\\ )或带有文字字符串的单个反斜杠(例如@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\......" ),连接字符串数据源路径似乎无效@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\......" )。

Hence, the correct connection sequence should be like this: 因此,正确的连接顺序应如下所示:

string str = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\charlyn_dale\\Documents\\Visual Studio 2010\\Projects\\LMS\\WindowsFormsApplication2\\Accounts.accdb;Persist Security Info=False");

using (OleDbConnection conn = new OleDbConnection(str))
{
    conn.Open();

    // security tips: better use parameter names to prevent SQL injection on queries
    // and put value checking method for all textbox values (sanitize input)
    string query = "insert into Account ([Username],[Password],FirstName,MiddleName,LastName,Age,Section,Gender,Address,AccountStatus) values ('" + txt1.Text + "','" + txt2.Text + "','" + txt4.Text + "','" + txt5.Text + "','" + txt6.Text + "','" + txt7.Text + "','" + txt8.Text + "','" + cmb2.Text + "','" + txt9.Text + "','" + cmb1.Text + "')";
    using (OleDbCommand cmd = new OleDbCommand(query, conn))
    {
        conn.ExecuteNonQuery();
    }
    ... // other stuff
    conn.Close();
}

NB: using statements added due to OLE DB connection should be disposed immediately after usage to free up resources. 注意:由于OLE DB连接而添加的using语句应在使用后立即处理以释放资源。

Similar issues: 类似问题:

get an error as ExecuteNonQuery:Connection property has not been initialized 由于尚未初始化ExecuteNonQuery:Connection属性而收到错误

ExecuteNonQuery: Connection property has not been initialized (access database) ExecuteNonQuery:连接属性尚未初始化(访问数据库)

ExecuteNonQuery: Connection property has not been initialized ExecuteNonQuery:连接属性尚未初始化

暂无
暂无

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

相关问题 'ExecuteNonQuery:连接属性尚未初始化。' - 'ExecuteNonQuery: Connection property has not been initialized.' ExecuteNonQuery:连接属性尚未初始化。 - ExecuteNonQuery: Connection property has not been initialized. ExecuteNonQuery:连接属性尚未初始化。 我被困住了 - ExecuteNonQuery: Connection property has not been initialized. I am stuck ExecuteNonQuery:连接属性尚未初始化。 SqlParameter数组 - ExecuteNonQuery: Connection property has not been initialized. Sqlparameter array System.InvalidOperationException:“ExecuteNonQuery:连接属性尚未初始化。” - System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.' 异常错误“ ExecuteNonQuery:连接属性尚未初始化。”这是我的C#编码 - exception error “ExecuteNonQuery: Connection property has not been initialized.” this is my C# Coding 出现错误“ ExecuteNonQuery:连接属性尚未初始化。”我运行程序 - getting error “ ExecuteNonQuery: Connection property has not been initialized.” wen i run my program 错误:ExecuteNonQuery:连接属性尚未初始化。 (尝试使用 SSIS 的脚本任务连接 Snowflake DB) - Error: ExecuteNonQuery: Connection property has not been initialized. (Trying to connect with Snowflake DB using script task of SSIS) 获取“System.InvalidOperationException:'ExecuteNonQuery:连接属性尚未初始化。'” - Getting “System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.'” 获取以下“ System.InvalidOperationException:'ExecuteNonQuery:连接属性尚未初始化。” - Getting the following “System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM