ACCESS中的"时间/日期"字段中插入DateTime.Now出错

本文介绍了一种常见的编程问题解决方案:当使用C#向Access数据库的时间/日期字段插入数据时,如何避免出现“标准表达式中数据类型不匹配”的错误。通过调整参数设置,确保了日期类型的正确转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

向ACCESS中的"时间/日期"字段中插入DateTime.Now时出现“标准表达式中数据类型不匹配。”错误的解决办法:

在使用下面的代码向Access数据库中添加数据的时候,如果是日期字段,则会出现"标准表达式中数据类型不匹配。"的错误,这可能是C#中的日期类型无法直接转换成Access中的日期类型OleDbType.DBDate所致:

string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MengXianHui.mdb;Persist Security Info=True";  
string QueryString = "Insert Into [Document] (Title, Content, Author, CreateDate) Values(@Title, @Content, @Author, @CreateDate)";  
OleDbConnection cn = new OleDbConnection(ConnectionString);  
cn.Open();  
OleDbCommand cmd = new OleDbCommand(QueryString, cn);  
cmd.Parameters.AddWithValue("@Title", Title);  
cmd.Parameters.AddWithValue("@Content", Content);  
cmd.Parameters.AddWithValue("@Author", Author);  
cmd.Parameters.AddWithValue("@CreateDate", DateTime.Now);  
cmd.ExecuteNonQuery();  
cn.Close();  
cn.Dispose(); 
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MengXianHui.mdb;Persist Security Info=True";
string QueryString = "Insert Into [Document] (Title, Content, Author, CreateDate) Values(@Title, @Content, @Author, @CreateDate)";
OleDbConnection cn = new OleDbConnection(ConnectionString);
cn.Open();
OleDbCommand cmd = new OleDbCommand(QueryString, cn);
cmd.Parameters.AddWithValue("@Title", Title);
cmd.Parameters.AddWithValue("@Content", Content);
cmd.Parameters.AddWithValue("@Author", Author);
cmd.Parameters.AddWithValue("@CreateDate", DateTime.Now);
cmd.ExecuteNonQuery();
cn.Close();
cn.Dispose();

解决办法就是将上面语句中的 cmd.Parameters.AddWithValue("@CreateDate", DateTime.Now); 转换成下面的语句即可:

OleDbParameter parameter = new OleDbParameter();  
parameter.OleDbType = OleDbType.DBDate;  
parameter.Value = DateTime.Now;  
cmd.Parameters.Add(parameter); 

帮我审查下这段代码: public class CustomEDFWriter { private readonly string[] _labels; private readonly string[] _transducerTypes; private readonly string[] _physicalDimensions; private readonly double[] _physicalMinimums; private readonly double[] _physicalMaximums; private readonly short[] _digitalMinimums; private readonly short[] _digitalMaximums; private readonly string[] _prefilterings; private readonly int[] _samplesPerRecordPerSignal; private readonly string _filePath; private readonly int _channelCount; private readonly int[] _samplesPerSecond; private FileStream _fs; private BinaryWriter _bw; private int _dataRecordCount=0; // 初始化EDF文件头 public CustomEDFWriter(string filePath, string patientID, string recordingInfo,int channelCount, int[] samplesPerSecond ,string[] labels, string[] transducerTypes, string[] physicalDimensions, double[] physicalMinimums, double[] physicalMaximums, short[] digitalMinimums, short[] digitalMaximums, string[] prefilterings, int[] samplesPerRecordPerSignal) { _filePath = filePath; _channelCount = channelCount; _samplesPerSecond = samplesPerSecond; _labels = labels; _transducerTypes = transducerTypes; _physicalDimensions = physicalDimensions; _physicalMinimums = physicalMinimums; _physicalMaximums = physicalMaximums; _digitalMinimums = digitalMinimums; _digitalMaximums = digitalMaximums; _prefilterings = prefilterings; _samplesPerRecordPerSignal = samplesPerRecordPerSignal; // 创建文件并写入固定头 _fs = new FileStream(filePath, FileMode.Create, FileAccess.Write); _bw = new BinaryWriter(_fs); WriteHeader(patientID, recordingInfo); } private void WriteHeader(string patientID, string recordingInfo) { // 头部第一部分(256字节) var header = new StringBuilder(); header.Append("0 "); // 8位版本号 header.Append(patientID.PadRight(80)); //本地患者标识 header.Append(recordingInfo.PadRight(80)); //本地记录标识 header.Append(DateTime.Now.ToString("dd.MM.yy").PadRight(8));//记录开始日期 header.Append(DateTime.Now.ToString("HH.mm.ss").PadRight(8));//录制开始时间 header.Append(((_channelCount+1)*256).ToString().PadRight(8)); // 头字节数 header.Append(" ".PadRight(44)); // 保留字段 header.Append("0".PadRight(8)); // 文件中数据记录块数nr 后面写数据会更新 header.Append("1".PadRight(8)); // 一个数据记录块的记录时间(秒) header.Append(_channelCount.ToString().PadRight(4)); // 通道数 // 填充剩余空间 //header.Append(new string(' ', 256 - header.Length)); _bw.Write(Encoding.ASCII.GetBytes(header.ToString())); // 头部第二部分(每通道256字节) for (int i = 0; i < _channelCount; i++) { var chHeader = new StringBuilder(); chHeader.Append(_labels[i].PadRight(16)); // 通道标签 chHeader.Append(_transducerTypes[i].PadRight(80)); // 传感器类 chHeader.Append(_physicalDimensions[i].PadRight(8)); // 物理单位 chHeader.Append(_physicalMinimums[i].ToString().PadRight(8)); // 物理最小值 chHeader.Append(_physicalMaximums[i].ToString().PadRight(8)); // 物理最大值 chHeader.Append(_digitalMinimums[i].ToString().PadRight(8)); // 数字最小值 chHeader.Append(_digitalMaximums[i].ToString().PadRight(8)); // 数字最大值 chHeader.Append(_prefilterings[i].PadRight(80)); // 滤波信息 chHeader.Append(_samplesPerRecordPerSignal[i].ToString().PadRight(8)); // 采样率 chHeader.Append(" ".PadRight(32)); _bw.Write(Encoding.ASCII.GetBytes(chHeader.ToString())); } } // 追加1秒数据 public void AppendData(short[][] channelData) { if (channelData.Length != _channelCount) throw new ArgumentException("Channel count mismatch"); lock (_fileLock) { // 切换到追加模式 _bw.Close(); _fs = new FileStream(_filePath, FileMode.Append, FileAccess.Write); _bw = new BinaryWriter(_fs); // 写入数据(按通道交错存储) for (int i = 0; i < channelData[0].Length; i++) { for (int ch = 0; ch < _channelCount; ch++) { _bw.Write(channelData[ch][i]); } } // 更新记录计数 Interlocked.Increment(ref _dataRecordCount);
最新发布
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值