解析查询时出错。 [令牌行号= 1,令牌行偏移= 27,令牌错误= int]

时间:2014-02-22 04:56:55

标签: c#

我正在以编程方式创建一个.sdf文件,一个表,它工作正常,但是当我尝试插入时出现错误, 任何人都可以告诉我为什么我收到此错误 解析查询时出错。 [令牌行号= 1,令牌行偏移= 27,令牌错误= int] 这是我的代码

private void createDbAndInsert()
    {
        String PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        string connStr = "Data Source = " + PersonalFolder + "\\FooDatabase.sdf; Password = SomePassword";

        if (!File.Exists(PersonalFolder + "\\FooDatabase.sdf"))
        {
            SqlCeEngine engine = new SqlCeEngine(connStr);
            engine.CreateDatabase();

            SqlCeConnection conn = null;
            try
            {
                conn = new SqlCeConnection(connStr);
                conn.Open();

                SqlCeCommand cmd = conn.CreateCommand();
                cmd.CommandText = "CREATE TABLE FooTable(col1 int, col2 ntext)";
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occured.." + ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
        insertDataToTable();
    }

    private void insertDataToTable()
    {
        String PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string connStr = "Data Source = " + PersonalFolder + "\\FooDatabase.sdf; Password = SomePassword";

        SqlCeConnection con = new SqlCeConnection(@"Data Source = " + PersonalFolder + "\\FooDatabase.sdf; Password = SomePassword");
        con.Open();

        SqlCeCommand cm = new SqlCeCommand("INSERT INTO FooTable(col1 int, col2 ntext) values(@col1, @col2)", con);
        cm.Parameters.AddWithValue("@col1", 1);
        cm.Parameters.AddWithValue("@col2", "some text here");

        try
        {
            int affectedRows = cm.ExecuteNonQuery();
            if (affectedRows > 0)
            {
                MessageBox.Show("OK");

            }
            else
            {
                MessageBox.Show("Not OK");
            }
        }
        catch (SqlCeException ex)
        {
            // do something here.
        }
    }

3 个答案:

答案 0 :(得分:0)

不要为插入查询添加列数据类型

只需尝试使用此代码而不是插入查询

 SqlCeCommand cm = new SqlCeCommand("INSERT INTO FooTable(col1 , col2 ) values(@col1, @col2)", con);

你不需要这个

int affectedRows = cm.ExecuteNonQuery();

只需写下这行而不是上面的行

cm.ExecuteNonQuery();

因为您没有从插入查询中返回任何值。所以它可能会抛出转换错误

Cheer's;)

答案 1 :(得分:0)

问题:您在使用datatypes语句将项目插入表格时提供了列INSERT INTO,如下所示:

                            |          |
                            |          |
                            >          >
"INSERT INTO FooTable(col1 int, col2 ntext) values(@col1, @col2)"

解决方案:在将项目插入表格

时,不应提供列类型

替换它:

SqlCeCommand cm = new SqlCeCommand("INSERT INTO FooTable(col1 int, col2 ntext) values(@col1, @col2)", con);

有了这个:

SqlCeCommand cm = new SqlCeCommand("INSERT INTO FooTable(col1, col2) values(@col1, @col2)", con);

答案 2 :(得分:0)

你也在插入数据类型。这就是为什么它创建error.try这个插入查询。

SqlCeCommand cm = new SqlCeCommand("INSERT INTO FooTable(col1, col2) values(@col1, @col2)", con);
cm.Parameters.AddWithValue("@col1", 1);
cm.Parameters.AddWithValue("@col1", "some text here");