Dapper GET有效,但POST无法获取数据-返回Sequence不包含任何元素

时间:2019-06-12 21:11:40

标签: c# sql-server ajax dapper svc

嘿,我正在为我的 C# api代码使用这个新的 Dapper框架

我正在使用 CRUD api ,而我使用Dapper的 GET 命令也可以正常运行(IAPI)

public class ChromeExtData
{
    public string fnum { get; set; }
    public string noty { get; set; }
    public string ffl { get; set; }
    public string code { get; set; }
    public string lname { get; set; }
    public string fname { get; set; }
    public string mname { get; set; }
    public string numsr { get; set; }
    public string sor { get; set; }
    public string pob { get; set; }
    public string sex { get; set; }
    public string birthday { get; set; }
    public string ethnicity { get; set; }
    public string race { get; set; }
    public string cstatus { get; set; }
    public string miscnumbers { get; set; }
    public string transp { get; set; }
}

[ServiceContract(Namespace = "")]
public interface IAPI
{
    [OperationContract]
    void DoWork();

    [OperationContract]
    [WebInvoke(Method = "GET",
               UriTemplate = "getall",
               ResponseFormat = WebMessageFormat.Json,
               BodyStyle = WebMessageBodyStyle.Bare)]
    Task<ChromeExtData> getall();

    [OperationContract]
    [WebInvoke(Method = "POST",
               UriTemplate = "Fnum",
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json,
               BodyStyle = WebMessageBodyStyle.Bare)]
    Task<ChromeExtData> Fnum(string FNum);
}

和我的其他 API

public class API : IAPI
{
    public void DoWork()
    {
    }

    public async Task<ChromeExtData> getall()
    {
        ChromeExtData _chromeExtData = new ChromeExtData();

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString()))
        {
            await conn.OpenAsync();
            _chromeExtData = await conn.QuerySingleAsync<ChromeExtData>("SELECT * FROM sAutomationOptionsSSRCC", null);
        }

        return _chromeExtData;
    }

    public async Task<ChromeExtData> Fnum(string FNum)
    {
        try
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString()))
            {
                await conn.OpenAsync();
                _chromeExtData = await conn.QuerySingleAsync<ChromeExtData>(
                                            "SELECT * FROM sAutomationOptionsSSRCC WHERE fnum = @fNum",
                                            new { fNum = FNum }
                                       );
            }
        }
        catch (Exception ex) {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }

        return _chromeExtData;
    }
}

然后我从 AJAX 调用 getall函数

function getALL() {
    $.ajax({
        url: "Service/API.svc/getall",
        type: "GET",
        dateType: "json",
        success: function (result) {
            console.log(result);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr.status);
            console.log(thrownError);
        }
    });
}

产生:

  

{生日:“ 5/21/2019”,代码:“ qTest”,cstatus:“ F”,fnum:“ F5218471”,ff2l:“ 147210123”,…}

但是,当尝试运行 POST 代码时,会出现此错误:

  

序列不包含任何元素

我的ajax呼叫 POST

function fNumber() {
    $.ajax({
        url: "Service/NNicsAPI.svc/Fnum",
        type: "POST",
        data: JSON.stringify({ FNum: "F5218471" }),
        dataType: "json",
        contentType: "application/json",
        success: function (result) {
            alert(result);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr.status);
            console.log(thrownError);
        }
    });
}

我知道它发送了fNum的值:

enter image description here

在MSSQL上的手动查询显示了数据:

enter image description here

那我想念什么,因为它与 GET 调用中的同一数据库一起使用,但对于 POST 却不返回任何内容?

更新1

使用此:

try
{
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString()))
    {
        await conn.OpenAsync();
        _chromeExtData = await conn.QueryFirstOrDefaultAsync<ChromeExtData>(
                                 "SELECT * FROM sAutomationOptionsSSRCC WHERE fnum = @fNum",
                                 new { fNum = FNum }
                               );
   }
}
catch (Exception ex) {
    System.Diagnostics.Debug.WriteLine(ex.Message);
}

return _chromeExtData;

我明白了:

enter image description here

因此,它不会返回错误,但是会返回 null 。...这又应该不是,因为我知道它发送正确的值并手动查询产生值...

更新2

我似乎愿意:

string sql = "SELECT * FROM sAutomationOptionsSSRCC";

enter image description here

它返回值就好了。

但是,当我执行位置时:

enter image description here

string sql = "SELECT * FROM sAutomationOptionsSSRCC where fnum = 'F5218471'";

除了 NULL ???

以外,它什么都不返回。

0 个答案:

没有答案