7

これが null を返すのはなぜですか?

//seedDate is set to DateTime.Now; con is initialized and open. Not a problem with that
using (SqlCommand command = new SqlCommand("fn_last_business_date", con))
{
       command.CommandType = CommandType.StoredProcedure;
       command.Parameters.AddWithValue("@seed_date", seedDate);//@seed_date is the param name
       object res = command.ExecuteScalar(); //res is always null 
}

しかし、次のようにDBでこれを直接呼び出すと:

select dbo.fn_last_business_date('8/3/2011 3:01:21 PM') 
returns '2011-08-03 15:01:21.000' 

これは、コードから呼び出したときに期待される結果です

なぜ、なぜ、なぜ?

4

3 に答える 3

27

なぜ誰もがselect構文を主張するのですか?..

using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("calendar.CropTime", c))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.DateTime).Direction = ParameterDirection.ReturnValue;
    cmd.Parameters.AddWithValue("@d", DateTime.Now);

    cmd.ExecuteNonQuery();

    textBox1.Text = cmd.Parameters["@RETURN_VALUE"].Value.ToString();

}
于 2011-08-03T19:56:22.130 に答える
8

試す:

using (SqlCommand command = new SqlCommand("select dbo.fn_last_business_date(@seed_date)", con))
{
       command.CommandType = CommandType.Text;
       command.Parameters.AddWithValue("@seed_date", seedDate);//@seed_date is the param name
       object res = command.ExecuteScalar(); //res is always null 
}
于 2011-08-03T19:36:49.177 に答える
1

実際には、キャッチされていないエラーが発生しています。ストアド プロシージャを呼び出すように、スカラー udfs を呼び出しません。

udf をストアド プロシージャでラップするか、構文を変更してください。一般的なものではないので正確にはわかりませんが…

あはは: これらの質問を参照してください:

于 2011-08-03T19:38:41.250 に答える