我有一个OpenFileDialog,它会假设选择照片并将其保存到数据库但问题是当我在openFileDialog的对话结果正常时访问该类时,它说no such table : PhotoFile当使用以下参数调用SavePhoto函数时:
TODO(PJ):把价值放在这里
这是我到目前为止所尝试的
OpenFileDialog d = new OpenFileDialog();
d.Filter = ("JPEG Imange (*.jpg|*.jpg|PNG Image (*.png)|All Files*.*");
if ((d.ShowDialog()) == DialogResult.OK)
{
SavePhoto(txtID.text,d.fileName);
}
Run Code Online (Sandbox Code Playgroud)
这是函数的代码
try {
using (SQLite.SQLiteConnection SQLConnect = new SQLite.SQLiteConnection(g_constring)) {
byte[] photo = FileImageToByte(PhotoFile);
SQLConnect.Open();
if (SQLConnect.State == ConnectionState.Open) {
SQLiteCommand SQLcommand = new SQLiteCommand(SQLConnect);
SQLcommand = SQLConnect.CreateCommand;
SQLcommand.CommandText = "DELETE FROM PhotoFile WHERE PhotoID = '" + PhotoId + "'";
SQLcommand.ExecuteNonQuery();
SQLcommand.Parameters.Clear();
SQLcommand.CommandText = "INSERT INTO PhotoFile(PhotoID, Photo) VALUES(@EmployeeID, @Photo1)";
SQLiteParameter SQLparmID = new SQLiteParameter("@EmployeeID", PhotoId);
SQLparmID.DbType = DbType.String;
SQLparmID.Value = PhotoId;
SQLcommand.Parameters.Add(SQLparmID);
SQLiteParameter SQLparm = new SQLiteParameter("@Photo1", photo);
SQLparm.DbType = DbType.Binary;
SQLparm.Value = photo;
SQLcommand.Parameters.Add(SQLparm);
SQLcommand.ExecuteNonQuery();
bReturn = true;
} else {
bReturn = false;
}
}
} catch (System.Exception eX) {
MessageBox.Show(eX.Message.ToString(), "Error in database", MessageBoxButtons.OK, MessageBoxIcon.Error);
bReturn = false;
}
return bReturn;
}
Run Code Online (Sandbox Code Playgroud)
PhotoFile表存在于我的数据库中,实际上我已经尝试过Windows窗体并触发该函数,dialog result = ok但是当我使用openFileDialog它时总是产生如上所述的错误.
这与OpenFileDialog 本身没有任何关系,它是你的SQL查询失败并说明该表不存在(Photofile) - 所以我建议它不会,你应该检查你的表名,或者创建如果有必要的话.
除此之外,您的查询存在问题:您的方法表示将保存照片但您使用的是DELETE.此外,如果表确实存在/当您设法正确排序表名时,我建议您不要使用字符串作为标识符.任何地方都没有储蓄.