.net SQLITE问题
if (Pic1.Image == null)
{
MessageBox.Show("请选择要保存的图像!");
}
else
{
try
{
string fullpath = op.FileName;
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] imagebytes = br.ReadBytes((int)fs.Length);
SetConnection();
SQLiteCommand com = sql_con.CreateCommand();
sql_con.Open();
com.CommandText = "insert into ImageStore(ImageBlob) values(@image)";
com.Parameters.AddWithValue("@image", imagebytes);
int Flag = 0;
Flag += com.ExecuteNonQuery();
sql_con.Close();
if (Flag == 1)
{
MessageBox.Show("图片保存成功!");
Pic1.Image = null;
getMaxValue();
}
else
{
MessageBox.Show("图片保存失败!");
Pic1.Image = null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是插入sqlite数据库图片的代码,请问有谁能看出问题,我执行后总是提示no such table ,我确定数据库和表名是没问题的,字段类型也是blob的
[最优解释]
String fullPath = @"F:\kankan\04.jpg";
FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] imgbytes = br.ReadBytes((int)fs.Length);
using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + path))
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand(conn);
cmd.CommandText = "insert into ImageStore values (null,@img)";
cmd.Parameters.AddWithValue("@img", imgbytes);
int result = cmd.ExecuteNonQuery();
}
这是我写的测试程序,已通过。
[其他解释]
你确定数据库文件存在?如果数据库文件不存在,.net sqlite的dll会自动创建一个空的数据库文件,然后当然就会找不到表了。
[其他解释]
肯定存在啊,我试了很多次了,插入普通文本是可以的!
[其他解释]
因为你说提示 no such table。我以前遇到过,就是在项目里没有把数据库文件的属性里的【复制到输出目录】给选择【如果较新则复制】。结果就是输出目录下,被dll自动创建了个同名的数据库文件,但是个空文件。
------其他解决方案--------------------
唉,应该不是这个的问题,都试过了
[其他解释]
Flag += com.ExecuteNonQuery();
为什么是+=?
你是执行到这句报的错?
[其他解释]
跟这个没关系,这个是我用来判断成没成功的,成功执行会返回1
[其他解释]
那你执行到什么地方报错?
[其他解释]
是Flag += com.ExecuteNonQuery()执行有问题,但是跟flag+没关系,不加也是这里报错;
[其他解释]
我知道是什么问题了,还是openfileDialog路径问题,这个函数貌似会改变默认路径,前提是我路径是相对的,
所以它会在你打开图片的地方创建一个空数据库,造成错误,http://bbs.csdn.net/topics/250023126
只要设置op.RestoreDirectory = true就可以了