[英]Cannot insert into database using vb.net
I've got a problem and I don't know if its the insert query statement or the Database it self. 我遇到了问题,我不知道它是插入查询语句还是数据库本身。
This is the insert statement. 这是插入语句。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim value As Integer
If txtPassword.Text = txtPassCon.Text Then
Try
constring = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\tmsDB.mdf;Integrated Security=True"
con = New SqlConnection(constring)
con.Open()
cmd.Connection = con
cmd.CommandText = "Insert into Login (Username,Password) values ('" & txtUsername.Text & "' , '" & txtPassword.Text & "')"
cmd.ExecuteNonQuery()
cmd.CommandText = "select max(id) from login"
value = cmd.ExecuteScalar()
cmd.CommandText = "Insert into Scouts (Name,Club,Email, LoginID) values ('" & txtName.Text & "' , '" & txtClub.Text & "' , '" & txtEmail.Text & "', '" & value & "')"
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Registeration Sucessful")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End If
End Sub
When I run this seems to work because it successfully gets to the messagebox and shows me the message but when I check the db to see if its actually saved there, it doesn't show 当我运行时,这似乎起作用了,因为它成功进入了消息框并向我显示了消息,但是当我检查数据库以查看其是否确实保存在那里时,它不会显示
CREATE TABLE [dbo].[Login] (
[Id] INT NOT NULL IDENTITY ,
[Username] NVARCHAR (15) NOT NULL,
[Password] NVARCHAR (20) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
This is the table in the database. 这是数据库中的表。 set id to Identity because I thought that was how to make an auto number column
将id设置为Identity,因为我认为这是如何制作自动编号列
'Create ExecuteScalar function '创建ExecuteScalar函数
Public Function ExecuteScalar(query As String, ByVal ParamArray param() As Object) As int
Try
_commanda = New SqlCommand(query, _connection)
_commanda.CommandTimeout = 200
_sqlda = New SqlDataAdapter(_commanda)
_commanda.Transaction = _transaction
For i = 0 To param.Count - 1
_commanda.Parameters.AddWithValue("@" & i, param(i))
Next
dim value = _commanda.ExecuteScalar()
_sqlda.Dispose()
_commanda.Dispose()
Return value
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
Return nothing
End Try
End Function
'Create ExecuteNonQuery function '创建ExecuteNonQuery函数
Public Function ExecuteNonQuery(query As String, ByVal ParamArray param() As Object) As Boolean
Try
_commanda = New SqlCommand(query, _connection)
_commanda.CommandTimeout = 200
_sqlda = New SqlDataAdapter(_commanda)
_commanda.Transaction = _transaction
For i = 0 To param.Count - 1
_commanda.Parameters.AddWithValue("@" & i, param(i))
Next
_commanda.ExecuteNonQuery()
_sqlda.Dispose()
_commanda.Dispose()
Return True
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
Return False
End Try
End Function
'Call it '称它为
try
if _connection.State = ConnectionState.Closed then
_connection.open()
end if
if ExecuteNonQuery("Insert into Login values (@0,@1)",txtUsername.Text, txtPassword.Text) then
dim value = ExecuteScalar("select max(id) from login")
if ExecuteNonQuery("Insert into Scouts (Name,Club,Email, LoginID) values (@0,@1,@2,@3)", txtName.Text , txtClub.Text , txtEmail.Text, value ) then
_connection.Close()
MessageBox.Show("Registeration Sucessful")
else
MessageBox.Show("Registeration Not Sucessful")
end if
else
MessageBox.Show("Registeration Not Sucessful")
end if
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
End try
在catch块中将异常更改为SQLException也将有所帮助,例如Catch ex As SQLException MessageBox.Show(ex.ToString)End Try
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.