2014-11-18 2 views
-3

SQL 쿼리 연결에 문제가 있습니다. 버튼 클릭 이벤트 후 두 문자열을 연결하려면 어떻게해야합니까? 여기 SQL 쿼리 연결 C#

코드의 샘플입니다 :

private void Form1_Load(object sender, EventArgs e) 
{ 
    string sql = "select * from table"; //defaultquery 
    sql += querytail; //buttonclick event, concatenate query + querytail 
    NpgsqlConnection conn = new NpgsqlConnection(DSN()); 
    conn.Open(); 

    NpgsqlCommand command = new NpgsqlCommand(sql, conn); 
    // ......... etc 

} 
private void searchbutton9_Click(object sender, EventArgs e) 
{ 
    querytail = " where a = '" + filter(textBox1.Text.ToString()) + "'"; 
} 
+2

당신은 이것을 절대해서는 안됩니다. 당신은 SQL 삽입에 열중하고 있습니다. [Paramertized Queries] (http://stackoverflow.com/questions/5468425/how-do-parameterized-queries-help)를보십시오. -against-sql-injection) – Sam

+0

웹 응용 프로그램이 아닙니다! 자사의 Windows Form 응용 프로그램. –

+0

미안하지만 다른 질문을했습니다. 주사에 관한 것이 아닙니다! SQL 쿼리가 아닌 두 문자열을 concanate하려는 경우를 상상해보십시오. –

답변

0
당신은 할 수있는 방법을 만들 필요가 두 문자열

String result = "Hello" + " World!"; //result = "Hello World!"

또는

string result = string.Concat("Hello", " World", "!"); //result = "Hello World!"

에 의해 CONCAT 할 수

귀하의 질의를 작성하고 그것들에 물건을 추가하십시오. 그래서 같이 뭔가를해야만 :이처럼 사용할 수 있습니다

public string GetQuery(string mainQuery, params string[] queryAppend) 
{ 
    return mainQuery + string.Join("", queryAppend); 
} 

: string result = GetQuery("Hello", " World", "!"); //result = "Hello World!";

당신은 당신의 쿼리 문자열을 전달하고 모든 조항 수 있습니다.

+0

아니요,이 방법으로 문자열을 연결하지 않겠습니다. –

+0

그럼 어떻게 할 건데? –

+0

메서드 searchbutton9_Click에서 메서드 Form1_Load로 매개 변수 (이 경우 querytail)를 보내고 쿼리와 연결 한 다음 NpgsqlCommand를 다시 실행합니다. –