2013-06-19 2 views
-5

데이터베이스에 'firstname'및 'lastname'이 저장된 목록 상자가 하나 있습니다.텍스트 상자에서 데이터베이스 값 선택

인덱스를 선택하는 데이터베이스에서 정보를 검색하려면 내 쿼리를 어떻게 작성합니까?

나는 여기

+0

작동하지 않는 기능은 무엇입니까? –

+0

질문을 명확히 해 주시겠습니까? 이름과 성은'목록 상자 '에 있으며 데이터베이스에서 가져온 것이지만 일부 세부 사항은 좋을 것입니다. –

답변

0

가 ... SQL에 LINQ를 사용하는 경우는 "여기서"

<asp:LinqDataSource 
      ContextTypeName="YourDataContext" 
      TableName="YourTable" 
      ID="DataSource" 
      runat="server" 
      Where="FirstName == @FN"> 
      <WhereParameters> 
       <asp:ControlParameter Name="FN" ControlID="LISTBOX" 
             Type="String" PropertyName="Text" /> 
      </WhereParameters> 
</asp:LinqDataSource> 
1

무엇 당신이 가진 것은 옆으로 좋은 시작처럼 보이는 것입니다

"Select * from table where firstname= '"+listbox1.Text+"' and lastname= '"+listbox1.Text+"' " 

그나마 작동하지만이 SQL Injection 취약점. 나는 매개 변수화 된 쿼리를 읽었을 것이다. This is an article 나는 컨셉을 잘 넘는다.

SqlConnection, SqlCommand, SqlParameter 및 아마도 다른 몇 가지 클래스를 조합하여 사용하게 될 것입니다.

1

두 단어 (두 가지 방법) : Parameterized Queries 또는 Prepared Statements. 당신이 부르는 것은 당신이 선호하는 것에 달려 있습니다 - 그들은 똑같은 것을 의미합니다! 성능 및 보안 이점을 사용할 수 있습니다.

귀하의 질문에 내가 읽은 것으로 가정하겠습니다. 귀하의 의견입니다. listbox 컨트롤에는 데이터베이스에있는 이름 목록이 포함되어 있는데 여기에는 listbox에 단지 Items으로 포함되어 있습니다.

My interpretation

이는 것이 좋습니다 디자인이 아니라 당신이 이미 가지고있는의 재 작업없이 그것을 기능을 할 수 있습니다. 이는 것이 좋습니다 디자인 선택이 아닌 이유

string commandText = "SELECT * FROM table WHERE firstname = @firstname and lastname = @lastname"; 
using (SqlConnection connection = new SqlConnection(connectionString)) 
{ 
    SqlCommand command = new SqlCommand(commandText, connection); 
    if (listbox1.SelectedItem == null) 
    { 
     // exit since nothing was selected in the listbox to query by 
    } 
    var names = listbox1.SelectedItem.ToString().Split(' '); 
    if (names.Length != 2) 
    { 
     // exit since this is not what we want and will yield unexpected results 
    } 
    string firstName = names[0]; 
    string lastName = names[1]; 
    command.Parameters.AddWithValue("@firstname", firstname); 
    command.Parameters.AddWithValue("@lastname", lastname); 

    // Read information from database... 

} 

은 분명히 당신이 볼 수 있지만, 그것은 당신이 이미 설정 한 내용을 주어진 작동합니다

SQL ServerC#를 들어, 코드는 다음과 같이 될 것이다.

관련 문제