-1
private SqlCommand createSQLQuery(SqlCommand command)
{
    string[] allTheseWords;
    if (textBoxAllTheseWords.Text.Length > 0)
    {
        allTheseWords = textBoxAllTheseWords.Text.Split(' ');
        string SQLQuery = "SELECT distinct [skullbase].[dbo].[patients].[name], [skullbase].[dbo].[patients].[dos], [skullbase].[dbo].[patients].[ACC2], [SKULLbase].[dbo].[fullreport].[mrn1], [SKULLbase].[dbo].[fullreport].[ACC], [skullbase].[dbo].[fullreport].[fullreport] FROM [skullbase].[dbo].[fullreport], [skullbase].[dbo].[patients] WHERE ";
        int i = 1;
        foreach (string word in allTheseWords)
        {
            command.Parameters.Add("@word" + i.ToString(), SqlDbType.Text).Value = word;
            SQLQuery = SQLQuery + " [skullbase].[dbo].[fullreport].[fullreport] LIKE @word" + i.ToString() + " AND ";
            i++;
        }
        SQLQuery = SQLQuery + " skullbase.dbo.patients.ACC2 = skullbase.dbo.fullreport.ACC";
        command.CommandText = SQLQuery;
    }
    MessageBox.Show(command.CommandText.ToString());
    return command;
}

以上是我的查询。“单词”一词没有被实际值取代。


allTheseWords = textBoxAllTheseWords.Text.Split(' ');
4

2 回答 2

4

对于初学者,当您在 SQL CommandText 中引用您的参数引用时(例如 ... [fullreport] = '@word'...),您实际上只是在使用字面值 value '@word'。它不会被解释为参数化查询。为此,您只需使用 ... [fullreport] = @word...)

其次,我不认为您可以像在循环中那样分配具有相同参数名称的多个参数。您添加的每个参数都应该有一个唯一的名称。

于 2013-01-28T18:17:26.160 回答
2

您为每个单词使用相同的参数名称。您应该为每个人使用不同的名称。您可能会考虑附加一个索引或其他类似的东西,以使其成为唯一的参数名称。

于 2013-01-28T18:16:53.250 回答