你好试图插入Access数据库我得到错误数据类型不匹配表达Creteria ....这是我的代码和数据库的打印屏幕.. [英] Hello Been Trying To Insert Into Access Database I Get Error Data Type Mismatch In Expression Creteria....Here Is My Code And Print Screen Of The Database..

查看:42
本文介绍了你好试图插入Access数据库我得到错误数据类型不匹配表达Creteria ....这是我的代码和数据库的打印屏幕..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

If Not OleDbConn.State = ConnectionState.Open Then
          OleDbConn.Open()



          AccQry = "INSERT INTO [Projects]([Project Name], [Owner], [Category], [Priority], [Status], [Start Date], [End Date], [Notes])" & _
                          "VALUES (@ProjectName , @Owner , @Category , @Priority , @Status , (?) , (?) , @Notes)"
          ''   AccCmd.Connection.Open()
          '' AccCmd.Parameters.AddWithValue("ID", Me.txtID.Text)
          AccCmd.Parameters.AddWithValue("@ProjectName", Me.txtName.Text)
          AccCmd.Parameters.AddWithValue("@Owner", Me.lblOwner.SelectedItem.Text)
          AccCmd.Parameters.AddWithValue("@Category", Me.ddlCat.SelectedValue)
          AccCmd.Parameters.AddWithValue("@Priority", Me.ddlPriority.SelectedValue)
          AccCmd.Parameters.AddWithValue("@Status", Me.ddlStatus.SelectedValue)
          AccCmd.Parameters.AddWithValue("", DateTime.Now.ToOADate()).Value = Me.sDate.Text
          AccCmd.Parameters.AddWithValue("", DateTime.Now.ToOADate()).Value = Me.eDate.Text
          AccCmd.Parameters.AddWithValue("@Notes", Me.txtNote.Text)

   AccCmd.CommandText = AccQry
          AccCmd.CommandType = CommandType.Text
          AccCmd.Connection = OleDbConn
          AccCmd.ExecuteNonQuery()
          OleDbConn.Close()
          RefreshData()

推荐答案

我的话,但对你真正粗鲁的冲动很强烈!但是......我不会。



这是一个很好的例子,把它放在一起并希望代码,它不会起作用。

My word, but the urge to be really rude to you is strong! But...I won't.

This is a prime example, of "chuck it together and hope" code and it won't work.
AccCmd.Parameters.AddWithValue("", DateTime.Now.ToOADate()).Value = Me.sDate.Text
AccCmd.Parameters.AddWithValue("", DateTime.Now.ToOADate()).Value = Me.eDate.Text

您认为AddWithValue有什么作用?你为什么要给它一个DateTime值,然后用字符串覆盖它?

如果用户输入42nd ZoomTang 8851作为日期怎么办?

为什么你突然从命名转换为匿名参数?



可能是你在互联网上发现了一些随机代码并没有'想想它做了什么,只是把它扔进你的代码中并将其破解,直到编译错误消失为止?



请尝试考虑你的作业 - 和编码思考的结果,不仅仅是希望我们能为你排序!

What do you think AddWithValue does? Why would you want to give it a DateTime value, and then override it with a string afterwards?
What happens if the user enters "42nd ZoomTang 8851" as the date?
Why do you suddenly switch from named to anonymous parameters?

Could it be that you found some random code on the internet and didn't think about what it did, just threw it into your code and hacked it about until the compilation errors went away?

Please, try thinking about your homework - and coding as a result of thinking, not just hoping we will sort it out for you!


If Not OleDbConn.State = ConnectionState.Open Then
          OleDbConn.Open()
End If
 
AccQry = "INSERT INTO [Projects]([Project Name], [Owner], [Category], [Priority], [Status], [Start Date], [End Date], [Notes])" & _
         "VALUES (@ProjectName , @Owner , @Category , @Priority , @Status , @StartDate , @EndDate , @Notes)"
          '' AccCmd.Connection.Open()
          '' AccCmd.Parameters.AddWithValue("ID", Me.txtID.Text)
          AccCmd.Parameters.AddWithValue("@ProjectName", Me.txtName.Text)
          AccCmd.Parameters.AddWithValue("@Owner", Me.lblOwner.SelectedItem.Text)
          AccCmd.Parameters.AddWithValue("@Category", Me.ddlCat.SelectedValue)
          AccCmd.Parameters.AddWithValue("@Priority", Me.ddlPriority.SelectedValue)
          AccCmd.Parameters.AddWithValue("@Status", Me.ddlStatus.SelectedValue)
          AccCmd.Parameters.AddWithValue("@StartDate", DateTime.Parse(Me.sDate.Text))
          AccCmd.Parameters.AddWithValue("@EndDate", DateTime.Parse(Me.eDate.Text))
          AccCmd.Parameters.AddWithValue("@Notes", Me.txtNote.Text)
 
   AccCmd.CommandText = AccQry
          AccCmd.CommandType = CommandType.Text
          AccCmd.Connection = OleDbConn
          AccCmd.ExecuteNonQuery()
          OleDbConn.Close()
          RefreshData()





备注:

- 在VB中,如果条件必须以<$ c终止$ c>结束如果声明。

- 我希望您在DB中的字段[开始日期]和[结束日期]是日期时间类型

- 为什么不使用DateTimePicker而不是TextBox来处理DateTime值?



Remarks :
- In VB, an If condition must be terminated by an End If statement.
- I hope your fields [Start Date] and [End Date] in DB are of datetime type
- Why not using a DateTimePicker instead of a TextBox for DateTime values handling?


这篇关于你好试图插入Access数据库我得到错误数据类型不匹配表达Creteria ....这是我的代码和数据库的打印屏幕..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆