【问题标题】:Cannot implicitly convert type 'string' to 'int' in my Code [closed]无法在我的代码中将类型“string”隐式转换为“int”[关闭]
【发布时间】:2014-10-03 01:45:54
【问题描述】:

我试图向我数据库中的多个电子邮件地址发送电子邮件。我的代码如下所示

 private void button1_Click(object sender, EventArgs e)
    {


        ArrayList list_emails = new ArrayList();
        int i=0,email = 0;


        String condata = "data source =(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE))); user id = myuser; password = myuser";
        String qry = "select emailid from emailmachin";
        OracleConnection con = new OracleConnection(condata);
        OracleCommand cmd = new OracleCommand(qry, con);
        OracleDataReader myReader;
        try
        {
            con.Open();
            myReader = cmd.ExecuteReader();

            while (myReader.Read())
            {


                email = myReader.GetValue(i).ToString();
                list_emails.Add(email); //Add email to a arraylist
                i = i + 1 - 1; //increment or ++i
                //string emailIdc = myReader["emailid"].ToString();
                //textBoxTo.Text = emailIdc;
                myReader.Close();
                con.Close();
                foreach (string email_to in list_emails)
                {
                    MailMessage mail = new MailMessage();
                    mail.To.Add(email_to);
                    mail.Subject = "Welcome to C#";
                    mail.From = new MailAddress(hold_Account);
                    mail.Body = "Test";
                    SmtpClient smtp = new SmtpClient("SMTP Server");
                    smtp.Send(mail);
                }


            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

}

在这行代码email = myReader.GetValue(i).ToString(); 中显示错误:

无法将类型'string'隐式转换为'int'

【问题讨论】:

  • 我想知道你为什么将email 定义为int 类型呢?
  • 认为i = i + 1 - 1;会做什么?提示:它不会增加任何东西。
  • 这是我的另一个错误。非常感谢:)。

标签: c#


【解决方案1】:

myReader.GetValue(i).ToString() 不返回整数,而是返回字符串,因此需要将email 定义为string

int i = 0;
string email = string.Empty;

这可能是一个错误,因为所有不同的邮件变量,尝试使用更好的变量名称。

另外,您的代码没有正确增加i,它保持不变。试试i++; 而不是i = i + 1 - 1;

【讨论】:

    【解决方案2】:
    1. 如果你不知道用字符串、int、char 等来声明你的变量,别担心,只需输入“var”并让 C# 选择它的类型,我的朋友 :)

    2. 在 C# 中,字符串不能隐式转换为 int,您应该使用此函数显式执行此操作:Convert.ToInt32(your string variable or your string with quotation mark);

    3. 当你想向多个收件人发送一封电子邮件时,我认为你最好使用“BCC”(密件抄送)而不是“TO”,因为smtp.Send(mail); 只运行一次

    4. 查看How to send the mail from c#

    【讨论】:

    • 在不理解打字时建议使用 var 似乎是个好建议 :)
    • 但一开始总比拥有可以运行的东西要好,然后在审查、测试和编程部分的其他部分我们优化我们的代码,无论如何这是我的意见老兄,想想敏捷;)跨度>
    • int i=0,email = 0; 更改为var i=0,email = 0;(这就是您的建议,对吗?)有何帮助?这不会消除错误,它会添加一个。
    • 不,亲爱的 svick,没有初始化是真的 :)。我多次使用这种类型,在我的 VS 中它可以工作,也许在你的 VS 中它不起作用;),因此我试图提供帮助,所以请阅读整个答案并发表评论,无论如何,非常感谢阅读我的第一段:)
    • @Reza.Ace 我并不是说你应该使用var,我也一直在使用它。但是说“如果你不知道,试试var”是一个糟糕的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多