我是 VB.NET 的初学者,所以您可能需要忍受我,但我需要在后面编辑此代码,以便将空值传递到我的数据库中的“imageurl”字段。前端是一个网络表单,用户可以在其中输入书籍的详细信息,并可以选择上传书籍封面。
我想更改我的代码,以便如果文件上传对话框不满足 hasFile,则在数据库中生成的 GUID 字符串将为 NULL 值(这样我就可以使用 ASP 中的 NullImageUrl 属性获得“无可用图像”图像.)
到目前为止,这是我尝试实现的,但智能感知告诉我“字符串类型的值不能转换为‘System.GUID’。
代码背后:
Imports System.Data.OleDb Partial Public Class addBook Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub btn_submission_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submission.Click Dim noFile As String = Nothing Dim myGUID = Guid.NewGuid() Dim newFileName As String = myGUID.ToString() & ".jpg" Dim fileLocationOnServerHardDisk = Request.MapPath("img/thumb") & "/" & newFileName If fu_picture.HasFile Then fu_picture.SaveAs(fileLocationOnServerHardDisk) Else myGUID = noFile End If Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString) Dim SqlString As String = "Insert into booklist(Title,Author,PublicationDate,Pages,Publisher,Blurb,imgurl,AverageRating) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8)" Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn) cmd.CommandType = CommandType.Text cmd.Parameters.AddWithValue("@f1", tb_booktitle.Text) cmd.Parameters.AddWithValue("@f2", tb_bookauthor.Text) cmd.Parameters.AddWithValue("@f3", tb_bookpubyear.Text) cmd.Parameters.AddWithValue("@f4", tb_bookpages.Text) cmd.Parameters.AddWithValue("@f5", tb_publisher.Text) cmd.Parameters.AddWithValue("@f6", tb_blurb.Text) cmd.Parameters.AddWithValue("@f7", "img/thumb/" & newFileName) cmd.Parameters.AddWithValue("@f8", rbl_Stars.SelectedValue) oleDbConn.Open() cmd.ExecuteNonQuery() System.Threading.Thread.Sleep("2000") Response.Redirect("~/addedrecord.aspx") End Sub Protected Sub rbl_Stars_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbl_Stars.SelectedIndexChanged End Sub End Class
请告诉我我的思路是否完全错误!
编辑:目前,即使没有上传文件,即使图像本身不存在,也会在数据库表中生成 guid 字符串 + jpg 后缀