问题描述
我一整天都被这个问题困扰,所以也许有人给我提示。
环境:MS Access DB、VB.NET、OleDb-Driver、MS Access Database Engine 2016。
我试图将 Long/Int64 字段绑定到 Numericupdown 控件,但在更新时我总是得到 OleDbException“参数太少”。我很确定长字段是问题所在,因为当我从表中删除它(并注释掉它的代码)时,一切正常。我不确定 Numericupdown 是否适合这项任务,但它的基础数据类型是十进制,十进制有更多的空间作为长期需要。所以这里有一些代码:
在数据层中:
Private Sub InitializeDataSet()
Dim LocFields = String.Join("],[",Fields)
Dim sql As String = "SELECT [" & LocFields & "] FROM [" & Table & "]"
damain = New OleDbDataAdapter(sql,Connection)
Using Builder As New OleDbCommandBuilder(damain) With {
.ConflictOption = ConflictOption.OverwriteChanges
}
Builder.GetInsertCommand()
Builder.GetUpdateCommand()
Builder.GetDeleteCommand()
End Using
DS = New DataSet
damain.Fill(DS,Table)
End Sub
Public Sub Update()
damain.Update(DS,Table) ' <-- Here the exception happens
End Sub
Public ReadOnly Property DataSource As Object
Get
Return DS
End Get
End Property
Public ReadOnly Property DataMember As String
Get
Return Table
End Get
End Property
我们在一个班级,所以变量是:
table = 表名
Fields = 表中字段的列表
连接 = OleDbConnection
damain = OleDbDataAdapter
DS = 数据集
在表单中:
Private DL As OleDbDataLayer
Private WithEvents BSource As New BindingSource
Public Sub New(DataLayer As OleDbDataLayer)
InitializeComponent()
DL = DataLayer
BSource.DataSource = DL.DataSource
BSource.DataMember = DL.DataMember
BSource.AllowNew = True
BSource.sort = DL.OrderBy
BSource.Position = 0
Initializefields()
End Sub
Private Sub DataUpdate()
BSource.EndEdit()
DL.Update()
End Sub
Private Sub Initializefields()
NUD.Minimum = Long.MinValue
NUD.Maximum = Long.MaxValue
Dim Binding As New Binding("Value",BSource,"F_Long")
AddHandler Binding.Format,AddressOf Formatdbnull
AddHandler Binding.Parse,AddressOf ParseNumericupdown
NUD.DataBindings.Add(Binding)
End Sub
Private Sub Formatdbnull(sender As Object,e As ConvertEventArgs)
If Convert.Isdbnull(e.Value) Then
Select Case e.DesiredType
Case = GetType(Decimal) : e.Value = 0
Case = GetType(Date) : e.Value = Today
Case = GetType(Boolean) : e.Value = False
Case Else
End Select
End If
End Sub
Private Sub ParseNumericupdown(sender As Object,e As ConvertEventArgs)
Select Case e.DesiredType
Case = GetType(Byte)
e.Value = Convert.ToByte(e.Value)
Case = GetType(Short)
e.Value = Convert.ToInt16(e.Value)
Case = GetType(Integer)
e.Value = Convert.ToInt32(e.Value)
Case = GetType(Long)
e.Value = Convert.ToInt64(e.Value)
Case Else
' Do nothing
End Select
End Sub
这里的 NUD 是 Numericupdown 控件,应该很明显。
也许我应该使用另一种控件类型?文本框?蒙面文本框?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)