這是我的代碼。
文件名是正確的,我不知道是什麼問題。我已經檢查過了,我找不到原因,如果有人能幫助我,這將是真棒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();
}
}
}
超過數據和源之間刪除空格,只留下一個。單引號並不需要轉義,無論如何使用建議應用參數的回答(但不要使用AddWithValue以避免字符串轉換中的錯誤)最後下一次,絕不會在您的問題中寫入** ASAP **,因爲您的問題是學校問題只是你的 – Steve