需要帮助我的代码 [英] Need help for my code
问题描述
嗨guyz,
i在我的代码中有问题将字符插入访问数据库...
请查看我的代码...
此代码适用于铁路预订系统的项目...在此代码中我试图将一些新列车添加到数据库中...我无法在数据库中插入任何值...总是没有价值给予一个或多个参数错误..请帮助我
代码如下.........
hi guyz,
i have a problem in my code to insert the characters into the access database ...
plz view my code...
This code is for a Railway Reservation System's Project ...in this code i am trying to add some new trains into the database ...i can't insert any value in database ...always "NO VALUE GIVEN FOR ONE OR MORE PARAMETERS" error..Please help me
the code is as following .........
Imports System.Data.OleDb
Imports System.String
Public Class Add_Trains
Private Sub Add_Trains_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CurrentRow = 0
con.Open()
Dad = New OleDbDataAdapter("SELECT * FROM TRAINS ORDER BY T_ID", con)
Dad.Fill(Dst, "RRS")
ShowData(CurrentRow)
con.Close()
End Sub
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.SelectAll()
End Sub
' To clear all fields : Train name, Train number, Train ID, Coach numbers, seat numbers, source, Destination
Private Sub Clear()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
End Sub
' To select the data in train number Field
Private Sub TextBox2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.GotFocus
TextBox2.SelectAll()
End Sub
' To select the data in train ID Field
Private Sub TextBox3_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.GotFocus
TextBox3.SelectAll()
End Sub
' To select the data in train ID Field
Private Sub TextBox4_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.GotFocus
TextBox4.SelectAll()
End Sub
' To select the data in train ID Field
Private Sub TextBox5_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox5.GotFocus
TextBox5.SelectAll()
End Sub
' To select the data in train ID Field
Private Sub TextBox6_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox6.GotFocus
TextBox6.SelectAll()
End Sub
' To select the data in train ID Field
Private Sub TextBox7_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox7.GotFocus
TextBox7.SelectAll()
End Sub
Private Sub ShowData(ByVal CurrentRow As Integer)
Try
TextBox1.Text = Dst.Tables("RRS").Rows(CurrentRow)("Train_name")
TextBox2.Text = Dst.Tables("RRS").Rows(CurrentRow)("T_num")
TextBox3.Text = Dst.Tables("RRS").Rows(CurrentRow)("T_ID")
TextBox4.Text = Dst.Tables("RRS").Rows(CurrentRow)("Coach_num")
TextBox5.Text = Dst.Tables("RRS").Rows(CurrentRow)("Seat_num")
TextBox6.Text = Dst.Tables("RRS").Rows(CurrentRow)("Source")
TextBox7.Text = Dst.Tables("RRS").Rows(CurrentRow)("Destination")
Catch ex As Exception
End Try
End Sub
' To check the data in Train Name Field : whether a string or not
Private Function CheckTrain_name()
Try
If TextBox1.Text = "" Or ValidateString(TextBox1.Text) = False Then
ShowData(CurrentRow)
TextBox1.Focus()
Return False
End If
Catch ex As Exception
End Try
Return True
End Function
' To check the string for numeric values
Private Function ValidateString(ByVal Str)
Dim i As Integer
Dim ch As Char
i = 0
While i < Str.Length()
ch = Str.Chars(i)
If IsNumeric(ch) = True Then
Return False
End If
i += 1
End While
Return True
End Function
' To check whether Train name exist in database
Private Function IsTrain_nameExist()
Dim Str, Str1 As String
Dim i As Integer
Str = TextBox1.Text
i = 0
While i <> Dst.Tables("RRS").rows.count
Str1 = Dst.Tables("RRS").Rows(i)("Train_name")
If Str = Str1 Then
Return True
End If
i += 1
End While
Return False
End Function
' To check the data in Train Number field : whether numeric or not
Private Function CheckT_num()
Try
If IsNumeric(TextBox2.Text) = False Then
ShowData(CurrentRow)
TextBox2.Focus()
Return False
End If
Catch ex As Exception
End Try
Return True
End Function
' To check the data in Train ID field : whether numeric or not
Private Function CheckT_ID()
Try
If IsNumeric(TextBox3.Text) = False Then
ShowData(CurrentRow)
TextBox3.Focus()
Return False
End If
Catch ex As Exception
End Try
Return True
End Function
' To check the data in Coach Numbers field : whether numeric or not
Private Function CheckCoach_num()
Try
If IsNumeric(TextBox4.Text) = False Then
ShowData(CurrentRow)
TextBox4.Focus()
Return False
End If
Catch ex As Exception
End Try
Return True
End Function
' To check the data in Seat Numbers field : whether numeric or not
Private Function Checkseat_num()
Try
If IsNumeric(TextBox5.Text) = False Then
ShowData(CurrentRow)
TextBox5.Focus()
Return False
End If
Catch ex As Exception
End Try
Return True
End Function
' To check the data in source Field : whether a string or not
Private Function CheckSource()
Try
If TextBox6.Text = "" Or ValidateString(TextBox6.Text) = False Then
ShowData(CurrentRow)
TextBox6.Focus()
Return False
End If
Catch ex As Exception
End Try
Return True
End Function
' To check the data in Destination Field : whether a string or not
Private Function CheckDestination()
Try
If TextBox7.Text = "" Or ValidateString(TextBox7.Text) = False Then
ShowData(CurrentRow)
TextBox7.Focus()
Return False
End If
Catch ex As Exception
End Try
Return True
End Function
Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Str As String
If CheckTrain_name() = False Then
MsgBox("Train_name : Only Character Values Allowed !!!")
Exit Sub
ElseIf IsTrain_nameExist() = True Then
MsgBox("Train_name : Train_name is already exist. Please choose another one")
Exit Sub
ElseIf CheckT_num() = False Then
MsgBox("T_num : Integer Valur Required!!!")
Exit Sub
ElseIf CheckT_ID() = False Then
MsgBox("T_ID : Integer Value Required !!!")
Exit Sub
ElseIf CheckCoach_num() = False Then
MsgBox("Coach_num : Integer Value Required !!!")
Exit Sub
ElseIf Checkseat_num() = False Then
MsgBox("Seat_num : Integer Value Required !!!")
Exit Sub
ElseIf CheckSource() = False Then
MsgBox("Source : Only Character Values Allowed !!!")
Exit Sub
ElseIf CheckDestination() = False Then
MsgBox("Destination : Only Character Values Allowed !!!")
Exit Sub
End If
Try
Str = "insert into TRAINS values("
Str += TextBox1.Text.Trim()
Str += ","
Str += """" & TextBox2.Text.Trim() & """"
Str += ","
Str += """" & TextBox3.Text.Trim() & """"
Str += ","
Str += """" & TextBox4.Text.Trim() & """"
Str += ","
Str += """" & TextBox5.Text.Trim() & """"
Str += ","
Str += """" & TextBox6.Text.Trim() & """"
Str += ","
Str += """" & TextBox7.Text.Trim() & """"
Str += ")"
Con.Open()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Dst.Clear()
Dad = New OleDbDataAdapter("SELECT * FROM TRAINS ORDER BY T_ID", Con)
Dad.Fill(Dst, "RRS")
MsgBox("Record inserted successfully...")
Con.Close()
Catch ex As Exception
MessageBox.Show("Could Not Insert Record!!!")
MsgBox(ex.Message & " - " & ex.Source)
Con.Close()
End Try
Administrator_Entry.Show()
Me.Hide()
End Sub
End Class
[edit]已添加代码块 - OriginalGriff [/ edit]
[edit]Code block added - OriginalGriff[/edit]
推荐答案
你需要用单个单引号重复双引号:
You need to replce the double quotes with single:
Str = "insert into TRAINS values("
Str += "'" & TextBox1.Text.Trim() & "',"
Str += "'" & TextBox2.Text.Trim() & "',"
Str += "'" & TextBox3.Text.Trim() & "',"
Str += "'" & TextBox4.Text.Trim() & "',"
Str += "'" & TextBox5.Text.Trim() & "',"
Str += "'" & TextBox6.Text.Trim() & "',"
Str += "'" & TextBox7.Text.Trim() & "')"
但不要'这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。
哦,不要使用VS默认名称!您可能还记得今天哪个文本框会产生什么,但是在三周之后呢?使用合理的名字!
But don't do it that way! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Oh, and don't use VS default names for everything! You may remember which textbox hodls what today, but in three weeks time? Use sensible names!
这篇关于需要帮助我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!