ConstraintException但我不知道为什么!

时间:2011-01-25 12:17:41

标签: c# .net mysql

当我使用一组特定的参数运行以下代码时,我得到一个未处理的约束异常:

using (MySqlConnection connMySql = new MySqlConnection(global.g_connString))
            {

                MySqlCommand cmd = connMySql.CreateCommand();

                cmd.CommandText = this.Query;

                connMySql.Open();

                using (MySqlDataReader dr = cmd.ExecuteReader())
                {

                    DataTable dt = new DataTable();
                    dt.Load(dr);
                    return dt;
                }

但是,如果我直接运行查询(即不在我的应用程序上但使用查询浏览器),我看不到任何空值或任何会产生错误的内容。

它必须是特定于数据的,就像我更改查询的日期范围一样,它可以正常工作。

有人有任何想法吗?!

谢谢,

PS查询如下:

SELECT COALESCE(ti.FIRST_NAME, 'Not Assigned') AS 'Technician',wo.WORKORDERID 'Request ID',aau.FIRST_NAME 'Requester', wo.TITLE 'Subject', rrs.resolution As Resolution, (wo.COMPLETEDTIME/1000) 'TimeStamp'
                                            FROM WorkOrder_Threaded wot
                                            INNER JOIN WorkOrder wo ON wot.WORKORDERID=wo.WORKORDERID
                                            LEFT JOIN SDUser sdu ON wo.REQUESTERID=sdu.USERID
                                            LEFT JOIN AaaUser aau ON sdu.USERID=aau.USER_ID
                                            LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
                                            LEFT JOIN SDUser td ON wos.OWNERID=td.USERID
                                            LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID
                                            LEFT JOIN RequestResolution rrs ON wo.WORKORDERID=rrs.REQUESTID
                                        WHERE (wo.COMPLETEDTIME != 0) AND (wo.COMPLETEDTIME != -1) AND (wo.COMPLETEDTIME IS NOT NULL)
                                            AND wo.COMPLETEDTIME >= (UNIX_TIMESTAMP(TIMESTAMP('" + sdChartRange.From + @"')) * 1000)
                                            AND wo.COMPLETEDTIME <= (UNIX_TIMESTAMP(TIMESTAMP('" + sdChartRange.To + @"')) * 1000)
                                            AND wot.THD_WOID=wot.WORKORDERID
                                        ORDER BY Technician ASC

其中sdChartRange.From和.To是日期时间值。

3 个答案:

答案 0 :(得分:0)

如果不知道您的表的架构以及您调用它的查询,很难说。你确定他们是完美的匹配吗?

答案 1 :(得分:0)

我建议您调用dataTable.GetErrors();来获取错误行,然后检查/记录每个错误行的RowError属性。

答案 2 :(得分:0)

尝试另一种加载DataTable的方法,以避免来自数据库的意外约束。在函数中试试这个。

MySqlCommand cmd = connMySql.CreateCommand();
cmd.CommandText = this.Query;

connMySql.Open();

DataTable dataTable;

// Create data adapter
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);

// Populate dataTable based on DB query
da.Fill(dataTable);
da.Dispose();

return dataTable;

另见:C# Sqlite throwing ConstraintException with DataTable