读书人

C#字符串转datatime出错代码如上如

发布时间: 2012-12-23 11:28:15 作者: rapoo

C#字符串转datatime出错,代码如下,如果不传参,直接把时间写到方法里就没错误,为什么呢?

static void Main(string[] args) 
{
int j = sqlQueryTable("Data Source=ZHANGL;Initial Catalog=db_GVM;Integrated Security=True", "2012/11/4 12:41:16", "2012/11/4 12:41:56"); Console.WriteLine(j);
Console.ReadKey();
}
public static int sqlQueryTable(string specConnection, String startdatetime, String enddatetime)
{
int i = 0;
try { using (SqlConnection conn = new SqlConnection(specConnection)) { string strCmd = "select count(*) from tb_PDU where datetime >= 'startdatetime' and datetime <= 'enddatetime' "; conn.Open(); SqlCommand sqlCmd = new SqlCommand(strCmd, conn); i = Convert.ToInt32(sqlCmd.ExecuteScalar()); conn.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message + ex.Source); } return i; }

[解决办法]
string strCmd = "select count(*) from tb_PDU where datetime >= 'startdatetime' and datetime <= 'enddatetime' ";
C#里能这样解析吗?
string strCmd = "select count(*) from tb_PDU where datetime >= '"+startdatetime+"' and datetime <= '"+enddatetime+"' ";

读书人网 >C#

热点推荐