从SQL Server表返回特定值

时间:2015-07-21 15:40:57

标签: c# sql asp.net sql-server

我正在尝试使用一个函数来检查电子邮件是否存在,我想要返回用户的姓名和电子邮件。我该怎么做?我应该如何处理现有代码?我怎样才能将用户名和电子邮件传递给aspx页面?

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   String select = request.getParameter("select");
   request.setAttribute("selected", select);
   request.getRequestDispatcher("/select.jsp").forward(request, response);

}

1 个答案:

答案 0 :(得分:2)

你可以使用这样的东西......

public static Tuple<string,string> searchEmail(string email)
{
    string userName = "";
    connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(connectionString);
    {
        conn.Open();              

        using (SqlCommand comm = new SqlCommand("select UserName from Member where email = @email", conn))
        {
            SqlDataReader reader;
            comm.Parameters.AddWithValue("@email", email);

            reader = comm.ExecuteReader();
            if (reader.HasRows)
            {
              while (reader.Read())
              {                                
                 userName = reader.GetString(0);
              }
            }
        }          
    }
     conn.Close();
     return new Tuple<string,string>(email,userName);
}