Sql server 如何将图像名称插入数据库?
我成功地上传和图像,并调整它的大小。现在我想将图像的名称插入数据库 我正在使用SQL Server。该表名为Sql server 如何将图像名称插入数据库?,sql-server,vb.net,image,Sql Server,Vb.net,Image,我成功地上传和图像,并调整它的大小。现在我想将图像的名称插入数据库 我正在使用SQL Server。该表名为images,有两列,一列为整数imageid,另一列为imagename字符串(invarchar(max))。我需要保存在imagename列中的文件名,imageid必须是一个标识 以下是我目前掌握的代码: Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Imaging Im
images
,有两列,一列为整数imageid
,另一列为imagename
字符串(invarchar(max))。我需要保存在imagename列中的文件名,imageid
必须是一个标识
以下是我目前掌握的代码:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnupload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnupload.Click
Response.Write("Done")
If (IsPostBack) Then
HandleUploadedFile()
End If
End Sub
Private Sub HandleUploadedFile()
' get the root of the web site
Dim root As String = Server.MapPath("~/")
' clean up the path
If Not root.EndsWith("\") Then
root += "\"
End If
' make a folder to store the images in
Dim fileDirectory As String = root & "Images/"
' create the folder if it does not exist
If Not System.IO.Directory.Exists(fileDirectory) Then
System.IO.Directory.CreateDirectory(fileDirectory)
End If
' make a link to the new file
Dim link As String = "<a href='Images/{0}' target='_blank'>{1}</a>{2}{3}"
' loop through the file in the request
For i As Integer = 0 To Request.Files.Count - 1
' get the file instance
Dim fi As HttpPostedFile = Request.Files.[Get](i)
' create a byte array to store the file bytes
Dim fileBytes As Byte() = New Byte(fi.ContentLength - 1) {}
' fill the byte array
Using stream As System.IO.Stream = fi.InputStream
stream.Read(fileBytes, 0, fi.ContentLength)
End Using
' create a random file name
Dim fileName As String = Guid.NewGuid().ToString()
' write the original file to the file system
File.WriteAllBytes(fileDirectory + fileName & ".jpg", fileBytes)
litText.Text += String.Format(link, fileName & ".jpg", fileName & " Original", "<br/>", "")
' write the resized file to the file system
File.WriteAllBytes(fileDirectory + fileName & "_small.jpg", ResizeImageFile(fileBytes, 100))
litText.Text += String.Format(link, fileName & "_small.jpg", fileName & " Small", "<br/>", "<br/>")
' cleanup
litText.Visible = True
fileBytes = Nothing
Next
End Sub
Private Shared Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte()
Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))
Dim newSize As Size = CalculateDimensions(oldImage.Size, targetSize)
Using newImage As New Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb)
Using canvas As Graphics = Graphics.FromImage(newImage)
canvas.SmoothingMode = SmoothingMode.AntiAlias
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality
canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize))
Dim m As New MemoryStream()
newImage.Save(m, ImageFormat.Jpeg)
Return m.GetBuffer()
End Using
End Using
End Using
End Function
Private Shared Function CalculateDimensions(ByVal oldSize As Size, ByVal targetSize As Integer) As Size
Dim newSize As New Size()
If oldSize.Height > oldSize.Width Then
newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height))))
newSize.Height = targetSize
Else
newSize.Width = targetSize
newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width))))
End If
Return newSize
End Function
End Class
导入系统图形
导入System.Drawing.Drawing2D
导入System.Drawing.Imaging
导入System.IO
导入System.Data.SqlClient
导入系统数据
部分类\u默认值
继承System.Web.UI.Page
受保护的子btnupload_Click(ByVal sender作为对象,ByVal e作为System.EventArgs)处理btnupload。单击
回答。写下(“完成”)
如果(IsPostBack)那么
HandleUploadedFile()
如果结束
端接头
私有子手文件()
'获取网站的根目录
Dim root作为字符串=Server.MapPath(“~/”)
"清理道路,
如果不是root.EndsWith(\”),则
根+=“\”
如果结束
'创建一个文件夹以存储图像
Dim fileDirectory作为String=root&“Images/”
'如果文件夹不存在,请创建该文件夹
如果System.IO.Directory.Exists(fileDirectory)不存在,则
System.IO.Directory.CreateDirectory(fileDirectory)
如果结束
'创建指向新文件的链接
Dim链接为字符串=“{2}{3}”
'循环浏览请求中的文件
对于i As Integer=0的Request.Files.Count-1
'获取文件实例
Dim fi作为HttpPostedFile=Request.Files。[Get](i)
'创建字节数组以存储文件字节
Dim fileBytes As Byte()=新字节(fi.ContentLength-1){}
'填充字节数组
使用流作为System.IO.stream=fi.InputStream
stream.Read(fileBytes,0,fi.ContentLength)
终端使用
'创建一个随机文件名
Dim文件名为String=Guid.NewGuid().ToString()
'将原始文件写入文件系统
File.writealBytes(fileDirectory+fileName&“.jpg”,fileBytes)
litext.Text+=String.Format(链接、文件名和“.jpg”、文件名和“原件”、“
”和“)
'将调整大小的文件写入文件系统
File.writealBytes(fileDirectory+fileName&“u small.jpg”,ResizeImageFile(fileBytes,100))
litext.Text+=String.Format(链接、文件名和“\u small.jpg”、文件名和“small”、“小于br/>”、“小于br/>”)
"清理",
litext.Visible=True
fileBytes=Nothing
下一个
端接头
专用共享函数将imageFile(ByVal imageFile作为Byte(),ByVal targetSize作为Integer)的大小调整为Byte()
将oldImage用作System.Drawing.Image=System.Drawing.Image.FromStream(新内存流(imageFile))
Dim newSize As Size=计算尺寸(oldImage.Size,targetSize)
将newImage用作新位图(newSize.Width、newSize.Height、PixelFormat.Format24bppRgb)
使用画布作为图形=Graphics.FromImage(newImage)
canvas.SmoothingMode=SmoothingMode.AntiAlias
canvas.InterpolationMode=InterpolationMode.HighQualityBicubic
canvas.PixelOffsetMode=PixelOffsetMode.HighQuality
DrawImage(旧图像,新矩形(新点(0,0),新大小))
Dim作为新的内存流()
newImage.Save(m,ImageFormat.Jpeg)
返回m.GetBuffer()
终端使用
终端使用
终端使用
端函数
专用共享函数CalculateDimensions(ByVal oldSize作为大小,ByVal targetSize作为整数)作为大小
将新闻大小调整为新大小()
如果oldSize.Height>oldSize.Width,则
newSize.Width=CInt((oldSize.Width*(CSng(targetSize)/CSng(oldSize.Height)))
newSize.Height=targetSize
其他的
newSize.Width=targetSize
newSize.Height=CInt((oldSize.Height*(CSng(targetSize)/CSng(oldSize.Width)))
如果结束
返回新闻大小
端函数
末级
首先,您需要知道您的连接字符串。由于代码引用的是SQLClient,我假设您使用的是sqlserver,所以您应该查看一下。接下来,您需要使用SQLCommand对象的ExecuteOnQuery方法。我在下面贴了一个样品。您可以阅读有关SQLCommand对象的更多信息。这包括插入语句
Public Sub SaveNameInDatabase(ByVal fileName As String, ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
String query = String.format("INSERT INTO Files_Table (FileName) Values (\"{0}\")",fileName)
Dim command As New SqlCommand(, connection)
command.Connection.Open()
command.ExecuteNonQuery()
End Using
End Sub
这可能是代码,但你的问题是什么?我想知道如何在数据库中添加图像名称什么样的数据库?什么服务器?什么表模式?什么操作系统?什么是执行上下文?什么运行时环境?一个只添加了“我想知道如何做我之前没有解释的事情”的澄清实际上并不是一个澄清:)它的sql server,表名为images,有两列,第一列是imageid,值为int,另一列是imagename,值为string(invachar(max))我想把文件名保存在imagename列中,我做了一个id标识。我把你的评论合并到帖子中——Jeremy在下面贴了一些例子。一般来说,当人们要求澄清时,最好是你自己提供有用的信息并编辑你的帖子——可能并不总是有人在附近帮助:)希望这是有帮助的,也许我们会在附近见到你。