VB中简单的增加、删除、修改、查询操作

'----------------Start-----------------------新增一条记录------------------------------

public Function InsertData()

Dim conn As New ADODB.Connection

Dim rs As New ADODB.Recordset

Dim strCnn As String

strCnn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=123;Initial Catalog=Import_DB"

conn.Open strCnn

strSQL = "select * from Stu"

rs.Open strSQL, conn, 3, 3

rs.AddNew
rs("name") = txtName.Text  '姓名
rs("age") = txtAge         '年龄
rs("num") = txtCardNum.Text '学号
rs.Update

rs.Close

conn.Close

MsgBox ("添加记录成功!")

End Function

'----------------End-----------------------新增一条记录------------------------------

'----------------Start-----------------------修改一条记录-----------------------------

public Function UpdateData()

Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset

Dim strCnn As String
Dim SqlSelect As String
 
strCnn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=123;Initial Catalog=Import_DB"

conn.Open strCnn
 
   SqlSelect = "SELECT * From Stu where num='" + txtCardNum.Text + "'"
    
     rs.Open SqlSelect, strCnn, adOpenDynamic, adLockOptimistic, adCmdText
       rs.UpdateBatch
        
         rs("name") = txtName.Text
         rs("age") = txtAge.Text
        
        rs.Update
     rs.Close
     conn.Close

End Function

'----------------End-----------------------修改一条记录-----------------------------

'----------------Start-----------------------删除一条记录-----------------------------

public Function DeleteData()

 

Dim conn As New ADODB.Connection

Dim strCnn As String
Dim SqlSelect As String
 
strCnn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=123;Initial Catalog=Import_DB"

conn.Open strCnn
 
   SqlSelect = "delete From Stu where num='" + txtCardNum.Text + "'"
    
    conn.Execute (SqlSelect)
 
     conn.Close

End Function

'----------------End-----------------------删除一条记录-----------------------------

'----------------Start-----------------------查询一条记录-----------------------------

public Function SelectData()

 

Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset

Dim strCnn As String
Dim SqlSelect As String
 
strCnn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=123;Initial Catalog=Import_DB"

conn.Open strCnn
 
   SqlSelect = "SELECT * From Stu "
    
      Set rs = conn.Execute(SqlSelect)
         If Not rs.EOF Then
          txtName.Text = rs("name")
          txtAge.Text = rs("age")
          txtCardNum.Text = rs("num")
         End If
     rs.Close 

     conn.Close

End Function

'----------------End-----------------------查询一条记录-----------------------------

 

你可能感兴趣的:(vb)