C#DB应用程序Access DB Create Table问题

时间:2015-11-10 14:26:18

标签: c# ms-access

我正在创建一个写入访问数据库的应用程序,并且在执行cmd.ExecuteNonQuery()时我一直收到错误。

错误是字段定义中的语法错误。

行:cmd.ExecuteNonQuery()

以下是我的代码:

            createTableSQL = "CREATE TABLE [Payments] (" +
                         "[PaymentData] VARCHAR(MAX) NOT NULL, [PaySource] VARCHAR(5) NOT NULL, [LastStatus] bit NOT NULL, [Remit] bit NOT NULL, [ConvenienceFee] DECIMAL(6,2) NOT NULL, " +
                         "[PaymentID] VARCHAR(30) NOT NULL, [PaymentMode] VARCHAR(5) NOT NULL, [PaymentEntryDate] DATETIME NOT NULL, [InvoicedAmount] VARCHAR(50) NOT NULL, [PaymentAmount] VARCHAR(50) NOT NULL, " +
                         "[PaymentStatus] VARCHAR(50) NOT NULL, [PaymentNote] VARCHAR(120) NOT NULL);";

        try
        {
            OleDbConnection cnn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName + ";Persist Security Info=True");
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = cnn;
            cmd.CommandText = createTableSQL;
            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();
            result = true;
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
            Console.WriteLine(ex.ToString());
            result = false;
        }

2 个答案:

答案 0 :(得分:0)

我发现了问题

createTableSQL = "CREATE TABLE [Payments] (" +
                     "[PaymentData] VARCHAR(MAX) NOT NULL, [PaySource] VARCHAR(5) NOT NULL, [LastStatus] bit NOT NULL, [Remit] bit NOT NULL, [ConvenienceFee] **DECIMAL(6,2)** NOT NULL, " +
                     "[PaymentID] VARCHAR(30) NOT NULL, [PaymentMode] VARCHAR(5) NOT NULL, [PaymentEntryDate] DATETIME NOT NULL, [InvoicedAmount] VARCHAR(50) NOT NULL, [PaymentAmount] VARCHAR(50) NOT NULL, " +
                     "[PaymentStatus] VARCHAR(50) NOT NULL, [PaymentNote] VARCHAR(120) NOT NULL);";

    try
    {
        OleDbConnection cnn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName + ";Persist Security Info=True");
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = cnn;
        cmd.CommandText = createTableSQL;
        cnn.Open();
        cmd.ExecuteNonQuery();
        cnn.Close();
        result = true;
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message.ToString());
        Console.WriteLine(ex.ToString());
        result = false;
    }

十进制数据类型在我正在使用的访问版本中不起作用。

答案 1 :(得分:0)

Access DDL在字段定义中不支持VARCHAR(MAX)。改为使用MEMO

我在Access 2010中使用该更改测试了您的CREATE TABLE语句。这创建了表而没有错误...

Dim strSql As String
strSql = "CREATE TABLE [Payments] (" & _
    "[PaymentData] MEMO NOT NULL, [PaySource] VARCHAR(5) NOT NULL, [LastStatus] bit NOT NULL, [Remit] bit NOT NULL, [ConvenienceFee] DECIMAL(6,2) NOT NULL, " & _
    "[PaymentID] VARCHAR(30) NOT NULL, [PaymentMode] VARCHAR(5) NOT NULL, [PaymentEntryDate] DATETIME NOT NULL, [InvoicedAmount] VARCHAR(50) NOT NULL, [PaymentAmount] VARCHAR(50) NOT NULL, " & _
    "[PaymentStatus] VARCHAR(50) NOT NULL, [PaymentNote] VARCHAR(120) NOT NULL);"
CurrentProject.Connection.Execute strSql