简体   繁体   English

使用VB.NET写入MS-Access数据库问题

[英]Writing to MS-Access Database Issue using VB.NET

I'm utilizing VB.Net to try and write to a database I created (the machine that needs to be writing to the database is the owner of the database, so there should be no permission issues). 我正在使用VB.Net尝试写入我创建的数据库(需要写入数据库的计算机是数据库的所有者,因此应该没有权限问题)。 I'm getting no errors on when my subs for writing to the database are run, but no new records are created in my database. 我的写入数据库的子程序何时运行没有错误,但是在数据库中没有创建新记录。 I'm successfully connected to the database, as my subs for returning records work perfectly. 我已成功连接到数据库,因为我的用于返回记录的用户可以完美地工作。

The Sub for writing to the database: 用于写入数据库的Sub:

Public Sub LogThisCard(a As Object)
    Dim con As New OleDb.OleDbConnection
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As New OleDb.OleDbCommand
    Dim inMachine As String = "Yes"

    con.ConnectionString = Connection()
    con.Open()

    Try
        sql.Connection = con
        sql.CommandText = "INSERT INTO LogTable (MSA, Foo, Bar, Jack, Rabbit, Date_/_Time, Foxtrot) VALUES (@MSA, @Foo, @Bar, @Jack, @Rabbit, @DT, @Foxtrot)"

        sql.Parameters.AddWithValue("MSA", a.MSA)
        sql.Parameters.AddWithValue("Foo", a.Foo)
        sql.Parameters.AddWithValue("Bar", a.Bar)
        sql.Parameters.AddWithValue("Jack", a.Jack)
        sql.Parameters.AddWithValue("Rabbit", a.Rabbit)
        sql.Parameters.AddWithValue("DT", DateTime.Now())
        sql.Parameters.AddWithValue("Foxtrot", a.FoxTrot)

        da.InsertCommand = sql
        da.InsertCommand.ExecuteNonQuery()
    Catch ex As Exception
        Debug.Print(ex.ToString)
    End Try

    con.Close()
End Sub

The Database Design is as follows: 数据库设计如下:

  • ID: AutoNumber (Primary Key) ID:自动编号(主键)
  • MSA: Number MSA:编号
  • Foo: Number Foo:数字
  • Bar: Short Text 条:短文本
  • Jack: Short Text 杰克:短文字
  • Rabbit: Short Text 兔子:短文字
  • Date / Time:Date/Time 日期/时间:日期/时间
  • Foxtrot: Short Text 狐步舞:短文本

It could be an issue with Access and your DateTime Parameter. Access和DateTime参数可能有问题。 If DateTime allows null values, change your insert to do all except DateTime. 如果DateTime允许为空值,则将您的插入内容更改为DateTime以外的所有内容。 If the Insert works then you know the issue. 如果插入有效,则说明问题所在。

I am not sure why access has issue with addwithvalue, but I see it often. 我不确定为什么accesswithaddwithvalue存在问题,但是我经常看到它。 I have found it better to declare a variable of parameter and set properties then add the param when it comes to date times. 我发现最好声明一个参数变量并设置属性,然后在日期时间添加参数。

Example: 例:

Dim cmd As OleDb.OleDbCommand
Dim param As New OleDb.OleDbParameter
param.Direction = ParameterDirection.Input
param.DbType = DbType.Date
param.Value = Date.Now

cmd.Parameters.Add(param)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM