本文介绍了如何在Access中保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! < asp:FileUpload ID = FileUploadControl1; runat = server;> 我想在ms访问数据库中保存图像。图像存储为oleobject。 如何使用where子句写入更新语句以插入图像 < string sql = insert into = values = 其中 = username = ?;& lt; br mode = hold /> 使用(OleDbCommand command = new OleDbCommand(SqlString,conn)) { command.CommandType = CommandType.Text; command.Parameters.AddWithValue( PhotoImage,Nattxt.Text); command.Parameters.AddWithValue( SignIamge,apldposttxt.Text); command.Parameters.AddWithValue( UserName,usertxt.Text); command.ExecuteNonQuery(); } 解决方案 如果您需要插入新记录;使用插入SQL语句 [ ^ ] 如果您需要更新现有记录; 使用更新SQL语句 [ ^ ] 下面的是使用 OleDbConnection 受保护 无效 btnSaveImage_Click( object sender,EventArgs e) { if (FileUpload1.HasFile ) { string name = FileUpload1.PostedFile.FileName; int length = FileUpload1.PostedFile.ContentLength; byte [] imageBytes = new 字节 [长度]; Stream imageStream = FileUpload1.PostedFile.InputStream; imageStream.Read(imageBytes, 0 ,length); string connString = ConfigurationManager.ConnectionStrings [ ImagesDB]的ConnectionString。 OleDbConnection connection = new OleDbConnection(connString); string insertQuery = INSERT INTO Images(ImageName,ImageSize,ImageData)VALUES(@ ImageName,@ ImageSize,@ ImageData); OleDbCommand command = new OleDbCommand(); command.Connection = connection; command.CommandText = insertQuery; command.CommandType = CommandType.Text; command.Parameters.AddWithValue( @ ImageName,name); command.Parameters.AddWithValue( @ ImageSize,length); command.Parameters.AddWithValue( @ ImageData,imageBytes); 尝试 { connection.Open(); command.ExecuteNonQuery(); lblMessage.Text = 图像数据已成功保存; } catch (例外情况) { lblMessage.Text = 无法保存图像数据; } 最后 { connection.Close(); } } } 参考此文章 [ ^ ] <asp:FileUpload ID=FileUploadControl1; runat=server;>i want to save image in ms access database. image is stored as oleobject.how to write update statement to insert image using where clause<string sql="insert" into="" values="" where="" username="?;&lt;br" mode="hold" />using (OleDbCommand command = new OleDbCommand(SqlString, conn)){ command.CommandType = CommandType.Text; command.Parameters.AddWithValue("PhotoImage", Nattxt.Text); command.Parameters.AddWithValue("SignIamge", apldposttxt.Text); command.Parameters.AddWithValue("UserName", usertxt.Text); command.ExecuteNonQuery();} 解决方案 if you need to insert new record; use insert SQL statement[^]if you need to update existing record; use update SQL statement[^]below is sample code which insert image data to access database using OleDbConnectionprotected void btnSaveImage_Click(object sender, EventArgs e){ if (FileUpload1.HasFile) { string name = FileUpload1.PostedFile.FileName; int length = FileUpload1.PostedFile.ContentLength; byte[] imageBytes = new byte[length]; Stream imageStream = FileUpload1.PostedFile.InputStream; imageStream.Read(imageBytes, 0, length); string connString = ConfigurationManager.ConnectionStrings["ImagesDB"].ConnectionString; OleDbConnection connection = new OleDbConnection(connString); string insertQuery = "INSERT INTO Images(ImageName, ImageSize, ImageData) VALUES(@ImageName, @ImageSize, @ImageData)"; OleDbCommand command = new OleDbCommand(); command.Connection = connection; command.CommandText = insertQuery; command.CommandType = CommandType.Text; command.Parameters.AddWithValue("@ImageName", name); command.Parameters.AddWithValue("@ImageSize", length); command.Parameters.AddWithValue("@ImageData", imageBytes); try { connection.Open(); command.ExecuteNonQuery(); lblMessage.Text = "Image data saved successfully"; } catch (Exception ex) { lblMessage.Text = "Unable to save image data"; } finally { connection.Close(); } }}Refer this article [^] 这篇关于如何在Access中保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 18:58