gpt4 book ai didi

c# - SQL Server 和 C# 的输出不同

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:04 25 4
gpt4 key购买 nike

我认为我的编码有问题。我猜这不是按日期过滤。请帮忙

SQL 代码:

SELECT
CONVERT(char(80), i.InvDate,3) AS InvDate,
i.InvoiceNo,
i.EmployerCode,
i.TaxAmount + i.SubTotal AS Amount,
'' AS Payment,
pd.GivenName
FROM
dbo.Invoice i
INNER JOIN
dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)

C#代码:

SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), i.InvDate,3) AS InvDate, 
i.InvoiceNo,
i.EmployerCode,
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, '" + dtpFrom.Text + "', 105 ))
AND (InvDate <= CONVERT(datetime, '" + dtpTo.Text + "', 105))," , objConn);

这是我想要实现的:

enter image description here

这是我得到的:

enter image description here

在我被踢出这里之前。我会进行一些修改。

编辑:

日期时间采用 105 格式 (dd-mm-yyyy)

WHERE (InvDate >= CONVERT(datetime, '" + dtpFrom.Text + "', 105 )) 
AND (InvDate <= CONVERT(datetime, '" + dtpTo.Text + "', 105))

这应该是从一个特定的日期中选择的。 EG:从 2012 年 10 月 11 日到 2012 年 10 月 15 日。

即使没有 DateTimepicker,输出也不会显示“GivenName”

最佳答案

最好的方法是将您的输入格式化为 dd/MMM/yyyy 格式。它更好地帮助SqlServer 识别月份和日期。我强烈建议您在与 SqlServer 通信时使用 MMM 格式。

DateTime fromDate, toDate;
fromDate = DateTime.Parse(dtpFrom.Text);
toDate = DateTime.Parse(dtpTo.Text);

SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), i.InvDate,3) AS InvDate,
i.InvoiceNo,
i.EmployerCode,
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 datediff(d, '" + fromDate.ToString("dd/MMM/yyyy") + "', i.InvDate) >=0
AND datediff(d, '" + toDate.ToString("dd/MMM/yyyy") + "', i.InvDate) <=0", objConn);

或者如果您有任何限制要执行上述操作,请使用 CONVERT() 转换输入

SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), i.InvDate,3) AS InvDate, 
i.InvoiceNo,
i.EmployerCode,
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 datediff(d, CONVERT(datetime, '" + dtpFrom.Text + "', 105 ), i.InvDate) >=0
AND datediff(d, CONVERT(datetime, '" + dtpTo.Text + "', 105 ), i.InvDate) <=0", objConn);

关于c# - SQL Server 和 C# 的输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12832294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com