我很沮丧。是的,我是一个新手,2周大的C#和sql server。请放轻松我。该命令应该打印出InvDate,InvoiceNo,TaxAmount和SubTotal作为金额。它还应该从另一个名为PatientsDetails的表中拖动Patientdetails。两者中的关键是MedicalRecordID。
SqlCommand objCmd =
new SqlCommand("SELECT CONVERT(char(80), inv.[InvDate],3)
AS InvDate,inv.[InvoiceNo],inv.[TaxAmount] + inv.[SubTotal]
AS Amount,
'' AS Payment
FROM [Invoice] inv
LEFT JOIN [PatientDetails] tab
ON inv.[MedicalRecordID] = tab.[MedicalRecordID]
WHERE (inv.[InvDate] >= CONVERT(datetime, '" +
dtpFrom.Text + "', 105 ))
AND (inv.[InvDate] <= CONVERT(datetime, '" +
dtpTo.Text + "', 105))",
objConn);
下一组命令是按照我希望的那样打印出来。
for (int i = 0; i < 4; i++)
{
if (!objReader.IsDBNull(i))
{
string s;
s = objReader.GetDataTypeName(i);
//MessageBox.Show(s);
if (objReader.GetDataTypeName(i) == "char")
{
sw.Write(objReader.GetString(i));
}
else if (objReader.GetDataTypeName(i) == "money")
{
sw.Write(objReader.GetSqlMoney(i).ToString());
}
else if (objReader.GetDataTypeName(i) == "nvarchar")
{
sw.Write(objReader.GetString(i));
}
}
if (i < 3)
{
sw.Write("\t");
}
}
count = count + 1;
sw.WriteLine();
这是我的SQL查询,我猜这显然是错误的,因为我没有添加SubTotal,但这实际上是我想要的。 InvoiceNo,InvDate,SubTotal标记给用户。
SELECT Invoice.InvDate,
Invoice.InvoiceNo,
Invoice.TaxAmount + Invoice.SubTotal,
PatientDetails.GivenName
FROM Invoice
INNER JOIN PatientDetails
ON (Invoice.MedicalRecordID = PatientDetails.MedicalRecordID)
答案 0 :(得分:1)
尝试以下内容写出结果
for (int i = 0; i < 4; i++)
{
if (!objReader.IsDBNull(i))
sw.Write(objReader.GetValue(i).ToString());
else
sw.Write("NULL");
if (i < 3)
sw.Write("\t");
}
count += 1;
sw.WriteLine();
如果您仍然缺少字段,我会按如下方式编写您的查询
SqlCommand cmd = objConn.CreateCommand();
cmd.CommandText = @"SELECT inv.[InvDate]
,inv.[InvoiceNo]
,inv.[TaxAmount] + inv.[SubTotal] AS Amount
,tab.GivenName
FROM [Invoice] inv
LEFT JOIN [PatientDetails] tab
ON inv.[MedicalRecordID] = tab.[MedicalRecordID]
WHERE inv.[InvDate] BETWEEN @From AND @To";
cmd.Parameters.AddWithValue("@From", fromDate);
cmd.Parameters.AddWithValue("@To", toDate);
您也可以使用WHERE inv.[InvDate] >= @From AND inv.[InvDate] <= @To
代替BETWEEN