2016-12-05 70 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

相關問題