Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言调用存储过程(发送邮件)#_C#_Sql Server 2008_Stored Procedures - Fatal编程技术网

C# 用C语言调用存储过程(发送邮件)#

C# 用C语言调用存储过程(发送邮件)#,c#,sql-server-2008,stored-procedures,C#,Sql Server 2008,Stored Procedures,我正在使用VisualStudio2010、SQLServer2008,我创建了一个发送邮件的应用程序,现在我的查询是一个简单的select,但我需要使用一个存储过程,如何替换存储过程中的select 这是我的密码: public List<string> SqlSelectMails() { List<string> dir_mails = new List<string>(); **string stSql = "select mail_

我正在使用VisualStudio2010、SQLServer2008,我创建了一个发送邮件的应用程序,现在我的查询是一个简单的select,但我需要使用一个存储过程,如何替换存储过程中的select

这是我的密码:

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;
}

首先,需要在数据库中创建存储过程(使用Sql Management Studio)

然后需要从代码中调用它

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());
    }
}

当然,将此代码集成到现有代码中取决于您。特别是如何替换对
mibd.sqlExecute(stSql)的调用

确切地说,为什么需要将其设置为存储过程?
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
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());
    }
}