通过在inputbox中输入一个字段名,从而访问其他数据库字段名
怎样实现那
Imports System.Data.OleDb '引入ADO.NET操作命名空间 Module Module1 Public ADOcmd As OleDbDataAdapter Public ds As DataSet = New DataSet() '建立DataSet对象 Public mytable As DataTable '建立表单对象 Public myrow As DataRow '建立数据行对象 Public rownumber As Integer '定义一个整型变量来存放当前行数 Public SearchSQL As String Public cmd As OleDbCommandBuilder
Public Function Testtxt(ByVal txt As String) As Boolean '判断字符串是否为空 If Trim(txt) = "" Then Testtxt = False Else Testtxt = True End If End Function
Public Function ExecuteSQL(ByVal SQL As String, ByVal table As String) Try '建立ADODataSetCommand对象 '数据库查询函数 Dim constr As String constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" constr = constr & Application.StartupPath() & "\student.mdb" ADOcmd = New OleDbDataAdapter(SQL, constr) '建立ADODataSetCommand对象
ds.Clear() ds.Tables.Clear() '清除数据集中的所有表
ADOcmd.Fill(ds, table) '取得表单
mytable = ds.Tables.Item(0) '取得名为table的表 rownumber = 0 '设置为第一行 myrow = mytable.Rows.Item(rownumber) '取得第一行数据 Catch MsgBox(Err.Description) End Try End Function
End Module
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click dim SearchSQL as string SearchSQL = "SELECT * FROM " & Trim(textbox1.text) '构造查询 Module1.ExecuteSQL(SearchSQL, tablename) '调用查询函数 Dim newrow As DataRow newrow = Module1.mytable.NewRow newrow.Item(0) = Trim(TxtUserName.Text) '给第一个字段赋值 newrow.Item(1) = Trim(TxtUserPassword.Text) '给第二个字段赋值 mytable.Rows.Add(newrow) '添加新用户 Module1.cmd = New OleDbCommandBuilder(Module1.ADOcmd) '使用自动生成的SQL语句 Module1.ADOcmd.Update(Module1.ds, tablename) '对数据库进行更新 MsgBox("添加用户成功!", vbOKOnly + vbExclamation, "信息框") End Sub