【问题标题】:the connectionstring property has not been initialized. c# Access database连接字符串属性尚未初始化。 c# 访问数据库
【发布时间】:2016-06-03 11:12:42
【问题描述】:

我似乎无法解决它我正在尝试连接到我的访问数据库,但我无法并不断出错。谁能帮帮我

namespace WindowsFormsApplication3
{
    public partial class Form2 : Form

    {
        private OleDbConnection connection = new OleDbConnection();
        public Form2()
        {
            InitializeComponent();
            string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string path = (System.IO.Path.GetDirectoryName(executable));
            AppDomain.CurrentDomain.SetData("DataDirectory", path);
            OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.accdb;User Id=admin; Password=;");


        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3(); // Instantiate a Form3 object.
            f3.Show(); // Show Form3 and
            this.Close(); // closes the Form2 instance
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {

                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                command.CommandText= "INSERT into Dataaa ([FirstName],[LastName],[ICNO],[Address],[Loan],[Percent],[Payback],[StartDate],[EndDate],[Monthly],[PaymentType],[Remark]) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "')";

                command.ExecuteNonQuery();
                MessageBox.Show("Details have been Saved.");

            }
            catch (Exception ex)

            {

                MessageBox.Show("error " + ex);
            }
            finally
            {
            connection.Close();
            }

        }

【问题讨论】:

  • 你能发布错误信息吗?
  • @M.Schena 连接字符串属性尚未初始化

标签: c# ms-access connection-string


【解决方案1】:

问题是您永远不会将连接字符串传递给connection 属性,所以这就是引发异常的原因。

目前,您正在初始化构造函数中未使用的局部变量。所需要做的就是删除局部变量并使用正确的连接字符串初始化属性。

要解决此问题,只需将构造方法更改为以下内容:

public Form2()
{
    InitializeComponent();
    string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
    string path = (System.IO.Path.GetDirectoryName(executable));
    AppDomain.CurrentDomain.SetData("DataDirectory", path);

    this.connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.accdb;User Id=admin; Password=;");
}

并删除属性上的初始化,如下所示:

private OleDbConnection connection;

正如 Owen Pauling 所说,您对 SQL 注入攻击持开放态度,因此我强烈建议您浏览此 article 以阻止自己受到攻击。主要看文章的参数化查询部分。

【讨论】:

    【解决方案2】:

    OleDbConnection 有一个私有变量(名为“connection”)和一个局部变量(名为“connect”)。您正在使用连接字符串初始化后者,但使用前者来运行您的命令。

    在您的 Form2 方法更改中:

    OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.accdb;User Id=admin; Password=;");
    

    到:

    connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.accdb;User Id=admin; Password=;");
    

    还要为您的命令使用查询参数,以免您受到 SQL 注入攻击。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 2017-09-14
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多