问题是这样的,我定义个一字符串email,想通过查询数据库,获得一条数据,然后把该获得的数据的某个字段的值赋值给字符串email,然后再赋值到session["email"]里面,这样怎样操作呢??
大虾们帮忙一下,是c#语言来的,下面给的是代码;
        string name = TextName.Text;
        string pwd = TextPassword.Text;
        string email = "";
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings"DatabaseConnectionString"].ConnectionString);
        SqlCommand command = new SqlCommand("SelectUser", conn);
        command.CommandType = CommandType.StoredProcedure;
        SqlParameter UserName = command.Parameters.Add("@name", SqlDbType.VarChar, 50);
        UserName.Value = TextName.Text;
        SqlParameter UserPwd = command.Parameters.Add("@pwd", SqlDbType.VarChar, 50);
        UserPwd.Value = TextPassword.Text;
        conn.Open();
        try
        {
            SqlDataReader dr = command.ExecuteReader();
            if (dr.Read() == true)
            {   Session["userName"] = name;
                
                Session["Email"] = 
                Response.Redirect(Request.CurrentExecutionFilePath.ToString ());                              
            }
            else
            {   Session["userName"] = "";
                 }
            dr.Close();
        }
        catch (Exception err)
        { Response.Write("<script>window.alert(' 系统错误'+err.message);</script>"); }
        conn.Close();

解决方案 »

  1.   

    Session["Email"] = dr["字段名"];
      

  2.   

    LZ:
    你的SP的返回值是Email的话就好说了。
    这样写:        string name = TextName.Text; 
            string pwd = TextPassword.Text; 
            string email = ""; 
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings"DatabaseConnectionString"].ConnectionString); 
            SqlCommand command = new SqlCommand("SelectUser", conn); 
            command.CommandType = CommandType.StoredProcedure; 
            SqlParameter UserName = command.Parameters.Add("@name", SqlDbType.VarChar, 50); 
            UserName.Value = TextName.Text; 
            command.Parameters.Add(UserName ); // important
            SqlParameter UserPwd = command.Parameters.Add("@pwd", SqlDbType.VarChar, 50); 
            UserPwd.Value = TextPassword.Text; 
            command.Parameters.Add(UserPwd ); // important
            // add. Email is return value of SP"SelectUser"
            SqlParameter UserEmail= command.Parameters.Add("@email", SqlDbType.VarChar, 50); 
            command.Parameters.Add(UserEmail);
             // add end        conn.Open(); 
            try 
            { 
                SqlDataReader dr = command.ExecuteReader(); 
                if (dr.Read() == true) 
                {  Session["userName"] = name; 
                   // add  
                    string email = command.Parameters["UserEmail"].Value.ToString();
                    Session["Email"] = email ;
                    // add end
                    Response.Redirect(Request.CurrentExecutionFilePath.ToString ());                              
                } 
                else 
                {  Session["userName"] = ""; 
                    } 
                dr.Close(); 
            } 
            catch (Exception err) 
            { Response.Write(" <script>window.alert(' 系统错误'+err.message); </script>"); } 
            conn.Close(); 
      

  3.   

    Session["Email"] = dr["字段编号"].ToString();