【问题标题】:Error- Connection String property has not been initialized (C# Database)?错误 - 连接字符串属性尚未初始化(C# 数据库)?
【发布时间】:2016-12-05 04:29:35
【问题描述】:

错误:

错误说:- ConnectionString 属性尚未 在 system.data.OledbOledConnection.PermissionDemand() 处初始化。

我无法找出错误。有人能解释一下这是什么意思以及如何解决吗?

C# 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;//microsoft database
namespace AccessLoginApp
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection connection = new OleDbConnection();
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\admin\Documents\Book Database.accdb;
        Persist Security Info=False;";

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error"+ ex);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {

        try{
        OleDbConnection connection = new OleDbConnection();
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = " Insert into Book Name(Book Name,Book Number,Publisher) values('" + bookName.Text + "','" + bookNumber.Text + "','" + publisher.Text + "')";
        command.ExecuteNonQuery();
        MessageBox.Show("Data Saved");


        }
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex);
        }

    }




}
}

【问题讨论】:

    标签: c# visual-studio ms-access


    【解决方案1】:

    表单加载中的变量connectionbutton1_Click事件是OleDbConnection类的不同实例(显然它们是不同的)并且您没有在button1_Click事件中使用连接字符串初始化连接变量(它也会导致错误)。如果您这样做意味着您的代码将正常工作,如果您将串联查询替换为Parameterized 查询意味着您的代码将运行良好。而using 语句的引入将使您的代码完美。您可以尝试以下方法:

    string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\admin\Documents\Book Database.accdb;Persist Security Info=False;";
    // This will be the connection string
    private void Form1_Load(object sender, EventArgs e)
    {
        // Need not to do anything regarding connection
        // some other statements if needed
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
    
        try
        {
            using (OleDbConnection connection = new OleDbConnection(conString)) // Create and initialize connection object
            {
                connection.Open();
                using (OleDbCommand command = new OleDbCommand())
                {
                    command.Connection = connection;
                    command.CommandText = " Insert into Book(Book_Name,Book_Number,Publisher) values(@BookName,@BookNumber,@Publisher)";
                    command.Parameters.Add("@BookName", OleDbType.VarChar).Value = bookName.Text;
                    command.Parameters.Add("@BookNumber", OleDbType.Integer).Value = bookNumber.Text;
                    command.Parameters.Add("@Publisher", OleDbType.VarChar).Value = publisher.Text;                     
                    command.ExecuteNonQuery();
    
                }
            }
          MessageBox.Show("Data Saved");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex);
        }
    
    }
    

    【讨论】:

    • 需要标识符,int 是关键字。怎么用?
    • @YoMama:改成Integer
    • 感谢您的帮助,但它仍然给我错误..E​​rror.System.Data.OledbException(0x80040e14):INSERT INTO 语句中的语法错误。
    • 很可能表名和列名中的空格可能会产生问题,用数据库和代码中的 Underscode 替换这些空格,或者将它们包含在一对 @987654329 中@ 和] 在查询中避免语法错误
    • 我没看懂,你指的是哪个空位?以及在哪里添加 [ ],你能在你的代码中演示一下吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 2017-09-14
    • 1970-01-01
    • 2022-01-07
    • 2013-08-03
    • 1970-01-01
    相关资源
    最近更新 更多