简体   繁体   English

我用来创建表的此SQL语句的语法有什么问题?

[英]What is wrong with the syntax of this SQL statement I am using to create a table?

I keep running the code/debugger and even set break points so I can step through the code and haven't been able to find where what the syntax error is I know its within this C# method for creating a new table in the c-tree database. 我一直在运行代码/调试器,甚至设置断点,所以我可以单步执行代码,却无法找到语法错​​误在哪里,我知道在此C#方法中用于在c树中创建新表的语法错误数据库。

Here's the code: 这是代码:

/*
   Define() method
   where specific data definitions are established also will include the definition of the table 
   being created as well as defining rows and columns of the table
*/
public static void Define()
{
    Console.WriteLine("DEFINE");

    try
    {
        //Creation of the table with the specified data types and the column Names 
        Console.WriteLine("\tCreating table...");

        cmd.CommandText = "CREATE TABLE testData (" +
           "Trackingid VARCHAR(35)," +
           "dates DATETIME," +
           "Name_Of_Recipient VARCHAR(35)," +
           "addresss VARCHAR(35)," +
           "address2 VARCHAR(31)," +
           "City VARCHAR(34)," +
           "Town VARCHAR(23)," +
           "states VARCHAR(5)," +
           "ZipCode INT )";
        cmd.ExecuteNonQuery();

    }
    catch(CtreeSqlException e)
    {
        Console.WriteLine(e + " could not define this table");
    }
    catch(Exception e)
    {
        Console.WriteLine(e + "Error the table could not be defined");
    }
}

Like I have mentioned before I did try and step through the code multiple times, I am assuming that it could be that I have to many column entries? 就像我在尝试多次遍历代码之前所提到的那样,我假设这可能是我必须有许多列条目吗? or Relying on the VARCHAR data type heavily? 还是严重依赖VARCHAR数据类型?

Exception message being thrown: 抛出异常消息:

"        Creating table...
Ctree.Data.SqlClient.CtreeSqlException (0x7FFFB1DD): Syntax error ---> Ctree.SqlClient.Common.FcSqlException: Syntax error
   at Ctree.SqlClient.FcSqlXApi.SQLExec(FcStatement stmt, Int32 InStatementType, FcSqlDA ida, FcSqlDA oda, FcSqlCA sqlca)
   at Ctree.SqlClient.FcSqlXApi.Prepare(FcStatement stmt, FcSqlDA input_sqlda, FcSqlDA output_sqlda, Int32 fetchSize)
   at Ctree.SqlClient.FcConnection.Prepare(FcStatement statement, FcSqlDA inputDA, FcSqlDA outputDA, Int32 fetchSize)
   at Ctree.SqlClient.FcPreparedStatement..ctor(FcConnection connexion, String sql, Int32 fetchSize, Int32 timeout)
   at Ctree.Data.SqlClient.CtreeSqlCommand.InternalPrepare(Boolean resultSet)
   at Ctree.Data.SqlClient.CtreeSqlCommand.ExecuteNonQuery()
   at Ctree.Data.SqlClient.CtreeSqlCommand.ExecuteNonQuery()
   at DBbeta.Program.Define() in C:\Users\WVX0DYQ\source\repos\DBbeta\DBbeta\Program.cs:line 76 could not define this table"

First check whether table exists or not. 首先检查表是否存在。 If not exists then it create the table. 如果不存在,则创建表。

This is running example put your values and run this 这个正在运行的示例将您的值并运行

And use INTEGER not int See this for Datatypes 并使用INTEGER not int 参见此数据类型

var commandStr= "CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime)";

using (SqlCommand command = new SqlCommand(commandStr, con))
command.ExecuteNonQuery();

Find reference 查找参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM