简体   繁体   English

为什么在执行时没有返回结果?

[英]Why Is this returning no results when executed?

SqlConnection conn = getConnection();
SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_PSLA_SEARCH"; //The stored procedure gets added
cmd.CommandTimeout = 0;
cmd.Connection = conn;

// Start adding the parameters of the stored procedure
cmd.Parameters.AddWithValue("@usrnm", thisUser.Username);

int constit = 0;

if (thisUser.Constituencies.Count > 0)
{
    foreach (KeyValuePair<int, string> kp in thisUser.Constituencies)
    {
        if (kp.Value == ddlConstituency.SelectedValue.ToString())
        {
            constit = kp.Key;
            break;
        }
    }
}

cmd.Parameters.AddWithValue("@cnstncy", constit);

string pdval = null;
int valtype = 0;

if (rbsearchradios.SelectedIndex == 0)
{
    try
    {
        pdval = searchVal;
        cmd.Parameters.AddWithValue("@Search", DBNull.Value);
        cmd.Parameters.AddWithValue("@pd", int.Parse(pdval));
        cmd.Parameters.AddWithValue("@type", valtype);
    }
    catch
    {
        System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "stop", "alert('Invalid PD Number Supplied! Please Provide A Valid Submission.');", true);
        return;
    }
}
else
{
    valtype = 1;
    cmd.Parameters.AddWithValue("@Search", searchVal);
    cmd.Parameters.AddWithValue("@pd", DBNull.Value);
    cmd.Parameters.AddWithValue("@type", valtype);
}

cmd.Parameters.AddWithValue("@app", 1);

conn.Open();                        

// Creates Dataadapter for execution
SqlDataAdapter dp2 = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

dp2.Fill(ds, "name");

I am trying to the arguments of the stored procedure and the having this stored procedure execute and get this results into a dataset but I get nothing.. Literally. 我正在尝试存储过程的参数,并使该存储过程执行并将结果存储到数据集中,但是我什么也没得到。 There are no exceptions, just no result from the stored procedure. 没有例外,只是存储过程没有结果。

This is the stored procedure: 这是存储过程:

DECLARE @return_value int

EXEC    @return_value = [dbo].[SP_PSLA_SEARCH] 
            @usrnm = N'tstone',
            @cnstncy = 55,
            @Search = N'primary',
            @pd = NULL,
            @type = 1,
            @app = 1

SELECT  'Return Value' = @return_value
GO

To troubleshoot: 解决方法:

  1. Make sure what values has each parameter and execute the same query directly against database in Sql Server Management Studio. 确保具有每个参数的值,并直接对Sql Server Management Studio中的数据库执行相同的查询。
  2. Check if you properly use the result from the dataset (it's not clear from the code) 检查您是否正确使用了数据集的结果(代码中不清楚)

In general, you can also try to simplify and make your code more clear: 通常,您还可以尝试简化代码并使代码更清晰:

  • the block with return and if (rbsearchradios.SelectedIndex == 0) can be moved at the beginning, it makes no sense to create SqlCommand and then break 具有return且if (rbsearchradios.SelectedIndex == 0)可以在开始时移动的块,因此创建SqlCommand然后中断就没有意义了
  • if SP returns only single value, you can use the ExecuteScalar() method, which is faster and straightforward. 如果SP仅返回单个值,则可以使用ExecuteScalar()方法,该方法更快,更直接。

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

相关问题 为什么Google距离矩阵API传递52时仅返回10个结果? - Why is google distance matrix API only returning 10 results, when passed 52? 为什么我所有的 Visual Studio 测试结果都“未执行” - Why are all my Visual Studio test results "Not executed" 迭代器块的奇怪测试覆盖率结果,为什么这些语句没有被执行? - Weird test coverage results for iterator block, why are these statements not executed? 为什么在使用foreach时不执行此LINQ查询? - Why is this LINQ query not executed when using foreach? 为什么MongoCursor不返回MongoCollection的所有结果? - Why is the MongoCursor not returning all results from a MongoCollection? 为什么我的嘲笑对象没有返回结果? - Why is my mocked object returning no results? 为什么ListBlobsSegmentedAsync仅在第二页上返回结果? - Why is ListBlobsSegmentedAsync only returning results on second page? 为什么Lucene的实现不返回任何结果? - Why is this implementaion of Lucene not returning any results? 为什么RijndaelManaged和AesCryptoServiceProvider会返回不同的结果? - Why are RijndaelManaged and AesCryptoServiceProvider returning different results? DateTime 在单独的 .cs 文件中执行时返回最小值 - DateTime returning min value when executed in separate .cs file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM