如何将图像添加到数据库表

时间:2012-11-29 12:07:14

标签: database vb.net image linq web

我在Visual Studio 2010中用VB编写了一个网站注意 - 一个项目并且绑定了一个LINQ数据库。我有一个用户注册页面,它提示用户输入他们的名字,电子邮件,密码和他们需要的管理员级别。这也是添加照片的功能。这是通过标准的asp:fileupload控件实现的。目前它无法正常工作。我想浏览文件,然后点击“注册”按钮将所有用户详细信息添加到数据库中。我到目前为止的代码如下。

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If IsPostBack Then
        Dim UpPath As String
        UpPath = "~/Uploads/"
        Image1.Visible = True
        Image1.ImageUrl = Session("ImagePath")

        If Not Directory.Exists(UpPath) Then
            Directory.CreateDirectory("C:\UploadedUserFiles\")
        End If

    End If
End Sub

Protected Sub btnRegister_Click(sender As Object, e As System.EventArgs) Handles btnRegister.Click


    Dim savePath = "\Uploads\"
    Dim appPath As String = Server.MapPath("~")

    If (FileUPload1.HasFile) Then
        Dim savePath2 As String = savePath & Session("currentuser") & "" & FileUPload1.FileName
        FileUPload1.SaveAs(appPath & savePath2)
        Session("ImagePath") = "." & savePath
    End If


    ' variables to store the user's registration details
    Dim username As String
    Dim email As String
    Dim password As String
    Dim retypedPassword As String

    ' variables to store the user's selected roles
    Dim productOwner As Boolean
    Dim projectManager As Boolean
    Dim scrumMaster As Boolean
    Dim developer As Boolean

    ' populate variables with the values from the web page
    username = txtUsername.Text
    email = txtEmail.Text
    password = txtPassword.Text
    retypedPassword = txtRetypePassword.Text

    ' check which user roles have been selected
    productOwner = checkProductOwner.Checked
    projectManager = checkProjectManager.Checked
    scrumMaster = checkScrumMaster.Checked
    developer = checkDeveloper.Checked


    ' boolean to check if the entered details are valid
    Dim isValid As Boolean
    isValid = True

    ' check if the values entered by the user are valid
    If _
        String.IsNullOrEmpty(username) Or _
        String.IsNullOrEmpty(email) Or _
        String.IsNullOrEmpty(password) Or _
        String.IsNullOrEmpty(retypedPassword) Or _
        password <> retypedPassword Then
        isValid = False
    End If

    ' if the values are valid, then populate the USER table with the new user and the USER ROLES table
    ' with the roles they are allowed in any project that will be created

    If isValid Then
        ' set up LINQ connection with database
        Dim db As New AgileClassesDataContext()

        ' create a user to populate a row in the USER table
        Dim user As New User With _
        {.Name = username, _
         .Password = password, _
         .Email = email}

        ' add the new user to the USER table
        db.Users.InsertOnSubmit(user)

        ' submit the changes to the database
        Try
            db.SubmitChanges()
        Catch ex As Exception
            Console.WriteLine(ex)
            db.SubmitChanges()
        End Try

目前我还没有在数据库中声明'FileUPload1',因为每当我尝试添加它时,我都会收到一条错误消息“无法将System.Web.UI.WebControls.FileUpload输入System.Data.Linq.Binary”我不知道该怎么做才能避免这种情况。我使用的数据库表是:

用户

-UserID(int,incrementing) -Name(nvarchar) -Password(nvarchar) -Email(nvarchar) -PhotoID(图片)

任何建议都会很棒。感谢

2 个答案:

答案 0 :(得分:1)

我建议您在构建网站时不要将图像保存到数据库中。 您最好将图像保存在硬件上,并仅将图像路径推送到数据库。

对最佳做法有一些解释,请看一下:

https://stackoverflow.com/a/348373/350977

答案 1 :(得分:0)

如果您最终决定使用SQL Server,那么您需要:

  1. 在数据库中创建一个将为图像类型的字段
  2. 创建一个存储过程,该过程将使用此图像接收所有参数
  3. 在您的应用程序端,您需要将上传的文件转换为 System.Data.Linq.Binary 类型,以便将其传递给sp,您可以这样做: new System .Data.Linq.Binary(FileUpload1.FileBytes)
  4. 从数据库中检索图像后,您将拥有与 System.Data.Linq.Binary 相同的对象类型。要在您的网站上显示,您必须:

    1. 将其转换为base64字符串
    2. 将此base64推送到img src

      System.Data.Linq.Binary img =“从数据库中检索的图像”

      img src =“data:image; base64,@ Convert.ToBase64String(img.ToArray())”