【问题标题】:Select those emails from db if they wanted to get an email如果他们想收到电子邮件,请从 db 中选择这些电子邮件
【发布时间】:2016-05-16 10:42:24
【问题描述】:

我正在开发一个严重依赖电子邮件的系统,我正在尝试确定用户是否希望收到通知。

用户详细信息存储在 SQL Server Express 中。我想检查哪些注册用户想要接收并从数据库中获取他们的电子邮件。这可能吗?

到目前为止,我已经做到了这一点:

using (SqlCommand command = new SqlCommand())
{
    command.Connection = connection;
    command.CommandType = CommandType.Text;
    command.CommandText = "SELECT COUNT(*) FROM [UserTable] WHERE ([price] = @price)";
    command.Parameters.AddWithValue("@price", "10.000");

    try
    {
        connection.Open();
        int recordsAffected = command.ExecuteNonQuery();
    }
    catch (SqlException ex)
    {
        MessageBox.Show("Error is SQL DB: " + ex);
    }
    finally
    {
        connection.Close();
    }
}

它返回 -1,但我在一行中有一个 10.000。从这里我想从数据库中保存那些有 10.000 首选项的人的电子邮件地址,以便我可以将其添加到电子邮件列表中。

总结一下:检查所有行,如果其中一些有“是”,并从同一行保存他们的“电子邮件”。

有人能指出我正确的方向吗?谢谢。


为@SeM 更新了它

private void getMailList()
{
    using (SqlConnection connection = new SqlConnection("Data Source=DESKTOP-9MMTAI1\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"))
    {
        try
        {
            connection.Open();
            using (SqlCommand cmd = connection.CreateCommand())
            {
                cmd.CommandText = "SELECT COUNT(*) FROM UserTable WHERE price = @price";
                cmd.Parameters.Add(new SqlParameter("@price", 10000));
                int count = int.Parse(cmd.ExecuteScalar());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error is SQL DB: " + ex);
            //Handle your exception;   
        }
    }
}

【问题讨论】:

  • ExecuteNonQuery 不是在这里调用的写东西。
  • ExecuteNonQuery 返回受影响的行数。
  • 而不是写try{} catch{} finally{}(在你的情况下),只需使用using(SqlConnection connection = new SqlConnection(yourConnectionString)){}
  • 只是出于好奇,很抱歉,如果这超出了范围,您为什么要使用“价格”列来确定某人是否想要接收电子邮件?这不是布尔字段的工作吗?
  • 不,这只是一个例子,这些字段不是英文的,所以我将它们更改为价格,但是是的,它们是数值,如果这就是你要问的。 @丹尼尔阿特伍德

标签: c# sql winforms sql-server-2014-express


【解决方案1】:

ExecuteNonQuery 返回仅对Update、Insert 和Delete 语句影响的行数。在你的情况下,你总是会得到-1,因为在选择语句ExecuteNonQuery返回-1

所以试试这个:

using(SqlConnection connection = new SqlConnection(connectionString))
{
    try
    {
        connection.Open();
        using(SqlCommand cmd = connection.CreateCommand())
        {
            cmd.CommandText = "SELECT COUNT(*) FROM UserTable WHERE price = @price";
            cmd.Parameters.Add(new SqlParameter("@price", 10000));
            int count = int.Parse(cmd.ExecuteScalar());         
        }
    }
    catch (Exception ex)
    {
        //Handle your exception;   
    }
} 

【讨论】:

  • 你也应该把你的SqlCommand放到using(....) { ... }块中,真的....
  • 'command.ExecuteScalar' 给出错误:无法从 'System.Data.SqlClient.SqlConnection' 转换为 'string'。我做错了吗?
  • @C.S.我已经更新了你的Updated it for @SeM代码,请检查一下。
【解决方案2】:

如上所述,ExecuteNonQuery 就是这样做的 - 没有查询结果。

改为:

int recordsAffected = (int)command.ExecuteScalar();

【讨论】:

    猜你喜欢
    • 2019-06-23
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    相关资源
    最近更新 更多