我的更新查询是否正确? sstudentid是studentregister表的主键?它可以更新吗? [英] Is my update query correct? sstudentid is the primary key of studentregister table? and can it be updated?
问题描述
Private Sub editbtn_Click(sender as System。 Object ,e As System.EventArgs)处理editbtn.Click
如果不是CheckEmptyTextbox(GroupBox1)然后
退出Sub
结束如果
如果stucntcttxt.Text.Length<> 10 然后
MsgBox( 联系号码必须长10个字符,MsgBoxStyle.Information)
退出Sub
结束如果
Dim Valid As Boolean
尝试
有效= Regex.IsMatch(stuemailtxt.Text, \ A( ?:[!A-Z0-9#$%&放大器; '!?* + / = ^ _`{|}〜 - ] +(?:\ [A-Z0-9#$%&放大器;' * ?+ / = ^ _`{|}〜 - ] +)* @(?:?[A-Z0-9](在[a-Z0-9 - ] * [A-Z0-9])\ 。)+ [a-z0-9](?:[a-z0-9 - ] * [a-z0-9])?)\ Z,RegexOptions.IgnoreCase)
Catch ex As Exception
结束尝试
如果无效则
MsgBox( 输入有效Emailid,MsgBoxStyle.Information)
退出Sub
结束如果
' Dim con As New SqlConnection(Data Source = ASHUTOSH-PC\SQLEXPRESS; Initial Catalog = Art Station Management System; Integrated Security = True)
Dim str As String
Dim i = CInt(stuidcmb.Text)
Dim a = CInt(stuagetxt.Text)
Dim gender As String = String.Empty
如果stumalerd.Checked那么
gender =Male
ElseIf stufemalerd.Checked然后
gender =Female
结束如果
con.Open()
str =更新StudentRegister set sname = @ sname,smiddlename = @smiddlename,ssurname = @ssurname,sdob = @sdob,ccoursename = @ccoursename,btbatchid = @btbatchid,sage = @sage,sgender = @sgender,scontact = @scontact,saddress = @ saddress,semailid = @ semailid,sdatereg = @sdatereg其中sstudentid = @sstudentid
com = New SqlCommand(str,con)
com.Parameters.AddWithValue(@ sstudentid,stuidcmb.Text)
com。 Parameters.AddWithValu e(@ sname,stufnametxt.Text)
com.Parameters.AddWithValue(@ smiddlename,stumnametxt.Text)
com.Parameters.AddWithValue(@ ssurname,stulnametxt.Text)
com.Parameters.AddWithValue(@ sdob,birthdate.Value.Date)
com.Parameters.AddWithValue(@ ccoursename,coursenamecmb.Text)
com.Parameters.AddWithValue( @btbatchid,batchidcmb.Text)
com.Parameters.AddWithValue(@ sage,a)
com.Parameters.AddWithValue(@ sgender,gender)
com.Parameters。 AddWithValue(@ scontact,stucntcttxt.Text)
com.Parameters.AddWithValue(@ saddress,stuaddtxt.Text)
com.Parameters.AddWithValue(@ semailid,stuemailtxt.Text)
com.Parameters.AddWithValue(@ sdatereg,regdate.Value.Date)
com.CommandText = str
com.ExecuteNonQuery()
con.Close()
MsgBox(记录编辑成功,MsgBoxStyle.Information)
显示()
clear()
End Sub
我的尝试:
更新主要是正确的?
%&'* + / =? ^ _` {|}〜 - ] +(?: \ [A-Z0-9#
%安培;!?* + / = ^ _`{|} 〜 - ] +)* @(:[α-Z0-9](?:?[A-Z0-9 - ] * [A-Z0-9])?。\)+ [A-Z0-9] (?:[a-z0-9 - ] * [a-z0-9])?)\ Z,RegexOptions.IgnoreCase)
Catch ex As Exception
End Try
如果无效则
MsgBox( 输入有效的Emailid,MsgBoxStyle.Information )
退出Sub
结束如果
' Dim con As New SqlConnection(数据源= ASHUTOSH-PC \ SQLEXPRESS;初始目录=艺术站管理系统;集成安全性=真)
Dim str As String
Dim i = CInt(stuidcmb.Text)
迪ma = CInt(stuagetxt.Text)
Dim gender As String = String.Empty
如果stumalerd.Checked那么
gender =Male
ElseIf stufemalerd.Checked那么
gender =Female
End if
con.Open()
str =更新StudentRegister set sname = @sname,smiddlename = @smiddlename,ssurname = @ssurname, sdob = @sdob,ccoursename = @ccoursename,btbatchid = @btbatchid,sage = @sage,sgender = @ sgender,scontact = @scontact,saddress = @ saddress,semailid = @semailid,sdatereg = @sdatereg where sstudentid = @sstudentid
com = New SqlCommand(str,con)
com.Parameters.AddWithValue(@ sstudentid,stuidcmb.Text)
com.Parameters.AddWithValue(@ sname,stufnametxt.Text )
com.Parameters.AddWithValue(@ smiddlename,stumnametxt.Text)
com.Parameters.AddWithValue(@ ssurname,stulnametxt.Text)
com.Parameters.AddWithValue( @sdob,birthdate.Valu e.Date)
com.Parameters.AddWithValue(@ ccoursename,coursenamecmb.Text)
com.Parameters.AddWithValue(@ btbatchid,batchidcmb.Text)
com.Parameters。 AddWithValue(@ sage,a)
com.Parameters.AddWithValue(@ sgender,gender)
com.Parameters.AddWithValue(@ scontact,stucntcttxt.Text)
com .Parameters.AddWithValue(@ saddress,stuaddtxt.Text)
com.Parameters.AddWithValue(@ semailid,stuemailtxt.Text)
com.Parameters.AddWithValue(@ sdatereg,regdate。 Value.Date)
com.CommandText = str
com.ExecuteNonQuery()
con.Close()
MsgBox(记录编辑成功,MsgBoxStyle.Information)
显示()
clear()
结束子
我尝试了什么:
更新主数据是正确的?
您没有更新主数据库钥匙 - 你说的是sstudentid - 你正在为那个学生和那个学生更新数据。
看看你的UPDATE声明(我已经拿出了一些错误的东西,所以你可以看到重要的一点):
str = UPDATE StudentRegister SET sname = @sname,.. 。,sdatereg = @sdatereg WHERE sstudentid = @sstudentid
您没有在SET子句中列出sstudentid,只是在WHERE中 - 并且只列出了SET子句中指定的列将被改变。 WHERE子句定义将更新哪些行。在这种情况下,因为WHERE子句引用主键,所以只会更改一行,因为主键值必须是唯一的。
顺便说一句:对于SQL关键字使用UPPER CASE是个好主意它们在复杂的SQL语句中脱颖而出。
Private Sub editbtn_Click(sender As System.Object, e As System.EventArgs) Handles editbtn.Click
If Not CheckEmptyTextbox(GroupBox1) Then
Exit Sub
End If
If stucntcttxt.Text.Length <> 10 Then
MsgBox("Contact Number Must Be 10 Characters Long", MsgBoxStyle.Information)
Exit Sub
End If
Dim Valid As Boolean
Try
Valid = Regex.IsMatch(stuemailtxt.Text, "\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase)
Catch ex As Exception
End Try
If Not Valid Then
MsgBox("Enter Valid Emailid", MsgBoxStyle.Information)
Exit Sub
End If
'Dim con As New SqlConnection("Data Source=ASHUTOSH-PC\SQLEXPRESS;Initial Catalog=Art Station Management System;Integrated Security=True")
Dim str As String
Dim i = CInt(stuidcmb.Text)
Dim a = CInt(stuagetxt.Text)
Dim gender As String = String.Empty
If stumalerd.Checked Then
gender = "Male"
ElseIf stufemalerd.Checked Then
gender = "Female"
End If
con.Open()
str = "Update StudentRegister set sname = @sname, smiddlename = @smiddlename, ssurname = @ssurname, sdob = @sdob, ccoursename = @ccoursename, btbatchid = @btbatchid, sage = @sage, sgender = @sgender,scontact = @scontact,saddress = @saddress,semailid = @semailid, sdatereg=@sdatereg where sstudentid = @sstudentid"
com = New SqlCommand(str, con)
com.Parameters.AddWithValue("@sstudentid", stuidcmb.Text)
com.Parameters.AddWithValue("@sname", stufnametxt.Text)
com.Parameters.AddWithValue("@smiddlename", stumnametxt.Text)
com.Parameters.AddWithValue("@ssurname", stulnametxt.Text)
com.Parameters.AddWithValue("@sdob", birthdate.Value.Date)
com.Parameters.AddWithValue("@ccoursename", coursenamecmb.Text)
com.Parameters.AddWithValue("@btbatchid", batchidcmb.Text)
com.Parameters.AddWithValue("@sage", a)
com.Parameters.AddWithValue("@sgender", gender)
com.Parameters.AddWithValue("@scontact", stucntcttxt.Text)
com.Parameters.AddWithValue("@saddress", stuaddtxt.Text)
com.Parameters.AddWithValue("@semailid", stuemailtxt.Text)
com.Parameters.AddWithValue("@sdatereg", regdate.Value.Date)
com.CommandText = str
com.ExecuteNonQuery()
con.Close()
MsgBox("Record Edited Successfully", MsgBoxStyle.Information)
Display()
clear()
End Sub
What I have tried:
Updating Primary would be right?
%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#
%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase) Catch ex As Exception End Try If Not Valid Then MsgBox("Enter Valid Emailid", MsgBoxStyle.Information) Exit Sub End If 'Dim con As New SqlConnection("Data Source=ASHUTOSH-PC\SQLEXPRESS;Initial Catalog=Art Station Management System;Integrated Security=True") Dim str As String Dim i = CInt(stuidcmb.Text) Dim a = CInt(stuagetxt.Text) Dim gender As String = String.Empty If stumalerd.Checked Then gender = "Male" ElseIf stufemalerd.Checked Then gender = "Female" End If con.Open() str = "Update StudentRegister set sname = @sname, smiddlename = @smiddlename, ssurname = @ssurname, sdob = @sdob, ccoursename = @ccoursename, btbatchid = @btbatchid, sage = @sage, sgender = @sgender,scontact = @scontact,saddress = @saddress,semailid = @semailid, sdatereg=@sdatereg where sstudentid = @sstudentid" com = New SqlCommand(str, con) com.Parameters.AddWithValue("@sstudentid", stuidcmb.Text) com.Parameters.AddWithValue("@sname", stufnametxt.Text) com.Parameters.AddWithValue("@smiddlename", stumnametxt.Text) com.Parameters.AddWithValue("@ssurname", stulnametxt.Text) com.Parameters.AddWithValue("@sdob", birthdate.Value.Date) com.Parameters.AddWithValue("@ccoursename", coursenamecmb.Text) com.Parameters.AddWithValue("@btbatchid", batchidcmb.Text) com.Parameters.AddWithValue("@sage", a) com.Parameters.AddWithValue("@sgender", gender) com.Parameters.AddWithValue("@scontact", stucntcttxt.Text) com.Parameters.AddWithValue("@saddress", stuaddtxt.Text) com.Parameters.AddWithValue("@semailid", stuemailtxt.Text) com.Parameters.AddWithValue("@sdatereg", regdate.Value.Date) com.CommandText = str com.ExecuteNonQuery() con.Close() MsgBox("Record Edited Successfully", MsgBoxStyle.Information) Display() clear() End Sub
What I have tried:
Updating Primary would be right?
You aren't updating the primary key - which you say is sstudentid - you are updating the data for that student, and that student only.
Look at your UPDATE statement (I've taken out the miscelaneous stuff so you can see the important bit):
str = "UPDATE StudentRegister SET sname = @sname, ... , sdatereg=@sdatereg WHERE sstudentid = @sstudentid"You don't list the sstudentid in the SET Clause, just in the WHERE - and only columns which are specified in the SET clause will be changed. The WHERE clause defines which row(s) will be updated. In this case because your WHERE clause references the primary key, only one row will be changed as primary key values must be unique.
BTW: It's a good idea to use UPPER CASE for SQL keywords and it makes them "stand out" better in a complex SQL statement.
这篇关于我的更新查询是否正确? sstudentid是studentregister表的主键?它可以更新吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!