我需要帮助。我尝试使用以下编码将数据添加到数据库中。但是数据无法保存到数据库中。这意味着点击“保存”按钮
后,数据库中没有任何内容请检查我的编码。 tq
Imports System.Data.SqlClient
Public Class frmAddNewStaffAdmin
Dim con As SqlClient.SqlConnection
Dim dbSource As String
Dim cmd As SqlClient.SqlCommand
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
If Len(Trim(cboRole.Text)) = 0 Then
MessageBox.Show("Please select user type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
cboRole.Focus()
Return
ElseIf Len(Trim(txtStaffID.Text)) = 0 Then
MessageBox.Show("Please enter Staff ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtStaffID.Focus()
Return
ElseIf Len(Trim(txtStaffName.Text)) = 0 Then
MessageBox.Show("Please enter Staff Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtStaffName.Focus()
Return
ElseIf Len(Trim(txtUsername.Text)) = 0 Then
MessageBox.Show("Please enter Username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtUsername.Focus()
Return
ElseIf Len(Trim(txtPassword.Text)) = 0 Then
MessageBox.Show("Please enter Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtPassword.Focus()
Return
End If
dbSource = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HMS.mdf;Integrated Security=True;User Instance=True"
con = New SqlConnection(dbSource)
con.Open()
Dim sql As String = "INSERT INTO [User] (Staff_ID, Staff_Role, Staff_Name, Username, Password) VALUES ('" & txtStaffID.Text & "', '" & cboRole.Text & "', '" & txtStaffName.Text & "', '" & txtUsername.Text & "', '" & txtPassword.Text & "' ) "
cmd = New SqlCommand(sql)
cmd.Connection = con
cmd.ExecuteReader()
MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
答案 0 :(得分:0)
使用cmd.ExecuteNonQuery()
而不是cmd.ExecuteReader()
答案 1 :(得分:0)
您的代码中有两个错误
更改
Dim sql As String = "INSERT INTO [User] (Staff_ID, Staff_Role, Staff_Name, Username, Password) VALUES ('" & txtStaffID.Text & "', '" & cboRole.Text & "', '" & txtStaffName.Text & "', '" & txtUsername.Text & "', '" & txtPassword.Text & "' ) "
到
Dim sql As String = "INSERT INTO [User] (Staff_ID, Staff_Role, Staff_Name, Username, [Password]) VALUES ('" & txtStaffID.Text & "', '" & cboRole.Text & "', '" & txtStaffName.Text & "', '" & txtUsername.Text & "', '" & txtPassword.Text & "' ) "
编辑:
使用像这样的查询
Dim sql As String = "INSERT INTO [User] (Staff_ID, Staff_Role, Staff_Name, Username, [Password]) VALUES (@StaffID,@Role,@StaffName,@Username,@Password) "
Cmd.Parameters.AddWithValue("@Staff_ID", txtStaffID.Text)
Cmd.Parameters.AddWithValue("@Role", cboRole.Text)
Cmd.Parameters.AddWithValue("@StaffName", txtStaffName.Text)
Cmd.Parameters.AddWithValue("@Username", txtUsername.Text )
Cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
将Password
更改为[Password]
,因为Password
是保留关键字。
将cmd.ExecuteReader()
更改为cmd.ExecuteNonQuery()
ExecuteNonQuery
和ExecuteReader
<强>的ExecuteNonQuery 强>
ExecuteNonQuery方法将返回受影响的行数 INSERT,DELETE或UPDATE操作。这个ExecuteNonQuery方法会 仅用于插入,更新和删除,创建和SET 声明。 (Read More about ExecuteNonQuery)
SqlCommand.ExecuteNonQuery MSDN Documentation
<强>的ExecuteReader 强>
Execute Reader将用于在执行时返回行集 使用命令对象的SQL查询或存储过程。这个是 仅转发记录的检索,并用于读取表格 从头到尾的值。(Read More about ExecuteReader)