Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 2.98 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I return a single value from an SqlDataReader?
  2. public  int Studentid()
  3.     {
  4.         try
  5.         {
  6.             SqlConnection con = new SqlConnection(connectionStr);
  7.             SqlCommand cmd = new SqlCommand("SELECT s_id FROM student where name = + ('" + Request.QueryString.ToString() + "')", con);
  8.             con.Open();
  9.             SqlDataReader dr = null;
  10.             con.Open();
  11.             dr = cmd.ExecuteReader();
  12.             if (dr.Read())
  13.             {
  14.                 //Want help herr how i return value
  15.             }
  16.  
  17.             con.Close();
  18.         }
  19.         catch (Exception ex)
  20.         {
  21.             throw ex;
  22.         }
  23.     }
  24.        
  25. public int GetStudentId()
  26. {
  27.     var sql = string.Format("SELECT s_id FROM student where name = '{0}'", Request.QueryString);
  28.     using (var con = new SqlConnection(connectionStr))
  29.     using (var cmd = new SqlCommand(sql, con))
  30.     {
  31.         con.Open();
  32.         var dr = cmd.ExecuteReader();
  33.         return dr.Read() ? return dr.GetInt32(0) : -1;
  34.     }
  35. }
  36.        
  37. int s_id = (int) dr["s_id"];
  38.        
  39. int studId=0;
  40. if(rdr.Read())
  41. {
  42.   studId=rdr.GetInt32(rdr.GetOrdinal("s_id"));
  43. }
  44.        
  45. public  int Studentid()
  46. {
  47.         try
  48.         {
  49.             SqlConnection con = new SqlConnection(connectionStr);
  50.             SqlCommand cmd = new SqlCommand("SELECT s_id FROM student where name = + ('" + Request.QueryString.ToString() + "')", con);
  51.             con.Open();
  52.             SqlDataReader dr = null;
  53.             con.Open();
  54.             dr = cmd.ExecuteReader();
  55.             if (dr.Read())
  56.             {
  57.                 return dr.GetInt32(0);
  58.             }
  59.  
  60.         }
  61.         catch (Exception ex)
  62.         {
  63.             throw ex;
  64.         }
  65.         finally
  66.         {
  67.             if(con != null)
  68.                  con.Close();
  69.             con = null;
  70.         }
  71.  }
  72.        
  73. if (dr.Read())
  74.             {
  75.                 //Want help hear how i return value
  76.                 int value = dr.GetInt32("s_id");
  77.             }
  78.        
  79. public  int Studentid() {
  80.   try {
  81.     using (SqlConnection con = new SqlConnection(connectionStr)) {
  82.       using (SqlCommand cmd = new SqlCommand("SELECT s_id FROM student where name = @Name", con)) {
  83.         cmd.Parameters.Add("@Name", DbType.VarChar, 50).Value = Request.QueryString.ToString();
  84.         con.Open();
  85.         using (SqlDataReader dr = cmd.ExecuteReader()) {
  86.           if (dr.Read()) {
  87.             return dr.GetInt32(0);
  88.           } else {
  89.             return -1; // some value to indicate a missing record
  90.             // or throw an exception
  91.           }
  92.         }
  93.       }
  94.     }
  95.   } catch (Exception ex) {
  96.     throw; // just as this, to rethrow with the stack trace intact
  97.   }
  98. }
  99.        
  100. public int StudentId()
  101. {
  102.     string sql = "SELECT s_id FROM student WHERE name = @name";
  103.     using (var con = new SqlConnection(connectionStr))
  104.     {
  105.         using (var cmd = new SqlCommand(sql, con))
  106.         {
  107.             cmd.Parameters.Add("@name", DbType.VarChar, 256).Value = Request.QueryString["name"];
  108.             con.Open();
  109.             return (int)cmd.ExecuteScalar();
  110.         }
  111.     }
  112. }