1

我有这张桌子:

Items
====
Barcode (text)
Des (text)
Price (double)

我制作了 csv 文件d:\Items.csv 以便快速插入 (12345,computer,120.44).....

我尝试像这样插入(C# WinForm 程序):

Cmd = Conn.CreateCommand();
SQL = @"INSERT INTO Items SELECT * FROM [Text;DATABASE=" + @"d:" + @"\].[Items.txt];";
Cmd.CommandText = SQL;
Cmd.ExecuteNonQuery();

但我有 2 个错误:

1. Data type mismatch in criteria expression.

2. The field 'Items.Barcode ' cannot contain a Null value because the Required property for this field is set to True.  Enter a value in this field.

如何解决这个问题?

4

1 回答 1

3

以下 C# 代码适用于我。它导入C:\Users\Public\Items.csv到我的 Access 2010 数据库中名为 [Items] 的表中C:\Users\Public\Database1.accdb。CSV 文件的内容很简单

12345,computer,120.44

代码是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;

namespace myDbTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string myConnectionString;
            myConnectionString =
                    @"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
                    @"Dbq=C:\Users\Public\Database1.accdb;";

            using (var con = new OdbcConnection())
            {
                con.ConnectionString = myConnectionString;
                con.Open();

                using (var cmd = new OdbcCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText =
                            @"INSERT INTO Items " +
                            @"SELECT * FROM [Text;FMT=Delimited;HDR=NO;IMEX=2;CharacterSet=437;ACCDB=YES;Database=C:\Users\Public].[Items#csv];";
                    cmd.ExecuteNonQuery();
                }
                con.Close();
            }
        }
    }
}
于 2013-10-06T12:59:41.777 回答