简体   繁体   English

选择列等于数组中字符串值之一的行

[英]Select rows where column equals one of the string values in array

I'm trying to get relevant rows into datatable, i want only rows that have column value equal to one of the values in other sql table, that's my code: 我正在尝试将相关行放入数据表中,我只希望其列值等于其他sql表中的值之一的行,这就是我的代码:

try
{
     sc.Open();
     for (int i = 0; i < queryTokens.Length; i++)
     {
          cmd = new SqlCommand("INSERT INTO QueryIndex (Term) Values ('" + queryTokens[i] + "')", sc);
          cmd.ExecuteNonQuery();
     }
     cmd = new SqlCommand("SELECT * FROM TermsIndex Where Term EXIST (Select Term From QueryIndex)");
     SqlDataAdapter da = neuw SqlDataAdapter(cmd);
     da.Fill(tempTable);
     da.Dispose();
     sc.Close();
}
catch (Exception e)
{
     Console.WriteLine(e.ToString());
}

the first part of the insert going well i'm sure, but in the second part, i hope you all understand what i'm trying to do, i know i have syntax errors here so please correct me, and i'm getting this exception message: {"Value cannot be null.\\r\\nParameter name: dataTable"} Null exception.. 我敢肯定插入的第一部分进展顺利,但是在第二部分中,我希望大家都明白我要尝试做的事情,我知道我这里有语法错误,所以请更正我,我已经明白了异常消息:{“值不能为空。\\ r \\ n参数名称:dataTable”}空异常。

this is my sql create table: 这是我的SQL创建表:

CREATE TABLE [dbo].[TermsIndex]
(
    [ID] VARCHAR(35) NOT NULL,
    [Term] VARCHAR(35)  NOT NULL,
    [Frequency] int NOT NULL,
    [Offset] VARCHAR(MAX) NOT NULL,
)


CREATE TABLE [dbo].[QueryIndex]
(
    [Term] VARCHAR(MAX) NOT NULL,
)

so why i'm getting that null error? 那么,为什么我会收到空​​错误? and anyway i'm creating a table just to be able to compare it's entries with column from the first one, i already had it string array, any chance to be able to compare that column value to changing number of strings (tokens) and return row results if it were equal to one of those strings in sql command? 而且无论如何,我正在创建一个表,只是为了能够将其条目与第一个列中的列进行比较,我已经拥有了它的字符串数组,可以将该列值与更改的字符串数(令牌)进行比较并返回的任何机会行结果是否等于sql命令中的那些字符串之一? or any more elegance way to get the wanted result? 还是有更多优雅的方式来获得想要的结果?

I assume you have DataTable tempTable; 我假设您有DataTable tempTable; in your code. 在您的代码中。 You mast initialize it. 您将其初始化。

DataTable tempTable = new DataTable();

There are a few ways you can do the query: 有几种方法可以执行查询:

SELECT DISTINCT T.* FROM TermsIndex T
INNER JOIN QueryIndex Q ON T.Term = Q.Term;

SELECT T.* FROM TermsIndex T
WHERE T.Term IN (SELECT Q.Term FROM QueryIndex Q);

SELECT T.* FROM TermsIndex T
WHERE EXISTS(SELECT Q.Term FROM QueryIndex Q WHERE Q.Term = T.Term);

SELECT T.* FROM TermsIndex T
WHERE T.Term IN('term1', 'term2', 'term3');

Note: There's no dataTable in your posted code. 注意:您发布的代码中没有dataTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#DataTable Linq选择列等于“ x”的不同值 - C# DataTable Linq select distinct values where column equals 'x' Linq where element.equals一个数组 - Linq where element.equals one array EF Core:where 子句检查是否至少一个字符串列包含数组中的所有值 - EF Core: where clause to check if at least one string column contains all values in an array Datagridview以编程方式选择多行并将一列中的值求和 - Datagridview programmatically select multiple rows and sum up values in one column C#Linq选择ID等于CSV的行ID - C# Linq Select Rows Where ID Equals ID in CSV Acumatica - BQL Select 表字段,其中等于字符串 - Acumatica - BQL Select table field where equals string SQLite选择ID等于的位置 - SQLite Select Where ID Equals 如何选择列包含字符串中任何单词的所有行 - How can I Select all rows where column contain any words of a string MongoDB linq C#选择文档,其中子文档数组包含字符串数组中的所有值 - MongoDB linq C# select document where array of subdocumnets contain all values from string array npgsql:如何在一个以集合为参数的命令中使用 npgsql 选择多行(具有多个列值)? - npgsql: How to select multiple rows (with multiple column values) with npgsql in one command with a collection as a parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM