问题描述
我想一次插入图像,但是我的代码一次插入两个图像,我的代码有误.请提供代码的解决方案.
I want to insert image one time but my code inserting two images at a time, where is mistake in my code. Please provide the solution for the code.
Try
Dim sqlcon As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cn").ConnectionString)
sqlcon.Open()
If FileUpload1.HasFile Then
'Dim f(1) As String
Dim f(0) As String
Dim file() As FileUpload = {FileUpload1}
Dim i As Integer
Try
cmd = New SqlClient.SqlCommand("Select max(id) from birthday", sqlcon)
dr = cmd.ExecuteReader()
While dr.Read
adcode = dr(0)
End While
dr.Close()
Catch ex As Exception
adcode = 0
dr.Close()
End Try
For i = 0 To 0
' i = i + 1
'adcode = adcode + 1
adcode = adcode
f(i) = upfile(file(i), i, adcode)
Next
End If
Catch ex As Exception
Finally
sqlcon.Close()
End Try
Public Function upfile(ByVal file As FileUpload, ByVal i As Integer, ByVal cd As Integer) As String
Dim sqlcon As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cn").ConnectionString)
sqlcon.Open()
Dim f1, f As String
f = file.FileName
If f <> "" Then
f1 = adcode
f = f.Substring(f.LastIndexOf("."))
f1 = "~/images/" + f1 + f
file.SaveAs(Server.MapPath(f1))
cmd = New SqlClient.SqlCommand("insert into birthday(title,photo,namewithdate) values('" & TextBox1.Text & "','" & f1 & "','" & TextBox2.Text & "')", sqlcon)
cmd.ExecuteNonQuery()
labMessage.Text = "Record Saved Sucessfully..."
clear()
End If
Return 0
End Function
推荐答案
Dim f(0) As String
如果不起作用,为什么还要麻烦地从数据库中读取数据,而您默认将其设置为零,以后又会引起问题?
Why do you bother to read from a database, if when it doesn''t work you default to zero and cause a problem later on?
Catch ex As Exception
adcode = 0
为什么要串联字符串以生成SQL语句?使用参数化查询,或将数据库释放到Little Bobby Tables ...
Why are you concatenating strings to make an SQL statement? Use parametrized queries, or loose your database to Little Bobby Tables...
cmd = New SqlClient.SqlCommand("insert into birthday(title,photo,namewithdate) values(''" & TextBox1.Text & "'',''" & f1 & "'',''" & TextBox2.Text & "'')", sqlcon)
为什么会有循环?
Why do you have a loop?
For i = 0 To 0
这将执行多少次?
我可以继续,但是这段代码使我感到不舒服...
How many times will this be executed?
I could go on, but that code is making me feel queasy...
这篇关于代码在需要一个的位置插入两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!