[英]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.