使用带有micrososft server2007的C#在ACCESS中查询日期 [英] Date inquiry in ACCESS using C# with micrososft server2007

查看:88
本文介绍了使用带有micrososft server2007的C#在ACCESS中查询日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我用c sharp编写了一个程序,这个程序包含数据库访问权限,当我使用查询时由于日期我使用了下一个表达式:





当我在windows7上测试程序时,这个短语可以正常工作

但它不适用于Microsoft Windows Server 2007和Access2010




我需要帮助,请...



我是什么尝试过:



Hello friends,
I wrote a program using c sharp, this program contains database access, when I use query due to date I used the next Expression:


this phrase works correctly when I Test the program on windows7
but it doesn't work with Microsoft Windows server 2007 and Access2010


I need help, please...

What I have tried:

AccessdataAdapter = new OleDbDataAdapter("SElect *from " + TableName + " where date= #" + Condition+"#" , AccessConnection);

推荐答案

对于初学者,不要这样做:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



您还可以自行开放日期格式问题:您尝试匹配的日期是什么格式?请记住,当您将基于字符串的日期传递给数据库时,它必须使用它认为适合的任何转换将其转换为DATE或DATETIME值 - 这可能与您预期的日期不同。



假设您的日期列(并且这不是一个好名字,DATE是保留字,所以它以后可能会出问题)存储为DATE或DATETIME:如果它存储为字符串,那么它必须完全匹配,包括任何空格,不同的分隔符等。



因此,使用调试器或将数据记录到文件中,查看要与之比较的值,并手动检查数据库以查看它们不匹配的原因。
并确保在那里代码中的任何位置都没有字符串连接!
For starters, don't do it like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

You also leave yourself open to date format problems: what format is the date you are trying to match? Remember that when you pass a string based date to the DB, it has to convert it into a DATE or DATETIME value using whatever conversion it thinks fits - this may not be the same as the date you intended.

That's assuming your date column (and that's not a good name, DATE is a reserved word, so it may give problems later) is stored as a DATE or DATETIME: if it's stored as a string then it has to match exactly, including any whitespace, different separators, etc.

So use the debugger or log the data to a file, look at the values you are trying to compare against, and check the DB manually to see why they do not match.
And make sure that there are no string concatenations anywhere in your code!


这篇关于使用带有micrososft server2007的C#在ACCESS中查询日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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