执行存储过程并在C#中接收“文本结果”

时间:2018-09-26 11:57:22

标签: c# sql sql-server stored-procedures

当前,我们有以下应该自动执行的手动过程:

  1. 数据库管理员执行存储过程“ StudyCloning” 在SSMS中显示“结果为文本”。
  2. 数据库管理员复制     将结果保存到文本文件中,并将其保存以用于文档目的

结果看起来像这样:

@GPMUserID: 100001721
SiteID      UserID      Active Status      ProfileName                                        Password                                                                                                                                                                                                                                                         UserName                                                               FirstName            LastName                                           LanguageID  TitleID     PayrollID   EmployeeID  DepartmentID DefaultFunctionID WorkPhone                                          WorkFax                                            WorkPager                                          WorkCell                                           WorkEMail                                                                                            Initials UDF1                                    CreatedBy                                          CreatedByDate           LastUpdatedBy                                      LastUpdatedByDate       RowVersion         RowVersionSave CDS_User_ID GlobalUserGUID                       LocalUserName
----------- ----------- ------ ----------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------- -------------------- -------------------------------------------------- ----------- ----------- ----------- ----------- ------------ ----------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------- --------------------------------------- -------------------------------------------------- ----------------------- -------------------------------------------------- ----------------------- ------------------ -------------- ----------- ------------------------------------ --------------------------------------------------
1           100001721   1      1           EMEA\muege.******                                  0                                                                                                                                                                                                                                                                *****, Müge                                                            Müge                 *****                                              0           99          0           100001721   0            0                                                                                                                                                                                                                             muege.******@************.com                                                                                 0.0000                                  EMEA\m********                                     2017-06-06 14:36:44.380 EMEA\m********                                     2017-06-06 14:36:44.380 0x0000000027DD32A2 NULL           100001721   DD9DEACB-B44A-E711-80C5-005056B01C75 muege.******

(1 row affected)

@GPMUserID: 191742
SiteID      UserID      Active Status      ProfileName                                        Password                                                                                                                                                                                                                                                         UserName                                                               FirstName            LastName                                           LanguageID  TitleID     PayrollID   EmployeeID  DepartmentID DefaultFunctionID WorkPhone                                          WorkFax                                            WorkPager                                          WorkCell                                           WorkEMail                                                                                            Initials UDF1                                    CreatedBy                                          CreatedByDate           LastUpdatedBy                                      LastUpdatedByDate       RowVersion         RowVersionSave CDS_User_ID GlobalUserGUID                       LocalUserName
----------- ----------- ------ ----------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------- -------------------- -------------------------------------------------- ----------- ----------- ----------- ----------- ------------ ----------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------- --------------------------------------- -------------------------------------------------- ----------------------- -------------------------------------------------- ----------------------- ------------------ -------------- ----------- ------------------------------------ --------------------------------------------------
1           191742      1      1           EMEA\julian.********                               0                                                                                                                                                                                                                                                                ********, Julian                                                       Julian               ********                                           1           0           0           191742      0            0                                                                                                                                                                                                                             julian.********@************.com                                                                              0.0000                                  EMEA\muege.******                                  2018-05-08 10:54:44.957 EMEA\muege.********                                2018-05-08 10:54:44.957 0x0000000027DCD8FA NULL           191742      371CB873-9D52-E811-80CE-005056B01C75 julian.********

(1 row affected)

我当前正在使用以下代码执行存储过程:

SqlCommand command = new SqlCommand(@"CDS.dbo.StudyCloningTest", con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@FromSiteID", SqlDbType.Int).Value = GetSiteID(fromSite);
command.Parameters.Add("@FromStudyID", SqlDbType.Int).Value = studyID;
command.Parameters.Add("@ToSiteID", SqlDbType.Int).Value = GetSiteID(destSite);
command.Parameters.Add("@ToCustomerID", SqlDbType.VarChar, 20).Value = customerID;
command.Parameters.Add("@ToJobID", SqlDbType.Int).Value = jobID;
command.Parameters.Add("@ToUserProfile", SqlDbType.VarChar, 50).Value = assignee;
command.Parameters.Add("@CopyStudyItems", SqlDbType.Bit).Value = copyItems;
var transaction = con.BeginTransaction("InsertLeadRecord");
command.Transaction = transaction;
List<string> gpmUsers = new List<string>();
using (SqlDataReader resultQuery = command.ExecuteReader())
{
    while (resultQuery.NextResult())
    {
        while (resultQuery.Read())
        {
            /* Read Tables */
        }
    }
}
transaction.Commit();

尽管这正在读取结果表,但我还需要PRINT的输出以及(受影响的行)。这对于研究转移的文档记录至关重要。

此外,顺序非常重要。虽然此示例仅具有两个打印和表格,但输出简单,但是实际的存储过程在结果中放入了大量数据。

您有什么办法实现这一目标?

2 个答案:

答案 0 :(得分:3)

PRINT语句的输出本身并不是结果的一部分-它们发生在连接级别。要观察它们,您需要处理InfoMessage上的SqlConnection事件。

答案 1 :(得分:-1)

您可以使用具有默认格式的SSRS报告来完成工作,此外,您还可以控制打印输出格式。

只需几分钟即可完成。

谢谢。