日期:2014-05-17  浏览次数:20808 次

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+"' ";