Question

I tried to select some data from one data table and save it to another based on a condition. I am getting the error - System.Data.SyntaxErrorException: Cannot interpret token '!' at position 6.

What is the correct syntax and where can I see a list of C# statements that do queries like SQL server.

        DataTable parent = some datatable from SQL server;
        DataTable child = new DataTable();
        child = parent.Select("cols != -1").CopyToDataTable();
Était-ce utile?

La solution

Why do you select a table from sql-server which you first need to filter at all? Can't you add this to the WHERE-clause?

With .NET 3.5 you should be able to use Linq-To-DataSet which is much more powerful and readable:

DataTable parent = GetTable();
DataTable child = parent.AsEnumerable()
    .Where(row => row.Field<int>("cols") != -1)
    .CopyToDataTable();

If you want to use DataTable.Select anyway you have to use <> instead of !=.

DataRow[] filtered = parent.Select("cols <> -1");

Have a look here for the expression syntax: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.110).aspx

When you create comparison expressions, the following operators are allowed:

<
>
<=
>=
<>
=
IN
LIKE

The following arithmetic operators are also supported in expressions:

+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top