connectionstring属性尚未初始化。 c#访问数据库

时间:2016-06-03 11:12:42

标签: c# ms-access connection-string access

我似乎无法解决它我正在尝试连接到我的访问数据库,但我只是不能继续得到错误。有人可以帮助我

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

        }

2 个答案:

答案 0 :(得分: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以阻止自己受到攻击。主要查看文章的参数化查询部分。

答案 1 :(得分:0)

你有一个私有变量(名为" connection")和一个OleDbConnection的局部变量(名为" 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注入攻击持开放态度。