首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

新人 ,vb.nel连access数据库,调用函数失败

2013-11-06 
新人求助 ,vb.nel连access数据库,调用函数失败跪求大神帮我看看我用vb.net2005和access数据库(数据库文件

新人求助 ,vb.nel连access数据库,调用函数失败
跪求大神帮我看看新人 ,vb.nel连access数据库,调用函数失败
我用vb.net2005和access数据库(数据库文件类型为accdb)做电话簿程序。调试时,函数的调用无法成功 ,错误是:
未处理 System.Data.OleDb.OleDbException
  ErrorCode=-2147467259
  Message="查询值的数目与目标字段中的数目不同。"
  Source="Microsoft Access Database Engine"
  StackTrace:
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       在 System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
       在 电话簿.DBOperation.DBOperate(String SQLString) 位置 C:\Documents and Settings\zyy\桌面\电话簿-张莹莹\电话簿啦啦\电话簿\函数\DBOperation.vb:行号 20
       在 电话簿.DBuser.AddUser(DBuser User) 位置 C:\Documents and Settings\zyy\桌面\电话簿-张莹莹\电话簿啦啦\电话簿\函数\DBuser.vb:行号 91
       在 电话簿.添加.Button1_Click(Object sender, EventArgs e) 位置 C:\Documents and Settings\zyy\桌面\电话簿-张莹莹\电话簿啦啦\电话簿\添加.vb:行号 26
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(ApplicationContext context)
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       在 电话簿.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
请会的人帮我看看。


接下来是我自己的代码。
下面是要调用的函数。
Public Class DBOperation
    '定义一个数据库操作类函数,其传入的参数是查询字符串,返回值的类型是内存中的数据表DataTable
    Shared Function DBOperate(ByVal SQLString As String) As DataTable
        Dim CONN As String
        CONN = "provider=microsoft.ace.oledb.12.0;Password="""";User ID=Admin;Data Source=|datadirectory|\Database4.accdb"


        Dim oleconn As New OleDb.OleDbConnection(CONN)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(SQLString, oleconn)
        cmd.CommandType = CommandType.Text
        Dim sTokens() As String
        sTokens = SQLString.Split(" ")

        
        Dim ObjectdsDataSet As New DataSet()
        Dim adapter As New OleDb.OleDbDataAdapter()
        adapter.TableMappings.Add("Table", "TEMP")
        adapter.SelectCommand = cmd
        If oleconn.State <> ConnectionState.Open Then
            oleconn.Open()  '打开数据库连接
        End If
        cmd.ExecuteNonQuery()  '执行SQL语句
        If oleconn.State <> ConnectionState.Closed Then
            oleconn.Close() '关闭数据库连接
        End If
        adapter.Fill(ObjectdsDataSet) '填充数据集
        Return ObjectdsDataSet.Tables("TEMP")

    End Function



End Class
接下来是函数的调用部分:
Imports System.Data.OleDb 
Public Class 编辑输入窗体
    Private Sub FrmBianji_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.Items.Clear()
        Dim SQLString As String
        SQLString = "SELECT * FROM telephonebook"
        Dim BianjiTable As DataTable = DBOperation.DBOperate(SQLString)
        Dim BianjiRow As DataRow
        For Each BianjiRow In BianjiTable.Rows
            Me.ListBox1.Items.Add(BianjiRow("姓名"))
        Next
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        '获取用户名的值,它是我们查询的条件
        Dim SQLString As String
        SQLString = "SELECT * FROM telephonebook WHERE 姓名 ='" & Me.ListBox1.SelectedItem & "'"
        Dim BianjiTable1 As DataTable = DBOperation.DBOperate(SQLString)
        '将表中的数据导入到相应的文本框中
        Me.nm.Text = BianjiTable1.Rows(0).Item("姓名")
        Me.no1.Text = BianjiTable1.Rows(0).Item("手机号码1")
        Me.no2.Text = BianjiTable1.Rows(0).Item("手机号码2")
        Me.nohm.Text = BianjiTable1.Rows(0).Item("住宅电话")
        Me.addr.Text = BianjiTable1.Rows(0).Item("地址")
        Me.qq.Text = BianjiTable1.Rows(0).Item("qq")
        Me.email.Text = BianjiTable1.Rows(0).Item("邮箱")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '首先的判断真实姓名和密码不能为空
        If Trim(Me.nm.Text) = "" Then
            MsgBox("姓名不能为空,请输入联系人姓名!", MsgBoxStyle.Exclamation, "消息框")
            Exit Sub
        End If
       
        '先调用删除方法删除该用户的信息
        User.DelUser(Me.ListBox1.SelectedItem)
        '我们在重新添加用户的信息
        Dim tempTable As New DBuser(Trim(Me.ListBox1.SelectedItem)) 'me.nm.text)
        ' 将我们在界面输入的信息填充到我们临时的表中
        tempTable.nm = Trim(Me.nm.Text)


        tempTable.no1 = Trim(Me.no1.Text)
        tempTable.no2 = Trim(Me.no2.Text)
        tempTable.nohm = Trim(Me.nohm.Text)
        tempTable.addr = Trim(Me.addr.Text)
        tempTable.qq = Trim(Me.qq.Text)

        tempTable.email = Trim(Me.email.Text)
        '这样tempTable中的信息就完善啦!下一步进行实例传入
        User.AddUser(tempTable)
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class
之前我还定义了一些变量:
Public Class DBuser
    Private _nm As String
    Private _no1 As String
    Private _no2 As String
    Private _nohm As String
    Private _addr As String
    Private _qq As String
    Private _email As String


    Property nm() As String
        Get
            Return _nm
        End Get
        Set(ByVal value As String)
            _nm = value
        End Set
    End Property
    Property no1() As String
        Get
            Return _no1
        End Get
        Set(ByVal value As String)
            _no1 = value
        End Set
    End Property

    Property no2() As String
        Get
            Return _no2
        End Get
        Set(ByVal value As String)
            _no2 = value
        End Set
    End Property
    Property nohm() As String
        Get
            Return _nohm
        End Get
        Set(ByVal value As String)
            _nohm = value
        End Set
    End Property
    Property addr() As String
        Get
            Return _addr
        End Get
        Set(ByVal value As String)
            _addr = value
        End Set
    End Property
    Property qq() As String
        Get
            Return _qq
        End Get
        Set(ByVal value As String)
            _qq = value
        End Set
    End Property
    Property email() As String
        Get
            Return _email
        End Get
        Set(ByVal value As String)
            _email = value
        End Set
    End Property
    '写一个构造函数,将传进来的参数赋给_nm和来存储起来,方便以后的调用!
    Sub New(ByVal Name As String)
        _nm = Name
还有一个定义:


Module PublicVar
    Public User As DBuser
    Public Frmobj As New 欢迎使用()
    Sub main()
        Application.Run(Frmobj)
    End Sub

End Module


    End Sub
接下来是添加时的实例调用部分:
 '写一个添加用户信息的方法,它的返回值是布尔型,
    Shared Sub AddUser(ByVal User As DBuser)
        Dim sqlstring As String
        sqlstring = "INSERT INTO telephonebook VALUES('" & User.nm & "','" & User.no1 & "','" & User.no2 & "','" & User.nohm & "','" & User.addr & "','" & User.qq & "','" & User.email & "')"
        DBOperation.DBOperate(sqlstring)
        MsgBox("成功添加联系人信息", MsgBoxStyle.Exclamation, "消息框")


    End Sub vb.net access 函数调用,
[解决办法]
 Shared Sub AddUser(ByVal User As DBuser)
        Dim sqlstring As String
        sqlstring = "INSERT INTO telephonebook VALUES('" & User.nm & "','" & User.no1 & "','" & User.no2 & "','" & User.nohm & "','" & User.addr & "','" & User.qq & "','" & User.email & "')"

这里设置断点,看看sqlstring是什么,把sqlstring结果复制到access里的SQL命令工具中执行一下试试看。
[解决办法]
INSERT INTO telephonebook  ...这个语句少了字段吧,insert into 表名(字段列表) values (值列表) 
如INSERT INTO Products (ProductName, SupplierID, CategoryID) Values ('"&inProductName&"', '"&inSupplierID&"','"&inCategoryID&"')

热点排行