2012-03-08 19 views
1

では動作しません:私はタイプのSQLConnectionそのすべての作品の接続に対してクエリを実行select * from information_schema.TablesDatatable.Loadは、私は、このメソッドを持っているSqlCeReader

public static DataTable ExecuteDataTable(IDbConnection connection, string cmdText) 
{ 
    IDbCommand command = connection.CreateCommand(); 
    command.CommandText = cmdText; 
    command.CommandType = CommandType.Text; 
    IDataReader reader = command.ExecuteReader(); 
    DataTable dt = new DataTable(); 
    dt.Load(reader); 
    return dt; 
} 

。しかし 、私はタイプSqlCEConnectionの接続に対して行dt.Load(reader)それを実行しようと例外が発生します:確かに

System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. 
+1

いつも 'DataTable'を調べて、' ConstraintException'の原因を調べることができます。 'dt.Load(reader);'行でブレークポイントを設定してください。この行を実行するには[Quick-Watch Window](http://msdn.microsoft.com/en-us/library/cyzbs7s2.aspx)をクリックします。次に、 'dt.GetErrors()'でエラーのある行をすべて取得できます。次に、返された各DataRowの['RowError'](http://msdn.microsoft.com/en-us/library/system.data.datarow.rowerror.aspx)プロパティを調べて、実際の理由を確認することができます。 –

+0

GetErrors()メソッドのヒントをありがとう。私はこれを取得します:RowError: "列 'DATE_MODIFIED'はDBNull.Valueを許可しません。 SqlCEはSQL Server 2005よりもいくつかの列を返すようですが、それらはNULLのままです。 – amaters

答えて

1

奇妙な問題が、ここでは、代替です:

データセットとセットにデータを読みますEnforceConstraintsをfalseにします。次に返すことができますDataSet.Tables[0]

+0

ありがとうございます。私は今EnforceConstraints = falseと組み合わせてデータセットを使用してくれます。 – amaters

関連する問題