2016-09-29 91 views
-4
Dim okToadd As MsgBoxResult = MsgBox("Are you sure you want to add the current record?", MsgBoxStyle.YesNo) 
    If okToadd = MsgBoxResult.Yes Then 
     MsgBox("Admin information Added Sucessfully", MsgBoxStyle.OkOnly) 

     provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" 
     'Change the following to your access database location 
     dataFile = "C:\Users\talha\Documents\login.accdb" 
     connString = provider & dataFile 
     myConnection.ConnectionString = connString 

     myConnection.Open() 
     Dim str As String 
     str = "insert into loginusers ([username], [password], [firstname], [lastname]) values (?, ?, ?, ?)" 
     Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection) 


     cmd.Parameters.Add(New OleDbParameter("username", CType(TextBox1.Text, String))) 
     cmd.Parameters.Add(New OleDbParameter("password", CType(TextBox2.Text, String))) 
     cmd.Parameters.Add(New OleDbParameter("firstname", CType(TextBox3.Text, String))) 
     cmd.Parameters.Add(New OleDbParameter("lastname", CType(TextBox4.Text, String))) 







     Try 

      cmd.ExecuteNonQuery() 
      cmd.Dispose() 
      myConnection.Close() 
      TextBox1.Clear() 
      TextBox2.Clear() 
      TextBox3.Clear() 
      TextBox4.Clear() 
      DataGridView1.Refresh() 

     Catch ex As Exception 
      DataGridView1.Refresh() 
      MsgBox(ex.Message) 
      DataGridView1.Refresh() 
     End Try 
    ElseIf okToadd = MsgBoxResult.No Then 

    End If 
+2

首先,在尝试插入之前,您不应该显示“成功”消息框,如果插入失败并且您已经告诉用户它已经工作会发生什么?其次,访问数据库没有合并SQL函数,因此您需要添加一个查询来检查用户名是否已经存在,并且只有在不存在的情况下才会插入,或者您需要将用户名作为表的主键并将其插入在一个尝试赶上,将读取主键约束违规 – soohoonigan

回答

1

您可以在数据库中将用户名字段指定为主键。

+0

请详细告诉我 –

+0

请编辑代码 –

相关问题