C#检测电子邮件是否未注册

时间:2014-07-28 16:54:38

标签: c#

我是C#的新手,我需要一些代码帮助

protected void btSubscribe_Click ( object sender, EventArgs e ) {
    try {
        string activationKey = Guid.NewGuid().ToString();
        bool exists = false;
        string fileName = Server.MapPath("~/Database/db.xlsx");
        string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" + 
    "Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName);
        using(OleDbConnection cn = new OleDbConnection(connectionString)) {
            try {
                cn.Open();
                using(OleDbCommand cmd = new OleDbCommand("SELECT count(*) FROM [subscritionEmails$] WHERE [COLUMN1] = @email", cn)) {
                    cmd.Parameters.AddWithValue("@email", subscriberEmail.Text.Trim());
                    exists = (int)cmd.ExecuteScalar() > 0;
                }
                Console.WriteLine(exists);
                if(exists) {
                    errorMessage.InnerHtml = "O email que foi indicado já existe na nossa base de dados.";
                } else {
                    try {
                        using(OleDbCommand cmd1 = new OleDbCommand("INSERT INTO [subscritionEmails$] " + "([COLUMN1],[COLUMN2],[COLUMN3]) " + "VALUES('@value1', '@value2','0')", cn)) {
                            cmd1.Parameters.AddWithValue("@value1", subscriberEmail.Text);
                            cmd1.Parameters.AddWithValue("@value2", activationKey);
                            cmd1.ExecuteNonQuery();
                        }
                        //EnviarEmails(subscriberEmail.Text, activationKey);
                        errorMessage.InnerHtml = "O seu pedido foi registado com sucesso. Obrigado.";
                        subscriberEmail.Text = "";
                        subscriberName.Text = "";
                    } catch(Exception data) {
                        if(ConfigurationManager.AppSettings["ShowErrors"].ToString() == "1") {
                            errorMessage.InnerHtml = data.ToString();
                        } else {
                            errorMessage.InnerHtml = "Lamentamos mas não foi possível registar o seu pedido. P.f. tente novamente.";
                        }
                    }
                }
            } catch(Exception conError) {
                if(ConfigurationManager.AppSettings["ShowErrors"].ToString() == "1") {
                    errorMessage.InnerHtml = conError.ToString();
                } else {
                    errorMessage.InnerHtml = "Lamentamos mas não foi possível registar o seu pedido. P.f. tente novamente.";
                }
            } finally {
                cn.Close();
            }
        }
    } catch(Exception eMail) {
        if(ConfigurationManager.AppSettings["ShowErrors"].ToString() == "1") {
            errorMessage.InnerHtml = eMail.ToString();
        } else {
            errorMessage.InnerHtml = "Lamentamos mas não foi possível registar o seu pedido. P.f. tente novamente.";
        }
    } finally {
        errorMessage.Visible = true;
        //Page.ClimentScript.RegisterStartupScript(this.GetType(), "JS1", "window.top.location = ('WebForm1.aspx');", true);
        UpdateNews.Update();
    }
}

好的,谢谢@Steve我已经修复了代码的第二部分,但是现在我添加电子邮件并再次尝试添加它之后,它会向我显示已添加电子邮件的消息,但是当我打开xlsx时,文件仍然是相同的,没有添加任何内容......

3 个答案:

答案 0 :(得分:1)

您确定[Column1]是正确的列名吗?当列名与查询中使用的名称不匹配时,我收到此错误。

答案 1 :(得分:1)

查看您的连接字符串,您有HDR = YES。这意味着工作表的第一行应包含列名。所以你有两个选择。

  1. 在您的第一行插入列名COLUMN1和COLUMN2 片
  2. 删除HDR = YES,但将查询更改为

    "SELECT count(*) FROM [subscribers$] WHERE [F1] = @email"
    
  3. 的第二个查询
        "INSERT INTO [Sheet1$] " + "([F1],[F2]) VALUES(@value1, @value2)"
    

答案 2 :(得分:-2)

尝试将第33行拆分为两行。在第一个中,将(int)cmd.ExecuteScalar()保存为int。在第二步中,将bool exists的int与0进行比较。

相关问题