向sqlite数据库写入文件流问题 请教怎么在C#中向sqlite数据库保存文件,比如我要上传一个*.xls文件到sqlite表里,该如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Sqlite数据库里指定数据类型为:Blob。在数据访问指定参数类型为:DbType.Binary然后把Excel文件读到字节数组里。 using(FileStream file = File.Open(this.TextBox2.Text,FileMode.Open)) { byte[] bytes = new byte[file.Length]; file.Read(bytes, 0, Convert.ToInt32(file.Length)); testDal.Insert(Guid.NewGuid().ToString(), bytes, null); }数据访问存储字节数据。public Int32 Insert(string ID,Byte[] fileStream, SQLiteTransaction trans) { if (conn.State == ConnectionState.Closed) { conn.Open(); } SQLiteCommand command; if (trans == null) { command = new SQLiteCommand("insert into test(ID,excelText) values (:ID,:excelText)", conn); } else { command = new SQLiteCommand("insert into test(ID,excelText) values (:ID,:excelText)", conn, trans); } command.Parameters.Add("ID", DbType.String); command.Parameters.Add("excelText", DbType.Binary); command.Parameters["ID"].Value = ID; command.Parameters["excelText"].Value = fileStream; try { return command.ExecuteNonQuery(); } catch { return 0; } }根据主键返回Excel的字节数据 public byte[] GetExcel(string ID) { if (conn.State == ConnectionState.Closed) { conn.Open(); } SQLiteCommand command = new SQLiteCommand("Select excelText from test where ID = :ID", conn); command.Parameters.Add("ID", DbType.String); command.Parameters["ID"].Value = ID; SQLiteDataReader dr = null; dr = command.ExecuteReader(); byte[] File = null; if (dr.Read()) { File = (byte[])dr[0]; } return File; }把字节数据存储为文件 using (FileStream fs = File.Create("D:\\Test.xls")) { fs.Write(file, 0, file.Length); fs.Close(); } 学习C#从俄罗斯方块开始(一)概述 要实现这个功能大家给个建议 dev treelist控件 .net 发传真求助 这个句子有啥问题呀 用户清除session后,其他用户的session还在吗? 请问 [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]中的..\\是什么意思? 怎么样把DataSet里的表的 当前行的下一行 设置为 当前行?(也就是移动行指针) DaataGrid问题!大家来帮忙! C#中有没有类似Template的特性,能模拟也行 一个webservice的问题 记事本的小问题
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, Convert.ToInt32(file.Length));
testDal.Insert(Guid.NewGuid().ToString(), bytes, null);
}数据访问存储字节数据。
public Int32 Insert(string ID,Byte[] fileStream, SQLiteTransaction trans)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SQLiteCommand command;
if (trans == null)
{
command = new SQLiteCommand("insert into test(ID,excelText) values (:ID,:excelText)", conn);
}
else
{
command = new SQLiteCommand("insert into test(ID,excelText) values (:ID,:excelText)", conn, trans);
}
command.Parameters.Add("ID", DbType.String);
command.Parameters.Add("excelText", DbType.Binary); command.Parameters["ID"].Value = ID;
command.Parameters["excelText"].Value = fileStream;
try
{
return command.ExecuteNonQuery();
}
catch
{
return 0;
}
}
根据主键返回Excel的字节数据
public byte[] GetExcel(string ID)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SQLiteCommand command = new SQLiteCommand("Select excelText from test where ID = :ID", conn);
command.Parameters.Add("ID", DbType.String);
command.Parameters["ID"].Value = ID; SQLiteDataReader dr = null; dr = command.ExecuteReader(); byte[] File = null;
if (dr.Read())
{
File = (byte[])dr[0];
}
return File;
}把字节数据存储为文件
using (FileStream fs = File.Create("D:\\Test.xls"))
{
fs.Write(file, 0, file.Length);
fs.Close();
}