使用“选择文件 ..”按钮 1 成功将 CSV 文件加载到 dataGridView。当我随后单击“提交...”按钮 3 时,出现错误:
附加信息:条件表达式中的数据类型不匹配。
请注意,我的 Access DB 的第一列是自动编号的主键。不确定这在这里是否重要。
我必须再次强调,CSV 加载完美,如您所见。“Date Filled”列已更改为“Date_Filled”,因此请不要注意图像中的这一点,因为它已经有几天了。
这是我的代码以及屏幕截图。非常感谢所有帮助!
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.IO;
using System.Globalization;
using System.Configuration;
using System.Data.OleDb;
namespace csvToGrid
{
public partial class Import : Form
{
DataSet dataset;
}
public partial class Import : Form
{
public Import()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
string delimiter = ",";
string tablename = "medTable";
dataset = new DataSet();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (MessageBox.Show("Are you sure you want to import the data from \n " + openFileDialog1.FileName + "?", "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
filename = openFileDialog1.FileName;
StreamReader sr = new StreamReader(filename);
string csv = File.ReadAllText(openFileDialog1.FileName);
dataset.Tables.Add(tablename);
dataset.Tables[tablename].Columns.Add("Prescription");
dataset.Tables[tablename].Columns.Add("Customer_Name");
dataset.Tables[tablename].Columns.Add("Medication");
dataset.Tables[tablename].Columns.Add("Quantity");
dataset.Tables[tablename].Columns.Add("Date_Filled");
string allData = sr.ReadToEnd();
string[] rows = allData.Split("\r".ToCharArray());
foreach (string r in rows)
{
string[] items = r.Split(delimiter.ToCharArray());
dataset.Tables[tablename].Rows.Add(items);
}
this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
MessageBox.Show(filename + " was successfully imported. \n Please review all data before sending it to the database.", "Success!", MessageBoxButtons.OK);
}
else
{
this.Close();
}
}
}
public string filename { get; set; }
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void Import_Load(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
//remove the semicolon, and add brackets below after line
{
var AccessCnn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;", @"C:\Search\KinneyDatabase.accdb");
using (OleDbConnection accessCnn = new OleDbConnection(AccessCnn))
{
//Create The Command
var accessCmd = new OleDbCommand(@"INSERT INTO script_Orders (script,cust_Name,drug,qty,fill_Date) VALUES (@Prescription, @Customer_Name, @Medication, @Quantity, @Date_Filled)", accessCnn);
OleDbParameter d = accessCmd.CreateParameter();
d.OleDbType = OleDbType.Date;
foreach (var row in dataset.Tables["medTable"].Rows)
{
accessCmd.Parameters.Clear();
accessCmd.Parameters.AddWithValue(@"Prescription","script");
accessCmd.Parameters.AddWithValue(@"Customer_Name", "cust_Name");
accessCmd.Parameters.AddWithValue(@"Medication", "drug");
accessCmd.Parameters.AddWithValue(@"Quantity", "qty");
accessCmd.Parameters.AddWithValue(@"Date_Filled", "fill_Date");
accessCnn.Open();
accessCmd.ExecuteNonQuery();
accessCnn.Close();
}
}
}
}
}