我在 SQL Server 2008 中有一个查询。
SELECT
CONVERT(char(80), i.InvDate,3) AS InvDate,
i.InvoiceNo,
i.TaxAmount + i.SubTotal AS Amount,
'' AS Payment,
pd.GivenName
FROM
dbo.Invoice i
INNER JOIN dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)
WHERE (InvDate >= CONVERT(datetime, '16/10/2012', 105)) AND
(InvDate <= CONVERT(datetime, '16/10/2012', 105))
输出:
当我尝试将其放入 C# 时,它显示“从字符串转换日期和/或时间时转换失败。”
信息:16/10/2012 是我试图插入查询以确保它运行的示例日期。这是一个旧程序,这就是它从字符串转换的原因。我很确定它与日期时间格式有关。看看http://www.sqlusa.com/bestpractices/datetimeconversion/。但我不能把手指穿过它。
SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), i.InvDate,3)
AS InvDate,
i.InvoiceNo, i.TaxAmount + i.SubTotal AS Amount,'' AS Payment,pd.GivenName
FROM
dbo.Invoice i INNER JOIN dbo.PatientDetails pd
ON
(pd.MedicalRecordID = i.MedicalRecordID)
WHERE
(InvDate >= CONVERT(datetime, 'dd/MM/yyyy', 105))
AND
(InvDate <= CONVERT(datetime, 'dd/MM/yyyy', 105))", objConn);
SqlDataReader objReader;
objReader = objCmd.ExecuteReader();
System.IO.FileStream fs = new System.IO.FileStream("C:\\CMSExportedData\\Sales-" +
DateTime.Now.ToString("dd-MM-yyyy") + ".txt", System.IO.FileMode.Create);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs,
System.Text.Encoding.Default);