c# - 我的存储过程返回了一些东西,但是 sqlDataAdapter.Fill 将数据表留空

标签 c# sql-server sqlcommand

我的应用程序停止工作。我测试了我的存储过程,它确实返回了 3 行(太大而无法在此处重新发布)。在 Visual Studio 的输出中它之后说:

(1 row(s) affected)
(3 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[GetCaseDocumentFile].

为什么我不能将它放入名为dtResult 的数据表中?命令 sqlDataAdapter.Fill(dtResult) 应该可以做到。相反,我在 Debug模式下查看它时得到 {}。

string connectionString = @"Data Source=34.56.55.251;Initial Catalog=DBXXX;User ID=xx;Password=XXXXXXXX;";
string queryString = "GetCaseDocumentFile";

using (SqlConnection connection = new SqlConnection(connectionString))
            {
    SqlCommand command = new SqlCommand(queryString, connection);
    command.CommandType = System.Data.CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@key", 200012);

    connection.Open();

    // Works! Three lines are returned.
    SqlDataReader reader = command.ExecuteReader();
    try
    {
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }
    }
    finally
    {
        reader.Close();
    }

    // Does not work! dtResult remains empty. {}
    DataTable dtResult = new DataTable();
    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);
    sqlDataAdapter.Fill(dtResult);

    DataTable result = dtResult;
}

更新:

没关系。这确实有效。这是我的测试代码和我认为我的真实代码所做的。但我想我在实际代码中忽略了一些东西。

最佳答案

你在你的程序中没有设置计数吗??
设置无计数;

关于c# - 我的存储过程返回了一些东西,但是 sqlDataAdapter.Fill 将数据表留空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5511374/

相关文章:

.net - SqlCommand.CommandTimeout 和 SqlConnection.ConnectionTimeout 之间有什么区别?

mysql - 从存储过程调用的触发器(触发器的回滚会发生吗?)

sql-server - 将 nvarchar 值转换为 int 时转换失败

c# - 混淆 SqlCommand 和 SqlDataAdapter

c# - 如何将文件合并为单个可执行文件或减少分发文件的数量

sql-server - 将当前表列更新为标识列

sql-server - 我可以从 .NET 获取 SqlCommand 对象(带有 SQL 参数)生成的完整 SQL 字符串吗?

C# 11 - 通过反射检测所需的属性

c# - 我可以使用什么来获取超出 String.Format 的名称字符串模板?

c# - Asp.net core 3 Web Api post请求不起作用