Select 那些来自 db 的电子邮件,如果他们想收到电子邮件
Select those emails from db if they wanted to get an email
我正在开发一个严重依赖电子邮件的系统,我正在尝试确定用户是否希望收到通知。
用户详细信息存储在 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();
}
}
它returns-1,但我有一个10.000排成一行。从这里我想从数据库中保存那些偏好设置为 10.000 的人的电子邮件地址,以便我可以将其添加到电子邮件列表中。
总结一下:检查所有行,如果其中一些行有 'yes',并从同一行保存它们的 'email'。
有人能给我指出正确的方向吗?谢谢。
为@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
返回仅影响 Update
、Insert
和 Delete
语句的行数。在您的情况下,您将始终获得 -1
,因为在 Select 语句 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;
}
}
如上所述,ExecuteNonQuery 就是这样做的 - 没有查询结果。
改为:
int recordsAffected = (int)command.ExecuteScalar();
我正在开发一个严重依赖电子邮件的系统,我正在尝试确定用户是否希望收到通知。
用户详细信息存储在 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();
}
}
它returns-1,但我有一个10.000排成一行。从这里我想从数据库中保存那些偏好设置为 10.000 的人的电子邮件地址,以便我可以将其添加到电子邮件列表中。
总结一下:检查所有行,如果其中一些行有 'yes',并从同一行保存它们的 'email'。
有人能给我指出正确的方向吗?谢谢。
为@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
返回仅影响 Update
、Insert
和 Delete
语句的行数。在您的情况下,您将始终获得 -1
,因为在 Select 语句 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;
}
}
如上所述,ExecuteNonQuery 就是这样做的 - 没有查询结果。
改为:
int recordsAffected = (int)command.ExecuteScalar();