0

使用“选择文件 ..”按钮 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();
                    }
                }
            }
        }
    }

在此处输入图像描述

4

1 回答 1

0

我会试试这个:

accessCnn.Open();
foreach (var row in dataset.Tables["medTable"].Rows)
{
    accessCmd.Parameters.Clear();
    accessCmd.Parameters.AddWithValue("@Prescription",row["script"].ToString());
    accessCmd.Parameters.AddWithValue("@Customer_Name", row["cust_Name"].ToString());
    accessCmd.Parameters.AddWithValue("@Medication", row["drug"].ToString());
    accessCmd.Parameters.AddWithValue("@Quantity", Convert.ToInt32(row["qty"]));
    accessCmd.Parameters.AddWithValue("@Date_Filled", Convert.ToDate(row["fill_Date"]));
    accessCmd.ExecuteNonQuery();
}
accessCnn.Close();

此外,您可以尝试在进入循环之前优化创建参数的查询,并且在循环内部仅更改值而不破坏它们并为每个循环重新创建它们

accessCmd.Parameters.Add("@Prescription", OleDbType.VarChar , 80);
accessCmd.Parameters.Add("@Customer_Name", OleDbType.VarChar, 80);
accessCmd.Parameters.Add("@Medication", OleDbType.VarChar, 80);
accessCmd.Parameters.Add("@Quantity", OleDbType.Integer);
accessCmd.Parameters.Add("@Date_Filled", OleDbType.Date);

accessCnn.Open();
foreach (var row in dataset.Tables["medTable"].Rows)
{
    accessCmd.Parameters["@Prescription"].Value = row["script"].ToString();
    accessCmd.Parameters["@Customer_Name"].Value = row["cust_Name"].ToString();
    accessCmd.Parameters["@Medication"].Value = row["drug"].ToString();
    accessCmd.Parameters["@Quantity"].Value = Convert.ToInt32(row["qty"]);
    accessCmd.Parameters["#Date_Filled"].Value = Convert.ToDate(row["fill_Date"]);
    accessCmd.ExecuteNonQuery();
}
accessCnn.Close();
于 2012-11-28T21:56:40.557 回答