我想检查Access数据库中是否已存在条目。通过我的研究,我成功地为此获得了一些代码,但它给了我一些错误。你能指出我正确的方向来实现我想要的代码。
这是我的代码:
Dim commandText = "SELECT COUNT(*) AS FROM ESRRegister WHERE ID = '" & IDtxt.Text & "'"
Using (conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Database\Database.accdb"))
Using ("select = New OleDbCommand(commandText, conn)")
conn.open()
Dim count = Convert.ToInt32("select.ExecuteScalar()")
If count > 0 Then
MessageBox.Show("Already Exists!", "ALI ENTERPRISES", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else 'Your insert code cmd.CommandText = "insert into ESRRegister (Dt,ID)VALUES ('" & Dttxt.Text & "' , '" & IDtxt.Text & "')" queryResult = cmd.ExecuteScalar() MsgBox("Added Successfuly")
End If
End Using
End Using
如果您对代码提供一些解释,那将是很棒的,bcoz我是.NET的新手
非常感谢
答案 0 :(得分:0)
'The SQL instruction as a string
commandText = "SELECT COUNT(*) AS FROM ESRRegister WHERE ID = '" & IDtxt.Text & "'"
'Open the connection to the Database
Connection.Open()
'Use the SQLinstruction to create an OleDBcommand
Dim mySQLCommand As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand(commandText, Connection)
'Perform the SQL-instruction
Dim count as integer = CInt(mySQLCommand.ExecuteScalar)
mySQLCommand.Dispose() 'Clean the command object
Connection.Close() 'Close/Clean connection
答案 1 :(得分:0)
Dim commandText As String = "SELECT NZ(COUNT(*),0) FROM ESRRegister WHERE ID = '" & IDtxt.Text & "'"
Using conn as New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Database\Database.accdb")
dim Cmd As New OleDbCommand(commandText, conn
conn.open()
Dim count as intiger = Convert.ToInt32(Cmd.ExecuteScalar())
If count > 0 Then
MessageBox.Show("Already Exists!", "ALI ENTERPRISES", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
cmd.CommandText = "insert into ESRRegister (Dt,ID)VALUES ('" & Dttxt.Text & "' , '" & IDtxt.Text & "')"
Dim queryResult as Variant = cmd.ExecuteNonQueryr()
MsgBox("Added Successfuly")
End If
End Using
select
是VB.net中保存的单词,我将OleDbCommand
的名称更改为Cmd