2017-11-19 8 views
1

私はVisual Studio 2013、C#およびSQL Serverデータベースを使用しています。パラメータの近くに不適切な構文

T-SQLコマンドは、パラメータを具体的な値に置き換えてもうまく動作します。

私は、コードの最後の行に、このエラーが出ます: 'Collection1 @'

付近に正しくない構文。

マイコード:

string myCommandString = "select Name, Collection, Text from List_Card @Collection1"; 
SqlConnection myConnection = new SqlConnection(connectionstring); 

SqlCommand myCommand = new SqlCommand(myCommandString, myConnection); 
SqlDataAdapter myydata = new SqlDataAdapter(); 

if (comboBox1.Text != "") 
{ 
    string1 = "where Collection IN (select Shortcut from Collections where Collection Like '" + comboBox1.Text + "')"; 
} 
else 
{ 
    string1 = ""; 
} 

myCommand.Parameters.Add(new SqlParameter("@Collection1", string1)); 
myydata.SelectCommand = myCommand; 

myConnection.Open(); 
DataTable myytab = new DataTable(); 
myydata.Fill(myytab); 
+2

これで間違いなく無効な構文 '文字列、何を達成しようとしています' '@ Collection1'をテーブル名の後ろに置いていますか? –

+2

T-SQLはデータベースエンジンではありません – Plutonix

+0

@Collectionに 'where'句があります。ユーザーがcomboBoxで何かを選択した場合、私は 'where'節が必要です。ユーザーがコンボボックスで何も選択しなかった場合は、「where」節は必要ありません。 – Matthew

答えて

3

コードにいくつかの誤りがあります。

まず、あなたのmyCommandString:

"select Name, Collection, Text from List_Card @Collection1" 

無効なSQL構文です(エラーはあなたが取得している何ですか)。あなたはこのパラメータで何もしていません。あなたはWHERE句の一部としてそれを置く必要がありますが、あなたはその値を使用していません。

次に、SqlParameterを完全に間違って使用しています。それを正しく使用する方法については、the documentationを参照してください。特定の問題は、条件付きSQL文字列を2番目のパラメータとして割り当てないことです。条件付きでクエリに追加する必要があります。

最後に、usingステートメントのすべてをラップして、オブジェクトを適切に処分する必要があります。

これは、あなたが探しているものをあなたを与える必要があります。myCommandString =「名前、コレクション、List_Card Collection1 @からテキストを選択し、」

var myCommandString = "select Name, Collection, Text from List_Card "; 

if (comboBox1.Text != "") 
{ 
    myCommandString += " where Collection IN (select Shortcut from Collections where Collection Like '@Collection1')"; 
} 

using (var myConnection = new SqlConnection(connectionstring)) 
using (var myCommand = new SqlCommand(myCommandString, myConnection)) 
{ 
    myCommand.Parameters.Add(new SqlParameter("@Collection1", string1)); 

    using (var myData = new SqlDataAdapter()) 
    { 
     myData.SelectCommand = myCommand; 
     myConnection.Open(); 

     var myytab = new DataTable(); 
     myydata.Fill(myytab); 
    } 
} 
0

あなたは、パラメータによる全体WHERE句を指定しないでください。パラメータは、... wellパラメータの代わりに使用できます。あなたがC#コード内の条件に基づいてWHERE句を追加したい場合は、次の操作を行います。

string myCommandString = "SELECT Name, Collection, Text FROM List_Card"; 
. 
. 
. 

if (comboBox1.Text != "") 
{ 
    myCommandString += " WHERE Collection IN (SELECT Shortcut FROM Collections WHERE Collection Like '" + comboBox1.Text + "')"; 
} 

また、それは、その使用後のオブジェクトのdisposeにスーパーが重要です。これを行う最も速い方法は、usingステートメントでコードを囲むことです。最後に、SQL injection attacksを防ぐために最善を尽くしてください。 ADO.NETの場合は、動的SQL問合せにParameterを追加するのが適切な方法です。

SqlConnectionSqlCommandSqlDataAdapterはIDisposableをあなたのコードは次のようになります。オブジェクトのすべてなので:

string myCommandString = "SELECT Name, Collection, Text FROM List_Card"; 

using (var myConnection = new SqlConnection(connectionstring)) 
using (var myCommand = new SqlCommand(myCommandString, myConnection)) 
using (var myydata = new SqlDataAdapter()) 
{ 
    if (comboBox1.Text != "") 
    { 
     myCommandString += " WHERE Collection IN (SELECT Shortcut FROM Collections WHERE Collection Like @Collection1)"; 
     myCommand.Parameters.Add(new SqlParameter("@Collection1", comboBox1.Text)); 
    } 

    myydata.SelectCommand = myCommand; 
    myConnection.Open(); 
    DataTable myytab = new DataTable(); 
    myydata.Fill(myytab); 
} 
+2

これは文字列の連結であり、SQLインジェクションが発生しやすくなります。安全で悪いクエリのやり方を奨励してはいけません。 – Nino

+0

これはOPが既に持っているものです。 SQLステートメントの構成はそのポストの主題ではありませんか? –

+0

Bozhidar Stoyneff、これもうまくいきました、ありがとう。 – Matthew

1

パラメータはそのように動作しません。私はあなたが同じクエリを持っていて、ユーザが何かを選択したときにwhere句を動的に追加したいと思っています。残念ながら、where節全体がパラメータであるようにすることはできません。

string myCommandString = @"select Name, Collection, Text from 
List_Card where Collection IN 
    (select Shortcut from Collections where Collection Like '%' + @collection + '%')"; 
SqlConnection myConnection = new SqlConnection(connectionstring); 
SqlCommand myCommand = new SqlCommand(myCommandString, myConnection); 
SqlDataAdapter myydata = new SqlDataAdapter(); 
myCommand.Parameters.Add(new SqlParameter("@Collection1", comboBox1.Text)); 
myydata.SelectCommand = myCommand; 
myConnection.Open(); 
DataTable myytab = new DataTable(); 
myydata.Fill(myytab); 
+0

IDisposableオブジェクトの処分をスキップすると、アプリケーションが不安定になるので、奨励しないでください。 –

関連する問題