SQL Server CE查询无法在c#中执行,但在DBMS中有效

时间:2013-05-20 21:13:53

标签: c# sql sql-server-ce

我试图在sql查询中运行一堆SQL语句。查询和代码如下所示:

    private void cancelTestCaseCreationWizard()
    {
        string deleteTestCaseCmd = @"
                                    DELETE FROM column_correlation WHERE test_id IN (SELECT test_id FROM test WHERE test_case_id = @tcid); 
                                    DELETE FROM data_range WHERE test_id IN (SELECT test_id FROM test WHERE test_case_id = @tcid); 
                                    DELETE FROM permitted_values WHERE test_id IN (SELECT test_id FROM test WHERE test_case_id = @tcid); 
                                    DELETE FROM test WHERE test_case_id = @tcid; 
                                    DELETE FROM parameter WHERE parameter_set_id IN (SELECT parameter_set_id from parameter_set WHERE test_case_id = @tcid); 
                                    DELETE FROM parameter_set WHERE test_case_id = @tcid; 
                                    DELETE FROM test_case WHERE test_case_id = @tcid;";
        List<SqlCeParameter> param = new List<SqlCeParameter>();
        SqlCeParameter tcid = new SqlCeParameter("@tcid", SqlDbType.Int);
        tcid.Value = reservedTestCaseId;
        param.Add(tcid);
        DatabaseInteraction.DMLQuery(deleteTestCaseCmd, param);
        Dispose();
    }


    public static int DMLQuery(string query, List<SqlCeParameter> parameters)
    {
        int rowsAffected = -1;
        try
        {
            DBConn.Open();
            var tran = DBConn.BeginTransaction();
            SqlCeCommand comm = new SqlCeCommand(query, DBConn, tran);
            foreach (SqlCeParameter par in parameters)
            {
                comm.Parameters.Add(par);
            }
            comm.ExecuteNonQuery();
            tran.Commit();
        }
        catch (SqlCeException e)
        {
            MessageBox.Show("Error executing DML query \n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return -2;
        }
        DBConn.Close();
        return rowsAffected;
    }

执行上述代码后,我收到消息:

Error executing DML query 
There was an error parsing the query. [ Token line number = 3,Token line offset = 41,Token in error = DELETE ]

我查了@tcid的值,这是正确的。查询本身在DBMS中可以正常工作。什么可能导致此错误?

1 个答案:

答案 0 :(得分:2)

  

SQL Server Compact不支持批量查询执行。你必须   单独运行每个语句。您可以重用SqlCeCommand   对象执行多个查询。

http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/d6f1db96-8724-4376-990e-3f6da18c2d08/