我已经设法创建了与数据库的连接,并且能够从我的表单中保存信息,表单包含22个文本框,一个保存和另一个退出按钮。
我已将表单设置为在form_load中检索数据。我在保存按钮上使用了“UPDATE”SQL命令,它确实将数据保存在所有22个文本框中(所有文本框都链接到它们各自的列)。但是只需要一条记录就是我没有使用“INSERT”命令的原因。
当我点击保存按钮,所有(19)文本框都被保存但当我检索文本(通过重新加载表单)时,问题就出现了,20,21,22个文本框中的每一个(仅)20的文本文本框继续移动下一个文本框,例如20> 21> 22> 20> 21> 22彼此互换。
请帮助我(我用谷歌搜索但没有发现任何内容)请告诉我为什么会这样。
代码如下:
Dim sqCon As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;Database=MegaDatabase;Integrated Security=SSPI")
Dim sqCmd As New SqlClient.SqlCommand
sqCmd.Connection = sqCon
Dim DatabaseErrorMsg As String = "Unable to save the details. Please contact the program developers."
Dim DatabaseErrorTitle As String = "Database Editing Error"
Dim DatabaseDoneSave As String = "New records made/updated successfully"
Dim CompNmtxtS As String
Dim TrdNmtxtS As String
Dim ComRegtxtS As String
Dim WTNmtxtS As String
Dim VRegtxtS As String
Dim TextBox1S As String
Dim ComPosttxtS As String
Dim StrAddrtxtS As String
Dim ComCitytxtS As String
Dim ComCounttxtS As String
Dim RegAddrtxtS As String
Dim ComZiptxtS As String
Dim RepTeltxtS As String
Dim ComFaxtxtS As String
Dim RepCelltxtS As String
Dim W_URLtxtS As String
Dim EWebtxtS As String
Dim BankNametxtS As String
Dim BankBranchtxtS As String
Dim BraCodetxtS As String
Dim BankAcctxtS As String
Dim TextBox2S As String
CompNmtxtS = CompNmtxt.Text
TrdNmtxtS = TrdNmtxt.Text
ComRegtxtS = ComRegtxt.Text
WTNmtxtS = WTNmtxt.Text
VRegtxtS = VRegtxt.Text
TextBox1S = TextBox1.Text
ComPosttxtS = ComPosttxt.Text
StrAddrtxtS = StrAddrtxt.Text
ComCitytxtS = ComCitytxt.Text
ComCounttxtS = ComCounttxt.Text
RegAddrtxtS = RegAddrtxt.Text
ComZiptxtS = ComZiptxt.Text
RepTeltxtS = RepTeltxt.Text
ComFaxtxtS = ComFaxtxt.Text
RepCelltxtS = RepCelltxt.Text
ComFaxtxtS = ComFaxtxt.Text
W_URLtxtS = W_URLtxt.Text
EWebtxtS = EWebtxt.Text
BankNametxtS = BankNametxt.Text
BankBranchtxtS = BankBranchtxt.Text
BraCodetxtS = BraCodetxt.Text
BankAcctxtS = BankAcctxt.Text
TextBox2S = TextBox2.Text
Try
'*NOTE: UPDATE function will only UPDATE the fields when there is already something in there, as it cannot work for the INSERT command
'Format for UPDATE command: USE DatabaseName; UPDATE Tablename SET ColumnName = '" & declared string name & "'"
sqCmd.CommandText = ("USE MegaDatabase; UPDATE CompDetails SET CompName = '" & CompNmtxtS & "', TradeName = '" & TrdNmtxtS & "', CompReg = '" & ComRegtxtS & "', WTnum = '" & WTNmtxtS & "', VATregNo = '" & VRegtxtS & "', TaxPeriod = '" & TextBox1S & "', CompPostalAddr = '" & ComPosttxtS & "', CompPhysAddr = '" & StrAddrtxtS & "', CompCity = '" & ComCitytxtS & "', CompCountry = '" & ComCounttxtS & "', CompProvince = '" & RegAddrtxtS & "', CompZip = '" & ComZiptxtS & "', CompTel = '" & RepTeltxtS & "', CompFax = '" & ComFaxtxtS & "', CompCell = '" & RepCelltxtS & "', CompWebsite = '" & W_URLtxtS & "', CompEmail = '" & EWebtxtS & "', CompBankName = '" & BankNametxtS & "', CompBranchName = '" & BankBranchtxtS & "', CurrentTaxTable = '" & TextBox2S & "', CompBranchCode = '" & BraCodetxtS & "', CompAccNo = '" & BankAcctxtS & "'")
sqCon.Open()
sqCmd.ExecuteNonQuery()
sqCon.Close()
Catch ex As Exception
MessageBox.Show(DatabaseErrorMsg, DatabaseErrorTitle, MessageBoxButtons.OK)
Finally
Me.Close()
frmMDImainform.MainMenuStrip.Enabled = True
MessageBox.Show(DatabaseDoneSave, "Done Saving...", MessageBoxButtons.OK)
End Try
对于装载部分:
Try
sqCmd.CommandText = "SELECT TOP 1 [CompName],[TradeName],[CompReg],[WTnum],[VATregNo],[TaxPeriod],[CompPostalAddr],[CompPhysAddr],[CompCity],[CompCountry],[CompProvince],[CompZip],[CompTel],[CompFax],[CompCell],[CompWebsite],[CompEmail],[CompBankName],[CompBranchName],[CurrentTaxTable],[CompBranchCode],[CompAccNo] FROM [MegaDatabase].[dbo].[CompDetails]"
sqCon.Open()
sqRdr = sqCmd.ExecuteReader()
Catch ex As Exception
End Try
Do While sqRdr.Read() 'No need for VbTab and Vb crlf
CompNmtxt.Text = CompNmtxt.Text & sqRdr.GetValue(0)
TrdNmtxt.Text = TrdNmtxt.Text & sqRdr.GetValue(1)
ComRegtxt.Text = ComRegtxt.Text & sqRdr.GetValue(2)
WTNmtxt.Text = WTNmtxt.Text & sqRdr.GetValue(3)
VRegtxt.Text = VRegtxt.Text & sqRdr.GetValue(4)
TextBox1.Text = TextBox1.Text & sqRdr.GetValue(5)
ComPosttxt.Text = ComPosttxt.Text & sqRdr.GetValue(6)
StrAddrtxt.Text = StrAddrtxt.Text & sqRdr.GetValue(7)
ComCitytxt.Text = ComCitytxt.Text & sqRdr.GetValue(8)
ComCounttxt.Text = ComCounttxt.Text & sqRdr.GetValue(9)
RegAddrtxt.Text = RegAddrtxt.Text & sqRdr.GetValue(10)
ComZiptxt.Text = ComZiptxt.Text & sqRdr.GetValue(11)
RepTeltxt.Text = RepTeltxt.Text & sqRdr.GetValue(12)
ComFaxtxt.Text = ComFaxtxt.Text & sqRdr.GetValue(13)
RepCelltxt.Text = RepCelltxt.Text & sqRdr.GetValue(14)
W_URLtxt.Text = W_URLtxt.Text & sqRdr.GetValue(15)
EWebtxt.Text = EWebtxt.Text & sqRdr.GetValue(16)
BankNametxt.Text = BankNametxt.Text & sqRdr.GetValue(17)
BankBranchtxt.Text = BankBranchtxt.Text & sqRdr.GetValue(18)
BraCodetxt.Text = BraCodetxt.Text & sqRdr.GetValue(19)
BankAcctxt.Text = BankAcctxt.Text & sqRdr.GetValue(20)
TextBox2.Text = TextBox2.Text & sqRdr.GetValue(21)
Loop
感谢您阅读
答案 0 :(得分:1)
您在查询中按列的顺序混合,而在读取结果集时按索引访问列:
在查询列#21(最后一列,从零开始的索引)是[CompAccNo]
但在阅读时将第21列分配给TextBox2.Text。
我建议您在阅读时按名称访问列:
CompNmtxt.Text = CompNmtxt.Text & sqRdr.GetValue("CompName")
TrdNmtxt.Text = TrdNmtxt.Text & sqRdr.GetValue("TradeName")
ComRegtxt.Text = ComRegtxt.Text & sqRdr.GetValue("CompReg")
'...
另一点:通过连接用户输入来摆脱创建更新查询 - 改为使用Command Parameters!