博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DotImage使用教程:从数据库中读写图像
阅读量:6953 次
发布时间:2019-06-27

本文共 3033 字,大约阅读时间需要 10 分钟。

hot3.png

.Net控件Atalasoft 具有流媒体功能,可与ADO.NET共同使用以直接读取数据库中的图像,无需保存至临时文件。下面代码片段演示了如何利用C#和VB.NET从数据库中读写图像。

写入数据库:

C#

private void SaveToSqlDatabase(AtalaImage image){SqlConnection myConnection = null;try{// Save image to byte array.byte[] imagedata = image.ToByteArray(new Atalasoft.Imaging.Codec.JpegEncoder(75));// Create the SQL statement to add the image data.myConnection = new SqlConnection(CONNECTION_STRING);SqlCommand myCommand = new SqlCommand("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', @Image)", myConnection);SqlParameter myParameter = new SqlParameter("@Image", SqlDbType.Image, imagedata.Length);myParameter.Value = imagedata;myCommand.Parameters.Add(myParameter);// Open the connection and execture the statement.myConnection.Open();myCommand.ExecuteNonQuery();}finally{myConnection.Close();}}

Visual Basic.NET

Private Sub SaveToSqlDatabase(ByVal image As AtalaImage)Dim myConnection As SqlConnection = NothingTry' Save image to byte array.Dim imagedata() As Byte = image.ToByteArray(New Atalasoft.Imaging.Codec.JpegEncoder(75))' Create the SQL statement to add the image data.myConnection = New SqlConnection(CONNECTION_STRING)Dim myCommand As SqlCommand = New SqlCommand("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', @Image)", myConnection)Dim myParameter As SqlParameter = New SqlParameter("@Image", SqlDbType.Image, imagedata.Length)myParameter.Value = imagedatamyCommand.Parameters.Add(myParameter)' Open the connection and execture the statement.myConnection.Open()myCommand.ExecuteNonQuery()FinallymyConnection.Close()End TryEnd Sub

从数据库中读取:

C#

private AtalaImage OpenFromSqlDatabase(){SqlConnection myConnection = null;try{// Establish connection and SELECT statement.myConnection = new SqlConnection(CONNECTION_STRING);SqlCommand myCommand = new SqlCommand("SELECT ImageData FROM Atalasoft_Image_Database WHERE Caption = '" + txtCaption.Text + "'", myConnection);myConnection.Open();// Get the image from the database.byte[] imagedata = (byte[])myCommand.ExecuteScalar();if (imagedata != null){AtalaImage image = AtalaImage.FromByteArray(imagedata);return image;}else{MessageBox.Show("Image does not exist in database.");return null;}}finally{myConnection.Close();}}

Visual Basic .NET

Private Function OpenFromSqlDatabase() As AtalaImageDim myConnection As SqlConnection = NothingTry' Establish connection and SELECT statement.myConnection = New SqlConnection(CONNECTION_STRING)Dim myCommand As SqlCommand = New SqlCommand("SELECT ImageData FROM Atalasoft_Image_Database WHERE Caption = '" + txtCaption.Text + "'", myConnection)myConnection.Open()' Get the image from the database.Dim imagedata() As Byte = CType(myCommand.ExecuteScalar(), Byte())If (Not imagedata Is Nothing) ThenDim image As AtalaImage = AtalaImage.FromByteArray(imagedata)Return imageElseMessageBox.Show("Image does not exist in database.")Return NothingEnd IfFinallymyConnection.Close()End TryEnd Function

 

转载于:https://my.oschina.net/u/932733/blog/264631

你可能感兴趣的文章
21、Samba配置详解
查看>>
Eclipse反编译插件 Enhanced Class Decompiler
查看>>
第五元素 感
查看>>
Java性能调优
查看>>
JAVA桌面图表控件Chart FX for Java Desktop
查看>>
RH413日志服务器篇
查看>>
Android模拟器Genymotion
查看>>
ecshop网店系统v2.7.4
查看>>
php验证码代码实例
查看>>
Python收集主机信息
查看>>
java 假 "幻灯片"
查看>>
《编写高质量代码--改善JavaScript程序的188个建议》学习记录(一)
查看>>
Java中Set、List、Map
查看>>
Spring学习总结(6)——Spring之核心容器bean
查看>>
ubuntu重啟網卡提示"Job failed while stopping"解決方法!【参考别人的信息】
查看>>
<org manual>翻译--2.9 区块
查看>>
IOS 学习笔记 —— EGOTableViewPullRefresh使用 (一)
查看>>
一道mysql面试题
查看>>
Storefront与NetScaler的集成配置 - part3
查看>>
python virtualenv的使用
查看>>