简体   繁体   English

在C#中调用存储过程(发送邮件)

[英]Call stored procedure (sending mails) in C#

I´m working with Visual Studio 2010, SQL Server 2008, I created an application that sends mails, now my query is a simple select, but I need make it with a stored procedure, how can I replace that select for a stored procedure? 我正在使用Visual Studio 2010,SQL Server 2008,我创建了一个发送邮件的应用程序,现在我的查询是一个简单的选择,但我需要使用存储过程,我如何替换存储过程的选择?

Here is my code: 这是我的代码:

public List<string> SqlSelectMails()
{
    List<string> dir_mails = new List<string>();

    **string stSql = "select mail_usuario from dbo.mail_usuario_v2 where n_visita=0 and   
    aviso=1 order by banca";**

    Bd mibd = new Bd();
    SqlDataReader sdr = mibd.sqlExecute(stSql);

    while (sdr.Read())
    {
        dir_mails.Add(sdr["mail_usuario"].ToString());
    }
    return dir_mails;
}

I will like something like: 我会喜欢这样的东西:

**string stSql = "exec pa_rep_mail";**

First you need to create the stored procedure in your database (using Sql Management Studio) 首先,您需要在数据库中创建存储过程(使用Sql Management Studio)

CREATE PROCEDURE [dbo].[pa_rep_mail]
AS
    select mail_usuario 
    from dbo.mail_usuario_v2 
    where n_visita=0 and aviso=1 
    order by banca

then you need to call it from your code 那么你需要从你的代码中调用它

using(SqlConnection cn = new SqlConnection(constring))
{
    cn.Open();
    SqlCommand cmd = new SqlCommand("pa_rep_mail", cn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataReader sdr = cmd.ExecuteReader();
    while (sdr.Read())
    {
      dir_mails.Add(sdr["mail_usuario"].ToString());
    }
}

of course it is up to you to integrate this code in your existing code. 当然,您可以将此代码集成到现有代码中。 In particular how to replace the call to mibd.sqlExecute(stSql); 特别是如何替换对mibd.sqlExecute(stSql);的调用mibd.sqlExecute(stSql);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM