2012-03-29 1 views
2

電子メール機能を送信する本文セクションに渡すためにtbl_TicketからTicket_Idを取得する必要があります。 以下のコードは正しいですか?私はTICKET_ID 1取得 すべての回..どのように関数内の選択クエリから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

int i=(int)cmd1.ExecuteScalar();メソッドを呼び出す必要があります。しかしそれは問い合わせです。これは

return (int) cmd1.ExecuteScalar(); 

...代わりにExecuteScalarを試してみてください、とintに結果をキャスト:)ようにあなたは、同様usingコマンドの文との接続を使用する必要があります注意を

をいくつかの警告の鐘を鳴らされている必要があります両方とも適切に閉じられる。

(私は前にこれを発見していなかった)あなたは間違いなくは直接SQLに値を含むパラメータ化されたSQLの代わりに使用する必要があります。あなたは正確に一つの結果はしかし...

+0

私はこれを試してみましょう:) – hks

+0

ありがとう、そんなに働く:) – hks

5
がない場合が起こるしたい 何を考慮すべきである

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(); 
     } 
    } 
} 

:そうしないと、だから... ...のようなもの

SQL Injection attacksに開放しています

最初の値を返す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を使用してみてください。

関連する問題