回 帖 发 新 帖 刷新版面

主题:找singleminded,,求助!!!

Dim MyDataSet As New DataSet
        Dim MyODA As OleDb.OleDbDataAdapter
        Dim Conn As String = "provider=Microsoft.jet.oledb.4.0;data source =学籍管理数据库.mdb"
        Dim MyConn As New OleDb.OleDbConnection(Conn)
        MyConn.Open()

        Dim selstr As String = "select * from users"
        Dim addstr As String = "insert into users (帐号,密码) values (" & TextBox1.Text & "," & TextBox2.Text & ")"
        MyODA = New OleDb.OleDbDataAdapter(selstr, MyConn)
        MyODA.InsertCommand.CommandText = addstr
        MyODA.InsertCommand.CommandType = CommandType.Text
        MyODA.InsertCommand.ExecuteNonQuery()

        MyODA.TableMappings.Add("Table", "users")
        MyODA.Fill(MyDataSet, "users")
        DataGrid1.DataSource = MyDataSet.Tables("users")
        MyConn.Close()


这段代码那错了。。。。帮我看看。。我总是添不进去记录

回复列表 (共4个回复)

沙发

Dim MyDataSet As New DataSet
        Dim MyODA As OleDb.OleDbDataAdapter
        Dim commd As New Data.OleDb.OleDbCommand
        Dim Conn As String = "provider=Microsoft.jet.oledb.4.0;data source =学籍管理数据库.mdb"
        Dim MyConn As New OleDb.OleDbConnection(Conn)
        MyConn.Open()
        Dim selstr As String = "select * from users"
        Dim addstr As String = "insert into users (帐号,密码) values (1,3);"
        commd = MyConn.CreateCommand()
        commd.CommandText = selstr
        MyODA = New OleDb.OleDbDataAdapter(commd)
        commd.CommandText = addstr
        commd.CommandType = CommandType.Text
        commd.ExecuteNonQuery()
        MyODA.TableMappings.Add("Table", "users")
        MyODA.Fill(MyDataSet, "users")
        DataGrid1.DataSource = MyDataSet.Tables("users")
        MyConn.Close()

板凳

我试了一下应该可以,结束语句后加";"

3 楼

不过还是有问题。。我把它换成字符串就不行了。。但是在数据库里面的字段属性也是文本属性啊。应该没什么问题。。但是换了就要报错

我现在是这样子的。。

        Dim MyDataSet As New DataSet
        Dim MyODA As OleDb.OleDbDataAdapter
        Dim commd As New Data.OleDb.OleDbCommand
        Dim Conn As String = "provider=Microsoft.jet.oledb.4.0;data source =学籍管理数据库.mdb"
        Dim MyConn As New OleDb.OleDbConnection(Conn)
        MyConn.Open()
        Dim selstr As String = "select * from users"
        Dim STR1 As String = TextBox1.Text
        Dim STR2 As String = TextBox2.Text
        Dim addstr As String = "insert into users (帐号,密码) values (" & STR1 & "," & STR2 & ");"
        commd = MyConn.CreateCommand()
        commd.CommandText = selstr
        MyODA = New OleDb.OleDbDataAdapter(commd)
        
        commd.CommandText = addstr
        commd.CommandType = CommandType.Text
        commd.ExecuteNonQuery()
        MyODA.TableMappings.Add("Table", "users")
        MyODA.Fill(MyDataSet, "users")
        DataGrid1.DataSource = MyDataSet.Tables("users")
        MyConn.Close()



这句话报错   commd.ExecuteNonQuery()

4 楼

Dim addstr As String = "insert into users (帐号,密码) values ('" & STR1 & "','" & STR2 & "');"

我来回复

您尚未登录,请登录后再回复。点此登录或注册