我使用VB.net创建一个程序,该功能是从Access中搜索数据,然后将数据保存到html文件中。
但是在从Access中搜索数据之后,该字符串无法连接在一起。
Dim strpath As String = System.Windows.Forms.Application.StartupPath + "\\output\\"
If (Not System.IO.Directory.Exists(strpath)) Then
System.IO.Directory.CreateDirectory(strpath)
End If
Dim strfilename As String = strpath + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + "_" + textBox_name.Text + ".html"
Dim screach_name As String = textBox_name.Text
Dim html_code As String = ""
html_code += "<!DOCTYPE html><html><head><title>"
html_code += screach_name
html_code += "</title></head><body>"
html_code += "Screach:<b>" + screach_name + "</b><br />"
Try
Dim strcon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb;"
Dim con_db As OleDbConnection = New OleDbConnection(strcon)
Dim sql_count As String = "SELECT COUNT(*) FROM table where name Like '%" + search_name + "%'"
Dim com_data As OleDbCommand = New OleDbCommand(sql_count, con_db)
Dim count_data As Integer = Convert.ToInt32(com_data.ExecuteScalar())
html_code += "Number of records = "
html_code += count_data.ToString
Catch ex As Exception
Finally
End Try
html_code += "</body></html>"
Using file As StreamWriter = New StreamWriter(strfilename, True)
file.WriteLine(html_code)
End Using
但仅html输出
Blockquote
<!DOCTYPE html> <html> <head> <title>
screach_name
</title> </head> <body>
Screach: <b> screach_name </b> <br />
Blockquote
答案 0 :(得分:0)
您的方法中发生了3种不同的事情。如果将代码分为3种不同的方法,则代码将更易于遵循,维护和测试。请注意,演示的方法未连接到用户界面(没有直接引用textBox_name.Text)。如果您重组应用程序并将例如GetRecordCount方法移动到DataAccess类,这可能会派上用场。
GetRecordCount方法中的Using ... End Using块可确保即使发生错误也可以关闭并处置数据库对象。
BuildHTMLString方法中的StringBuilder保存了创建和丢弃字符串的程序形式。每次以任何方式更改字符串时,程序都必须丢弃旧字符串并创建一个全新的字符串。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim RecordCount = GetRecordCount(textBox_name.Text)
Dim HTMLString = BuildHTMLString(RecordCount, textBox_name.Text)
SaveHTMLString(HTMLString, textBox_name.Text)
End Sub
Private Function GetRecordCount(SearchName As String) As Integer
Dim RecordCount As Integer
'Pass the connection string directly to the constructor of the connection
Using cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb;")
'Pass the query text an the connection directly to the constructor of the command
Using cmd As New OleDbCommand("SELECT COUNT(*) FROM table where name Like @SearchName;", cn)
'Always use parameters
cmd.Parameters.Add("@SearchName", OleDbType.VarChar).Value = "%" & SearchName & "%"
RecordCount = CInt(cmd.ExecuteScalar())
End Using
End Using
Return RecordCount
End Function
Private Function BuildHTMLString(RecordCount As Integer, SearchName As String) As String
Dim HTMLString As String = ""
Dim sb As New StringBuilder()
sb.AppendLine("<!DOCTYPE html><html><head><title>")
sb.AppendLine(SearchName)
sb.AppendLine("</title></head><body>")
sb.AppendLine("Screach:<b>" & SearchName & "</b><br />")
sb.AppendLine("Number of records = " & RecordCount.ToString)
sb.AppendLine("</body></html>")
HTMLString = sb.ToString
Return HTMLString
End Function
Private Sub SaveHTMLString(HTML As String, SearchName As String)
'Add Imports System.IO to the top or the file
'Using Path.Combine makes sure the back slashes are OK
Dim strpath As String = Path.Combine(Application.StartupPath, "\output\")
If (Not Directory.Exists(strpath)) Then
Directory.CreateDirectory(strpath)
End If
Dim HTMLFileName As String = DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") & "_" & SearchName & ".html"
Dim strfilename As String = Path.Combine(strpath, HTMLFileName)
Using file As StreamWriter = New StreamWriter(strfilename, True)
file.WriteLine(HTML)
End Using
End Sub