我对如何使用SQL数据库还不熟悉,我在这个代码领域的目标是创建一个do循环,它将持续读取当月制作的文件并将其插入SQL数据库的各自列中。如果有一个简单的错误,请原谅我,这是我的课程,这是基于剧院/电影院。我似乎无法弄清楚除了显示的显而易见的错误之外的错误:“对象引用未设置为对象的实例”,非常感谢帮助,谢谢!
SpecificPath()从硬盘驱动器中检索文件,并从其访问的文本文件中读取特定行。
Do Until C = 32
If File.Exists(SearchPath(C)) Then
Dim sr As New StreamReader(SearchPath(C))
Items(1, C) = ReadSpecificLine(SearchPath(C), 14) 'Total Product Profit
Items(2, C) = ReadSpecificLine(SearchPath(C), 15) 'Total Ticket Profit
Items(3, C) = ReadSpecificLine(SearchPath(C), 16) 'Total Profit from Water
Items(4, C) = ReadSpecificLine(SearchPath(C), 17) 'Total Profit from Large Soft Drink
Items(5, C) = ReadSpecificLine(SearchPath(C), 18) 'Total Profit from Small Soft Drink
Items(6, C) = ReadSpecificLine(SearchPath(C), 19) 'Total Profit from Buttered Popcorn
Items(7, C) = ReadSpecificLine(SearchPath(C), 20) 'Total Profit from Salted Popcorn
Items(8, C) = ReadSpecificLine(SearchPath(C), 21) 'Total Profit from Sweet Popcorn
Items(9, C) = ReadSpecificLine(SearchPath(C), 28) 'Total Profit from Adult Tickets
Items(10, C) = ReadSpecificLine(SearchPath(C), 29) 'Total Profit from Teen Tickets
Items(11, C) = ReadSpecificLine(SearchPath(C), 30) 'Total Profit from Child Tickets
Items(12, C) = ReadSpecificLine(SearchPath(C), 34) 'Gross Profit
Items(13, C) = ReadSpecificLine(SearchPath(C), 35) 'Net Profit
MySqlConn = New MySqlConnection
Try
MySqlConn.ConnectionString = "Placeholder for pasting onto website"
MySqlConn.Open()
cmd.Connection = MySqlConn
cmd.CommandText = "INSERT INTO Cinema_Graph_Data (Year,Month,Water,Large_SD,Small_SD,Buttered_Popcorn,Salted_Popcorn,Sweet_Popcorn,Adult_Tickets,Teen_Tickets,Child_Tickets,Products_Profit,Ticket_Profit,Gross_Profit,Net_Profit) VALUES(@year,@month,@water,@largeSD,@smallSD,@buttered,@salted,@sweet,@adult,@teen,@child,@product,@ticket,@gross,@net)"
cmd.Parameters.Add("@year", MySqlDbType.VarChar).Value = Year
cmd.Parameters.Add("@month", MySqlDbType.VarChar).Value = Month
cmd.Parameters.Add("@water", MySqlDbType.VarChar).Value = Items(3, C)
cmd.Parameters.Add("@largeSD", MySqlDbType.VarChar).Value = Items(4, C)
cmd.Parameters.Add("@smallSD", MySqlDbType.VarChar).Value = Items(5, C)
cmd.Parameters.Add("@buttered", MySqlDbType.VarChar).Value = Items(6, C)
cmd.Parameters.Add("@salted", MySqlDbType.VarChar).Value = Items(7, C)
cmd.Parameters.Add("@sweet", MySqlDbType.VarChar).Value = Items(8, C)
cmd.Parameters.Add("@adult", MySqlDbType.VarChar).Value = Items(9, C)
cmd.Parameters.Add("@teen", MySqlDbType.VarChar).Value = Items(10, C)
cmd.Parameters.Add("@child", MySqlDbType.VarChar).Value = Items(11, C)
cmd.Parameters.Add("@product", MySqlDbType.VarChar).Value = Items(1, C) 'This is product profit - employee form saves data in file first
cmd.Parameters.Add("@ticket", MySqlDbType.VarChar).Value = Items(2, C) 'This is ticket profit - employee form saves data in file second then its water
cmd.Parameters.Add("@gross", MySqlDbType.VarChar).Value = Items(12, C)
cmd.Parameters.Add("@net", MySqlDbType.VarChar).Value = Items(13, C)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
MySqlConn.Close()
sr.Close()
End Try
ElseIf C = 31 Then
MsgBox("Imported Data From " & MonthName(Month, True) & " " & Year & vbNewLine & "Successfully uploaded data to SQL Database.")
End If
C = C + 1
Loop
Else
MsgBox("Please select a product above to import its data.")
End If
答案 0 :(得分:0)
谢谢大家的帮助,我终于想通了“对象引用未设置为对象的实例”。实际上是指@nick正在谈论的那条线。
代码的问题是:
MySqlConn.ConnectionString = "Placeholder for pasting onto website"
MySqlConn.Open()
cmd.Connection = MySqlConn
我用MySqlConn = New MySqlConnection
替换了该代码 MySqlConn.ConnectionString = "Placeholder for pasting onto website"
Query = "INSERT INTO Cinema_Graph_Data (Rest of the giant query)
cmd = New MySqlCommand(Query, MySqlConn)
所有参数底部都有MySQLConn.Open()
。 cmd变量实际上根本没有初始化,因此是对象引用错误的目标。感谢您告诉我调试和感谢指出错误@Nick。
我现在唯一的问题是数据库无法翻译“£”符号而只显示“?”但我会尝试自己解决这个问题。再次感谢您的帮助!