简体   繁体   English

如何使用dapper从数据库返回字符串

[英]How can return string from database using dapper

I tried but it gives this error 我试过了但是它给出了这个错误

Must declare the scalar variable "@imageid" 必须声明标量变量“ @imageid”

controller.cs controller.cs

    [HttpGet]
    [Route("download/{id:int}")]
    public String Getfiles(int imageid)
    {

        return _ShopDataProvider.DownloadImage(imageid);

    }

class.cs class.cs

public String  DownloadImage(int imageid)
    {
        using(IDbConnection dbConnection = Connection)
        {

            string sQuery0 = "SELECT path FROM Shop WHERE ShopId = @imageid";
            dbConnection.Open();
            String Path = dbConnection.QueryFirstOrDefault<String>(sQuery0, new { ShopId = imageid });

            return Path;
        } 
    }

You're declaring a parameter in your SQL query named @imageid but not providing a value for it. 您正在SQL查询中声明一个名为@imageid的参数,但未为其提供值。 You're providing a value for the parameter @ShopID ( new { ShopId = imageid } ), which your SQL query doesn't use. 您正在为参数@ShopIDnew { ShopId = imageid } )提供一个值,您的SQL查询不使用该值。

Change new { ShopId = imageid } to new { imageid = imageid } new { ShopId = imageid }更改为new { imageid = imageid }

Please refer to the documentation . 请参考文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM