2012-11-29 55 views
0

我正在编程一个网站注意 - 不是项目在VB中的Visual Studio 2010中,并有一个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”在数据库中,因为每当我尝试添加它,我得到一个错误说“无法converet型System.Web.UI.WebControls.FileUpload到System.Data.Linq程序。二进制“我不知道该怎么做才能避免这种情况。我使用的数据库表:

用户

-userid(INT,递增) -Name(NVARCHAR) -Password(NVARCHAR) - 电子邮件(NVARCHAR) -PhotoID(图)

任何建议都会很棒。谢谢

回答

1

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

有最佳实践的一些说明,让我们来看一看:

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

+0

您好感谢您的答复。不幸的是,这并不是直截了当的,因为这是一个大学项目,将托管在uni服务器上,所以我不能保证具体的硬件位置,而如果图像(少量)保存在数据库中,我可以打包它全部放在一起并托管它 – Gavin

+0

如果您的唯一方法是保存文件,并且您使用的是SQL Server 2008或更高版本,则它支持变量类型“image”。 您可以将图片保存为数据库中的此类型。 –

+0

我有我的数据库中已经声明的图像字段,但我不知道如何实际上获取图像上传到它。如前所述,我正在使用LINQ。问题的全部范围是我有一个注册页面(上面的代码)。用户需要能够输入: - 名称,电子邮件,密码,管理员级别并浏览并上传图片。我想要通过一个提交按钮处理所有事件。我不知道如何将fileupload控件绑定到提交按钮来整理所有数据并将其作为一个用户输入 – Gavin

0

如果你最终决定与SQL Server做到这一点,所以你需要:

  1. 创建数据库中的字段将是图像的类型
  2. 创建一个存储过程,该图像将接收您的所有参数
  3. 在您的应用程序方面,您需要将您上传的文件转换为System.Data.Linq.Binary将其传递给sp的类型,您可以这样做:new System.Data.Linq.Binary(FileUpload1。 FileBytes)

后您从数据库中检索你的形象,你就会有相同的对象类型的System.Data.Linq.Binary。要在你的网站,你必须显示:

  1. 将其转换为base64字符串
  2. 推这个的base64到IMG SRC

    System.Data.Linq.Binary IMG = “图片从数据库中检索”

    IMG SRC = “数据:图像; BASE64,@ Convert.ToBase64String(img.ToArray())”