在变量中获取电子邮件地址 [英] fetching email address in a variable

查看:83
本文介绍了在变量中获取电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中检索电子邮件地址,将其放入字符串类型的变量中,然后在该电子邮件地址上发送邮件,该电子邮件地址已成功检索,但是当我在send mail方法中写入此变量时,它显示错误使用未分配的局部变量",代码如下所示.

I want to retrieve an email address from the database , strore it in a variable of string type and then send mail on that email address,the email address is successfully rerieved but ehen i write this variable in send mail method it shows error "use of unassigned local variable", the code is shown below.

con.Open();
        string addquery = "SELECT  [EMAIL] FROM [DATABASE NAME].[dbo].[TABLE NAME] where Column Name=''" + txtRegEmp.Text + "''";
        //string query = "select EMAIL from USER where REGNO_EMPID = ''" + txtRRegEmp.Text + "''";
        SqlCommand sqlcom = new SqlCommand(addquery, con);
        SqlDataReader myreader;
        String emailad;
        myreader = sqlcom.ExecuteReader();
        while (myreader.Read())
        {
            emailad = myreader[0].ToString();
           
        }
        myreader.Close();
        con.Close();
        //sending email button code 
       Response.Write(SendMail("abc@gmail.com", "emailad", "abc@gmail.com", "Good work done"));



它显示emailad错误

请在这方面指导我



it shows error of emailad

please guide me in this regard

推荐答案

如果要从数据库中选择单个值,可以使用ExecuteScalar()命令
来自MSDN的ExecuteScalar() [
If you want to select single value from Database you can use ExecuteScalar() Command
ExecuteScalar() from MSDN[^]

check the sample
SqlConnection con = new SqlConnection("yourConnectionString");
            string sqlcomd = string.Format("SELECT  email FROM  table WHERE  userid =''{0}''", "65");
            con.Open();
            SqlCommand cmd = new SqlCommand(sqlcomd, con);
            string emailId = string.Empty;
            try
            {
                emailId = Convert.ToString(cmd.ExecuteScalar());
            }
            finally
            {
                con.Close();
            }


永远不要尝试任何SqlReader来提取一个值...
使用这个:
don''t ever try any SqlReader to extract just one value...
use this:
SqlCommand sqlcom = new SqlCommand(addquery, con);

 String emailad;
 emailad = (string)sqlcom.ExecuteScalar( );
SendMail(.., emailad, ..);



特别查看您在"emailad"周围的引号中所使用的方法SendMail
请勿使用ttys



Check out your method SendMail espicially on your quotes around "emailad"
don''t use quites


这篇关于在变量中获取电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆