2016-12-05 69 views
2

错误:错误 - 连接字符串属性尚未初始化(C#数据库)?

The error says:- The ConnectionString Property has not been initialized at 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); 
     } 

    } 




} 
} 

回答

2

形式负载变量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); 
    } 

} 
+0

标识符预期,int是关键字。如何使用它? – YoMama

+0

@YoMama:它改变为'Integer' –

+0

感谢您的帮助,但是,它仍然给我error..Error.System.Data.OledbException(0x80040e14):在INSERT INTO语句的语法错误。 – YoMama

相关问题