小编Lon*_*dow的帖子

使用 Dapper 将 byte[] 数组转换为 sqldbtype.Varbinary

我在互联网上找到了一段代码,该代码将文档作为字节数组插入数据库中。如下:

    public void databaseFilePut(string varFilePath)
    {
        byte[] file;
        using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read))
        {
            using (var reader = new BinaryReader(stream))
            {
                file = reader.ReadBytes((int)stream.Length);
            }
        }

        //using (var varConnection = Locale.sqlConnectOneTime(Locale.slqDataConnectionDetails))
        using (SqlConnection connection = new SqlConnection(connectionString))
        using (var sqlWrite = new SqlCommand("INSERT INTO EXCEL_EM_BYTES (DOCUMENTO_BYTES) Values(@File)", connection))
        {
            sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
            connection.Open();
            sqlWrite.ExecuteNonQuery();
            connection.Close();
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在,我必须将其应用到 Dapper/Entity 框架,但到目前为止,还没有成功。到目前为止我得到的信息如下:

      public void InsereRegistroEmail(string a, string b, string c, byte[] anexoBytes)
    {
        //var cn = _context.Database.Connection; …
Run Code Online (Sandbox Code Playgroud)

c# sql sql-server entity-framework dapper

4
推荐指数
1
解决办法
1万
查看次数

如何不登记18岁以下的人

我希望不要让18岁以下的人在数据库中注册.我对如何做到这一点毫无头绪.我已经有控制器了.这里是.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "nome,cpf,data_nasc")] clientes clientes)
{
    try
    {
        if (ModelState.IsValid)
        {
            db.Cliente.Add(clientes);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
    catch (DataException /* dex */ )
    {
        ModelState.AddModelError("", "Não foi possível salvar as mudanças. Tente de novo e se o problema persistir entre em contato com o administrador de sistema.");
    }

    return View(clientes);
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc

2
推荐指数
1
解决办法
157
查看次数

标签 统计

c# ×2

asp.net ×1

asp.net-mvc ×1

dapper ×1

entity-framework ×1

sql ×1

sql-server ×1