2012-03-29 2 views
2

tbl_Ticket에서 Ticket_Id를 검색하여 전자 메일 기능 전송 본문 섹션으로 전달해야합니다. 아래 코드가 맞습니까? 내가 TICKET_ID 1 얻을 모든 시간 ..함수에서 select 쿼리에서 int 값을 반환하는 방법은 무엇입니까?

public int select_TicketId(){ 
    string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString.ToString(); 
    SqlConnection sqlCon = new SqlConnection(strConn); 
    string getId = ("select Ticket_Id from tbl_Ticket where Client_EmailAdd='" + objNewTic_BAL.email + "' "); 
    sqlCon.Open(); 
    SqlCommand cmd1 = new SqlCommand(getId, sqlCon); 
    int i=cmd1.ExecuteNonQuery(); 
    return i; 
} 

답변

2

를 호출해야합니다. 그러나 그것은 하나의 질의입니다. 이 때문에, 일부 경고 벨 :

대신 ExecuteScalar을 시도하고 int로 결과를 캐스팅 ... 당신은뿐만 아니라 명령 및 연결 using 문을 사용한다

return (int) cmd1.ExecuteScalar(); 

주 렁해야 둘 다 적절히 닫혀있다.

그리고 당신은 당신의 SQL에 직접 값을 포함하는 대신 확실히 사용 매개 변수화 된 SQL해야한다 (내가 전에이 문제를 발견하지 않았다). 그렇지 않으면

... SQL Injection attacks에 열려있어 그래서 같은 것을 : 당신은 정확히 하나 개의 결과는하지만이 경우 일어날 원하는 것을 고려해야한다

private const string FetchTicketIdSql = 
    "select Ticket_Id from tbl_Ticket where Client_EmailAdd = @Email"; 

public int FetchTicketId() 
{ 
    // No need for ToString call... 
    string connectionString = 
     ConfigurationManager.ConnectionStrings["conString"].ConnectionString; 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    { 
     connection.Open(); 
     using (SqlCommand command = new SqlCommand(connection, FetchTicketIdSql)) 
     { 
      command.Parameters.Add("@Email", SqlDbType.NVarChar).Value = 
       bjNewTic_BAL.email; 
      return (int) command.ExecuteScalar(); 
     } 
    } 
} 

...

+0

감사합니다.이 시도해 보겠습니다. :) – hks

+0

감사합니다. – hks

5

첫 번째 값을 반환하는 ExecuteScalar를 검색하고 있습니다.

public int select_TicketId() 
     { 
      string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString.ToString(); 
      SqlConnection sqlCon = new SqlConnection(strConn); 
      string getId = ("select TOP 1 Ticket_Id from tbl_Ticket where Client_EmailAdd='" + objNewTic_BAL.email + "' "); 
      sqlCon.Open(); 
      SqlCommand cmd1 = new SqlCommand(getId, sqlCon); 
      int i=Convert.ToInt32(cmd1.ExecuteScalar()); 
      return i; 

     } 

또한, 더 나은 보안을 위해 어디 문을 설정 아래와 같이 할 CommandProperties를 사용

public int select_TicketId() 
{ 
    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString; 
    int result = -1; 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    { 
     connection.Open(); 
     SqlCommand command = new SqlCommand(); 
     command.Connection = connection; 
     command.CommandType = CommandType.Text; 
     command.CommandText = "select TOP 1 Ticket_Id from tbl_Ticket where [email protected]"; 
     command.Parameters.Add("@email", SqlDbType.Text).Value = objNewTic_BAL.email; 
     result = Convert.ToInt32(command.ExecuteScalar()); 
    } 

    return result; 
} 
0

Hiral, 만족하는 레코드 수를 반환합니다

int i=cmd1.ExecuteNonQuery(); 

에서 ExecuteNonQuery는 당신의 질문. 이 경우 1 (또는 이메일이없는 경우 0)

대신 ExecuteReader를 사용해보십시오.

관련 문제