我在互联网上找到了一段代码,该代码将文档作为字节数组插入数据库中。如下:
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) 我希望不要让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)