如何使用C#将数据插入MS访问 [英] How to insert Data to MS access using C#

查看:77
本文介绍了如何使用C#将数据插入MS访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ms访问中创建了一个数据库,在数据库中我有一个包含两列的表,第一列是Name with Text数据类型,另一列是Date with Date / time数据类型。我想在表格中插入值,但它会为月历提供此错误



标准表达式中的数据类型不匹配。





I created a database in ms access, in the database I have a table with two columns, fist column is Name with Text data type, and the other column is Date with Date/time data type. and I want to insert values in the table, but it give this error for the month calendar

Data type mismatch in criteria expression.


string sqlQuery = "INSERT INTO register (`Name`,`Date`) values (?,?)";
                using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\DB.accdb"))
                using (OleDbCommand cmd = new OleDbCommand(sqlQuery, conn))
                {

                    conn.Open();
                    cmd.Parameters.AddWithValue("@Name", this.textBox1.Text);
                    cmd.Parameters.AddWithValue("@Date", this.monthCalendar1.Text);
                    cmd.ExecuteNonQuery();
                    }

推荐答案

尝试获取Calendar.SelectedDate [ ^ ] property。



类似于 -

Try getting the Calendar.SelectedDate[^] property.

Something like-
cmd.Parameters.AddWithValue("@Date", this.monthCalendar1.SelectedValue);





希望,它有帮助:)



Hope, it helps :)


Date是MS Access中的保留关键字。所以改变你的查询如下,



INSERT INTO寄存器(名称,[日期])值(?,?)
Hi, Date is the reserved keyword in MS Access. So change you query like the following,

INSERT INTO register (Name,[Date]) values (?,?)


这篇关于如何使用C#将数据插入MS访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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