CannotinsertDateTimeinAccessusingOleDb在Access中插入一行数据时,出现“条件表达式中的数据类型不匹配”错误。稍微弄乱之后,我将范围缩小到DateTime是问题所在。这是我的代码:classABGDA{privateOleDbConnectiondbConn;私有OleDbCommanddbCmd;私有OleDbDataReaderdbReader;私有字符串sConnection;私有字符串sql;私人ABGabg;publicvoidinsertProgressNotes(ABGABG){abg=ABG;sConnection="Provider=Microsoft.ACE.OLEDB.12.0;"+"数据源=SimEMR.accdb";dbConn=newOleDbConnection(sConnection);dbConn.Open();sql="INSERTINTOABG(AccountNo,LabDate,PAO2,PACO2,SAO2,Bicarbonate,BaseExcess,"+"O2Setting,SetRate,SetPEEP,FiO2)VALUES(?,?,?,?,?,?,?,?,?,?,?);”;dbCmd=newOleDbCommand();dbCmd.CommandText=sql;dbCmd.Connection=dbConn;dbCmd.Parameters.Add("AccountNo",OleDbType.Integer).Value=abg.AccountNo;dbCmd.Parameters.Add("LabDate",OleDbType.DBTimeStamp).Value=abg.LabDate;dbCmd.Parameters.Add("PAO2",OleDbType.Double).Value=abg.PAO2;dbCmd.Parameters.Add("PACO2",OleDbType.Double).Value=abg.PACO2;dbCmd.Parameters.Add("SAO2",OleDbType.Double).Value=abg.SAO2;dbCmd.Parameters.Add("碳酸氢盐",OleDbType.Double).Value=abg.Bicarbonate;dbCmd.Parameters.Add("BaseExcess",OleDbType.Double).Value=abg.BaseExcess;数据库命令。Parameters.Add("O2Setting",OleDbType.Char).Value=abg.O2Setting;dbCmd.Parameters.Add("SetRate",OleDbType.Double).Value=abg.SetRate;dbCmd.Parameters.Add("SetPEEP",OleDbType.Double).Value=abg.SetPeep;dbCmd.Parameters.Add("FiO2",OleDbType.Double).Value=abg.FiO2;dbCmd.ExecuteNonQuery();dbConn.Close();abg.LabDate是我使用DateTime得到的奇怪的东西。现在是我在另一个类中使用DBTimeStamp来插入语句,它似乎工作正常。有谁知道我的问题可能是什么?更新:看来我找到了解决方案,我不知道它为什么有效。我将abg.LabDate更改为字符串并保存当前日期/时间。abg.LabDate=DateTime.Now.ToString();然后当我将它插入数据库时??,我将它解析回DateTime并且它可以工作......我认为错误是由于DateTime中存在的毫秒部分不会被Access处理,因此您可以截断毫秒部分并尝试插入,或者如果它只是DateTime.Now则使用Now等价物()函数访问。insertintotable1(datecolumn)values(Now())//Date()如果对时间部分不感兴趣我知道这个问题很老了。但是,我只是在使用Access和MSSQL数据库时才涉及到这一点。Access中的字段是Date/Time类型,在MSSQL中是DateTime类型。我的解决方案是使用OleDbType.Date。以上是C#学习教程:无法使用OleDb将DateTime插入到Access共享的所有内容中。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——dbCmd。Parameters.Add("LabDate",OleDbType.Date).Value=DateTime.Now;OleDbConnectioncon=newOleDbConnection();con.ConnectionString="provider=microsoft.ace.oledb.12.0;datasource=E:\Sohkidatabase\Sohki.accdb";OleDbCommandcmd=newOleDbCommand();cmd.CommandType=CommandType.Text;cmd.CommandText=@"INSERTINTOChallan_No(challan,goods,quantity,nwlm,rate,total,ident,taaff,dateissue,nature,factory,expected,palce,date)VALUES("+labelchallan.Text+",'"+textGood.Text+"',"+combQuit.Text+","+combNwlm.Text+","+textRate.Text+","+textvalu.Text+",'"+textIdent.Text+"','"+texttfclass.Text+"','"+dateTimeIssue.Text+"','"+textNatup.Text+"','"+textFact.Text+"','"+textExpDu.Text+"','"+textPlace.Text+"','"+dateTimeDate.Text+"')";cmd.Connection=con;con.Open();cmd.ExecuteNonQuery();System.Windows.Forms.MessageBox.Show("记录创建成功");con.Close();本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
