用asp.net实现将上传的图片存入数据库!

Public   Class changimage
    
Inherits System.Web.UI.Page
    
Protected WithEvents cmddemo As System.Web.UI.WebControls.Button
    
Protected WithEvents cmdupload As System.Web.UI.WebControls.Button
    
Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
    
Protected WithEvents SqlComm As System.Data.SqlClient.SqlCommand
    
Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile

Web Form Designer Generated Code

    
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
End Sub

   
    
Private Sub cmdupload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupload.Click
        
Dim image As System.Drawing.Image, newimage As System.Drawing.Image
        
Dim callb As System.Drawing.Image.GetThumbnailImageAbort
        
Dim f As System.IO.File, fs As System.IO.FileStream
        
Dim temppath As String 
        
Dim bigdata As Byte(), smalldata As Byte()   '大图片数据、小图片数据
        Dim pic As System.Data.SqlClient.SqlParameter, picsmall As System.Data.SqlClient.SqlParameter
        
'检察上传文件是否合标准,check函数是我根据网站需要写的了
        If check(File1.PostedFile.FileName) <> "ok" Then
            Response.Write(check(File1.PostedFile.FileName))
            
Exit Sub
        
End If
        
'设置临时路径,为了防止多用户访问时的冲突,设了一个application对象
        If Application("image"= "" Then
            Application(
"image"= 0
        
End If
        Application.Lock()
        temppath 
= Server.MapPath(CStr(Application("image")))  '临时路径
        Application("image"= Application("image"+ 1
        Application.UnLock()
        
'读取图片的数据
        ReDim bigdata((Me.File1.PostedFile.InputStream.Length)
        
Me.File1.PostedFile.InputStream.Read(bigdata, 0UBound(bigdata))  '将原图片数据读到bigdata中
        '改变图片的大小
        image = System.Drawing.Image.FromStream(Me.File1.PostedFile.InputStream)
'newimage里面的size也可另外设置,我只用了80*60和60*80两种
        If image.Width > image.Height Then
            newimage 
= image.GetThumbnailImage(8060, callb, New System.IntPtr(0))
        
Else
            newimage 
= image.GetThumbnailImage(6080, callb, New System.IntPtr(0))
        
End If
        image.Dispose()   
'将新图片及图片变小后存到临时路径中
        newimage.Save(temppath, System.Drawing.Imaging.ImageFormat.Jpeg)
        newimage.Dispose()
'读取临时文件数据到smalldata中
        fs = New System.IO.FileStream(temppath, IO.FileMode.Open, IO.FileAccess.Read)
        
ReDim smalldata(fs.Length)
        fs.Read(smalldata, 
0UBound(smalldata))
        fs.Close()
'上述获得小图片的方法我原本想用system.io.memorystream的,可是行不通:代码如下:
'
dim m as system.io.memorystream
'
m=new system.io.memorystream()
'
newimage.save(m,System.Drawing.Imaging.ImageFormat.Jpeg)
'
redim smalldata(m.length)
'
m.read(smalldata,0,m.length)
'
可是上述方法读出来的smalldata全是空的,不知道原因,请指教
        '删除临时文件
        If f.Exists(temppath) Then
            f.Delete(temppath)
        
End If
        
'将数据加入数据库中
'
由于数据库中有image字段,我用sql插不进去,就用一个存储过程
'
请教各位大虾用sql语句插入有image字段的表该怎么写
'
用insert into talbe(pic,picsmall) values("&bigdata&","&smalldata&")这样不行,用'"&bigdata&"'也不行呀!
        SqlConn = New System.Data.SqlClient.SqlConnection(connstr)  '可自己设置connstr连接数据库服务器
        SqlComm = New System.Data.SqlClient.SqlCommand()
        SqlComm.CommandType 
= CommandType.StoredProcedure
        SqlComm.CommandText 
= "dbo.image"
        pic 
= New System.Data.SqlClient.SqlParameter("@pic", SqlDbType.Image)
        pic.Value 
= bigdata
        picsmall 
= New System.Data.SqlClient.SqlParameter("@picsmall", SqlDbType.Image)
        picsmall.Value 
= smalldata
        SqlComm.Parameters.Add(pic)
        SqlComm.Parameters.Add(picsmall)
        SqlComm.Connection 
= SqlConn
        SqlComm.Connection.Open()
        SqlComm.ExecuteNonQuery()
        SqlComm.Connection.Close()
        SqlComm.Dispose()
        SqlConn.Dispose()
    
End Sub

End Class
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET中将数据存储到SQL数据库中,可以采用以下步骤: 1. 创建一个SQL Server数据库,可以使用SQL Server Management Studio创建。 2. 在Visual Studio中打开你的ASP.NET项目,并在项目中添加一个连接到SQL Server数据库的数据源。可以通过在“服务器资源管理器”中右键单击“数据连接”来添加数据源。 3. 在你的ASP.NET页面中创建一个控件,用于输入数据。例如,可以使用TextBox控件。 4. 在代码中,使用ADO.NET对象(如SqlConnection、SqlCommand等)连接到数据库,并将数据插入到数据库表中。 下面是一个示例代码,可以将TextBox中输入的数据存储到名为“myTable”的表中: ``` protected void Button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword"; string insertSql = "INSERT INTO myTable (columnName1, columnName2) VALUES (@value1, @value2)"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(insertSql, connection); command.Parameters.AddWithValue("@value1", TextBox1.Text); command.Parameters.AddWithValue("@value2", TextBox2.Text); connection.Open(); command.ExecuteNonQuery(); } } ``` 注意,这个示例代码中的连接字符串需要替换成你自己的SQL Server连接字符串。另外,在实际应用中,还需要进行一些错误处理和数据验证,以确保数据的安全性和完整性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值