Public Class Form4
Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As the DataSet
Private Sub Form4_Load (sender As Object, As EventArgs e) Handles MyBase. Load
Dim CNSTR As String="Data Source=(local); Initial Catalog=AmusementPark; Integrated Security=true;"
Cn=New SqlConnection (CNSTR)
Da=New SqlDataAdapter (" select * from tourists table ", cn)
Ds=New DataSet ()
Da. The Fill (ds, tourists "table")
DataGridView1. The DataSource=ds. The Tables (" tourist table ")
TextBox1. Text=""
TextBox2. Text=""
TextBox3. Text=""
TextBox4. Text=""
TextBox5. Text=""
TextBox6. Text=""
End Sub
Private Sub Button10_Click (sender As Object, As EventArgs e) Handles Button10. Click
Me. Hide ()
Form2. The Show ()
End Sub
Private Sub DataGridView1_CellContentClick (sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1. CellContentClick
End Sub
Private Sub TextBox1_TextChanged (sender As Object, As EventArgs e) Handles TextBox1. The TextChanged
End Sub
Private Sub Button7_Click (ByVal sender As Object, ByVal As EventArgs e) Handles Button7. Click
If Me. TextBox1. Text & lt;> "" And Me. TextBox2. Text & lt;> "" Then
Dim the insert As String
Insert="insert into tourist table (VName Vid, ID, FP, Pword, Birthdate)"
Insert=insert & amp; "Values (@ ei, @ ci, @ cc, @ zyh, @ ce, @ CG)"
Call the cn. The Open ()
Dim CMD As New SqlClient. SqlCommand (insert, cn)
CMD. The Parameters. The Add (" @ ei, SqlDbType. BigInt). The Value=https://bbs.csdn.net/topics/Trim (TextBox1. Text)
CMD. The Parameters. The Add (" @ ci ", SqlDbType. NVarChar). The Value=https://bbs.csdn.net/topics/Trim (TextBox2. Text)
CMD. The Parameters. The Add (" @ cc, SqlDbType. Char). The Value=https://bbs.csdn.net/topics/Trim (TextBox3. Text)
CMD. The Parameters. The Add (" @ zyh, SqlDbType. NVarChar). The Value=https://bbs.csdn.net/topics/Trim (TextBox4. Text)
CMD. The Parameters. The Add (" @ CG ", SqlDbType. Date). The Value=https://bbs.csdn.net/topics/Trim (TextBox6. Text)
CMD. The Parameters. The Add (" @ ce ", SqlDbType. NVarChar). The Value=https://bbs.csdn.net/topics/Trim (TextBox5. Text)
Dim v=CMD. ExecuteNonQuery ()
MsgBox (" add success!" , MsgBoxStyle. Information, "tip")
Call the cn. The Close ()
The Else
MsgBox (" visitors name and number can't be empty!" , MsgBoxStyle. Information, "tip")
End the If
End Sub
Private Sub Button9_Click (ByVal sender As Object, ByVal As EventArgs e) Handles Button9. Click
If Me. TextBox1. Text & lt;> "" And Me. TextBox2. Text & lt;> "" Then
Dim edit As String
Edit="update table set Vid=@ ei, visitors VName=@ ci, ID=@ cc, FP=@ zyh, Pword=@ ce, Birthdate=@ CG"
Edit=edit & amp; "Where name='" & amp; Trim (TextBox1. Text) & amp; "'
"Call the cn. The Open ()
Dim CMD As New SqlClient. SqlCommand (edit, cn)
CMD. The Parameters. The Add (" @ ei, SqlDbType. BigInt). The Value=https://bbs.csdn.net/topics/Trim (TextBox1. Text)
CMD. The Parameters. The Add (" @ ci ", SqlDbType. NVarChar). The Value=https://bbs.csdn.net/topics/Trim (TextBox2. Text)
CMD. The Parameters. The Add (" @ cc, SqlDbType. Char). The Value=https://bbs.csdn.net/topics/Trim (TextBox3. Text)
CMD. The Parameters. The Add (" @ zyh, SqlDbType. NVarChar). The Value=https://bbs.csdn.net/topics/Trim (TextBox4. Text)
CMD. The Parameters. The Add (" @ CG ", SqlDbType. Date). The Value=https://bbs.csdn.net/topics/Trim (TextBox6. Text)
CMD. The Parameters. The Add (" @ ce ", SqlDbType. NVarChar). The Value=https://bbs.csdn.net/topics/Trim (TextBox5. Text)
CMD. ExecuteNonQuery ()
MsgBox (" modify success!" , MsgBoxStyle. Information, "tip")
Call the cn. The Close ()
The Else
MsgBox (" visitors number and name cannot be empty!" , MsgBoxStyle. Information, "tip")
End the If
End Sub
CodePudding user response:
CMD. The Parameters. The Add (" @ CG ", SqlDbType. Date). The Value=https://bbs.csdn.net/topics/Trim (TextBox6. Text)
Do not match the code type
CodePudding user response:
TextBox6. The Text is what contents?Inserted into the database before, as much as possible to the judgment, each field is legal, such as a database of DataTime fields, insert a "ABC" will fail, if the judgment in advance, there is no need for additional an insert records, also do not need to rely on exceptions to catch this can completely avoid the input error,
CodePudding user response:
In addition, similar to theCMD. The Parameters. The Add (" @ ei, SqlDbType. BigInt). The Value=https://bbs.csdn.net/topics/Trim (TextBox1. Text)this type of code, is also the same kind of problem, don't rely on luck to write code, random luck code of the past, will be the connection completely in front of the end user, in front of a particular data and process, a disgrace,
CodePudding user response:
I am a medical students, SQL database design to come into contact with vb.net, so is there are many don't understand, you said that is a shame, but I just want to look for ways to solve the problem ofCodePudding user response:
Program, "starting from the type design" is a necessary quality, actually very clearly said a programmer at the beginning of the programming of the level of education, such asDim t=CType (" 2012/7/18 13:0 ", Date)strong type code you know have to be as far as possible to check at compile time, runtime error in advance as much as possible, rather than delayed,
Vague or even wrong type code, so obviously muddle through, if in the code review is if I look not to come out, is a serious problem, is a problem that will be strictly treated before mount guard,
CodePudding user response: