【问题标题】:what is the meaning of error "Must declare the scalar variable @ " in .NET Core.NET Core中的错误“必须声明标量变量@”是什么意思
【发布时间】:2018-10-13 22:24:42
【问题描述】:

我是 .NET Core 的新手;我尝试将图像保存在 wwwroot 文件夹中,并使用 Dapper 将文件名和路径保存到 SQL Server 数据库

public async Task<string> WriteFile(IFormFile file)
{
        String fileName;

        try
        {
            var extension = "." + file.FileName.Split('.')[file.FileName.Split('.').Length - 1];
            fileName = Guid.NewGuid().ToString() + extension; 
            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\s", fileName);

            using (var bits = new FileStream(path, FileMode.Create))
            {
                await file.CopyToAsync(bits);
            }

            Image image = new Image(fileName,path);

            toDb(image);
        }
        catch (Exception e)
        {
            return e.Message;
        }

        return fileName;
}

public void toDb(Image image)
{
        string fileName = image.fileName;
        string path = image.path;

        using (IDbConnection dbConnection = Connection)
        {
            string sQuery = "INSERT INTO images(title, dir)" + "VALUES(@fileName, @path)";

            dbConnection.Open();
            dbConnection.Execute(sQuery,image);
        }
    }
}

这种方式是错误的,或者我该如何用 dapper 解决这个错误?

【问题讨论】:

标签: sql-server asp.net-core dapper


【解决方案1】:

您错误地调用了 dapper 命令。

错误发生是因为执行的命令找不到@fileName,@path参数

您必须使用匿名类传递参数名称

喜欢

dbConnection.Execute(sQuery, new { fileName = filename, path = path });

或者

dbConnection.Execute(sQuery, new { fileName = image.fileName, path = image.path});

参考Dapper: Parameterized queries

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多