1

我越来越沮丧了。是的,我是一个新手,在 C# 和 sql server 中使用了 2 周。请对我放轻松。该命令应该打印出 InvDate、InvoiceNo、TaxAmount 和 SubTotal 作为 Amount。它还应该从另一个名为 PatientDetails 的表中拖动 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)
4

1 回答 1

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

于 2012-07-27T09:43:42.213 回答