2017-11-19 2 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 :

(오류가 당신이 얻고있는 무엇) 유효하지 않은 SQL 구문은
"select Name, Collection, Text from List_Card @Collection1" 

. 매개 변수로 아무 것도하지 않습니다. WHERE 절의 일부로 넣어야하지만 그 값을 사용하지는 않습니다.

다음으로 SqlParameter을 완전히 잘못 사용하고 있습니다. 제대로 사용하는 방법을 보려면 the documentation을 확인하십시오. 특정 문제는 조건부 SQL 문자열을 두 번째 매개 변수로 지정하지 않는다는 것입니다. 조건부로 쿼리에 추가해야합니다.

마지막으로 개체를 올바르게 처리하려면 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 절을 지정하지 마십시오. ... 매개 변수 대신 매개 변수가 허용됩니다. 당신이 당신의 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을 추가하는 것이 올바른 방법입니다.

SqlConnection, SqlCommandSqlDataAdapter 때문에 모두는 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 개체의 처리를 건너 뛰면 불안정한 응용 프로그램이 생길 수 있으므로 권장하지 마십시오. –

관련 문제