SQLite数据库中插入图片,且关联界面显示
项目需要将图片存入到数据库。图片大小较小且数量有限,需直接存图片,所以不考虑存网址或图片地址的方式。遇到的问题为:
1、数据类型
2、存图片失败
先加入所需的库System.Data.SQLite.dll,然后加入命名空间
using System.Data.SQLite;
以下为所有实现流程:
第一步:创建数据库(包含文件名的路径用ss代替)
SQLiteConnection.CreateFile(ss);//创建数据库文件,SS包含文件名
第二步:建立连接,并打开
//方法一
SQLiteConnection m_dbConnection = new SQLiteConnection();//建立连接
SQLiteConnectionStringBuilder sqlstr = new SQLiteConnectionStringBuilder();//构建连接字符串
sqlstr.DataSource = ss;
m_dbConnection.ConnectionString = sqlstr.ToString();
m_dbConnection.Open();//打开连接
//方法二
string str = @"Data Source=" + ss + ";Version=3;";
SQLiteConnection m_dbConnection = new SQLiteConnection(str);
m_dbConnection.Open();
第三步:添加表
string cmdText = "CREATE TABLE IF NOT EXISTS [Sheet] ([id] INTEGER PRIMARY KEY AUTOINCREMENT,[时间] varchar(50),[图像] BLOB)"