2016-02-13 79 views
0

這是我的代碼。
文件名是正確的,我不知道是什麼問題。我已經檢查過了,我找不到原因,如果有人能幫助我,這將是真棒System.Data.dll中出現'System.Data.OleDb.OleDbException'附加信息:不是有效的文件名

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace Car 
{ 
    public partial class AddCar : Form 
    { 
     OleDbConnection cnnOLEDB = new OleDbConnection(); 
     OleDbCommand cmdInsert = new OleDbCommand(); 
     public AddCar() 
     { 
      InitializeComponent(); 
     } 

     private void AddCar_Load(object sender, EventArgs e) 
     { // i use access 2013 
     //the address of file is exactly the same as here 
      cnnOLEDB.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data  Source=‪C:\database\LOGIN.accdb;"; 
      cnnOLEDB.Open(); 
      //the error exactly shows here 
     } 

     private void InstButton_Click(object sender, EventArgs e) 
     { 
      if(txtFullName.Text != "" && txtPIC.Text != "" && txtEmail.Text != "" && txtHP.Text != "" && txtAddress.Text != "" && txtAmount.Text != "" && txtDOR.Text != "" && txtDORE.Text != "") 
      { 
       cmdInsert.CommandText = "INSERT INTO MemN(FullName, PICNO, Email, HP, Address, Amount, DOR, DORE) VALUES (\'" + txtFullName.Text + "\' , \'" + txtPIC.Text + "\' , \'" + txtEmail.Text + "\' , " + txtHP.Text + " , \'" + txtAddress.Text + "\' , \'" + txtAmount.Text + "\' , \'" + txtDOR.Text + "\' , \'" + txtDORE.Text + "\');"; 
       cmdInsert.CommandType = CommandType.Text; 
       cmdInsert.Connection = cnnOLEDB; 
       MessageBox.Show("Customer added."); 
      } 
      else 
      { 
       MessageBox.Show("Customer is not added successfully!"); 
      } 

      cmdInsert.Dispose(); 

     } 
    } 
} 
+1

超過數據和源之間刪除空格,只留下一個。單引號並不需要轉義,無論如何使用建議應用參數的回答(但不要使用AddWithValue以避免字符串轉換中的錯誤)最後下一次,絕不會在您的問題中寫入** ASAP **,因爲您的問題是學校問題只是你的 – Steve

回答

0

首先,你應該使用parameterized SQL防止sql injection

您沒有給qoutes圍繞它的值HP qoutes。

cmdInsert.CommandText = "INSERT INTO MemN(FullName, PICNO, Email, HP, Address, Amount, DOR, DORE) VALUES (\'" + txtFullName.Text + "\' , \'" + txtPIC.Text + "\' , \'" + txtEmail.Text + "\' , \'" + txtHP.Text + "\' , \'" + txtAddress.Text + "\' , \'" + txtAmount.Text + "\' , \'" + txtDOR.Text + "\' , \'" + txtDORE.Text + "\');"; 

您可以使用參數化查詢這樣

cmdInsert.CommandText = "INSERT INTO MemN(FullName, PICNO, Email, HP, Address, Amount, DOR, DORE) VALUES (@FullName, @PICNO, @Email, @HP, @Address, @Amount, @DOR, @DORE);"; 
cmd.Parameters.AddWithValue("@FullName", txtFullName.Text); 
cmd.Parameters.AddWithValue("@PICNO", txtPIC.Text); 
cmd.Parameters.AddWithValue("@Email", txtEmail.Text); 
cmd.Parameters.AddWithValue("@HP", txtHP.Text); 
cmd.Parameters.AddWithValue("@Address", txtAddress.Text); 
cmd.Parameters.AddWithValue("@Amount", txtAmount.Text); 
cmd.Parameters.AddWithValue("@DOR", txtDOR.Text); 
cmd.Parameters.AddWithValue("@DORE", txtDORE.Text); 
+0

thx男人你的時間,我做到了這一點,但我仍然得到相同的錯誤,文件名無效 –

+0

什麼是確切的錯誤? – Mairaj

+0

System.Data.dll中發生未處理的「System.Data.OleDb.OleDbException」類型異常 附加信息:不是有效的文件名。它顯示cnnOLEDB.open();由黃色 –

相關問題