Crystal 报告试图从 2 个表中 select

Crystal Reports trying to select from 2 tables

我想要 select 2 table 秒的数据。这是 2 tables

CREATE TABLE [dbo].[Invoice] (
[InvoiceID]    INT          IDENTITY (1, 1) NOT NULL,
[CustomerName] VARCHAR (50) NULL,
[Telephone]    CHAR (10)    NULL,
[Date]         VARCHAR (30) NULL,
[Total]        FLOAT (53)   NULL,
[Discount]     FLOAT (53)   NULL,
[ToPay]        FLOAT (53)   NULL,
CONSTRAINT [Invoice_PK1] PRIMARY KEY CLUSTERED ([InvoiceID] ASC)

CREATE TABLE [dbo].[Orders] (
[InvoiceID] INT          NOT NULL,
[ItemNO]    INT          NOT NULL,
[Category]  VARCHAR (50) NULL,
[ItemName]  VARCHAR (50) NULL,
[Price]     FLOAT (53)   NULL,
[Qty]       INT          NOT NULL,
[SubTotal]  FLOAT (53)   NULL,
CONSTRAINT [Orders_FK1] FOREIGN KEY ([InvoiceID]) REFERENCES [dbo].[Invoice] ([InvoiceID])

所以我想通过发票 ID select 来自这 2 table 的数据。这是我的代码

SqlConnection conect = new SqlConnection("Data Source=DESKTOP-R34C6VV\SQL;Initial Catalog=Restaurant;Integrated Security=True");


            try
            {
                String str = "Data Source=DESKTOP-R34C6VV\SQL;Initial Catalog=Restaurant;Integrated Security=True";

                String query = "select * from Invoice where InvoiceID = '" + value + "'";


                SqlConnection con = null;
                con = new SqlConnection(str);

                SqlCommand cmd = new SqlCommand(query, con);
                SqlDataReader view;

                con.Open();

                view = cmd.ExecuteReader();


                if (view.HasRows)
                {
                    if (view.Read())
                    {

                        Cus_Name = view.GetString(1);
                        Cus_Tel = view.GetString(2);
                        Date = view.GetString(3);
                        Total = view.GetDouble(4);
                        Discount = view.GetDouble(5);
                        ToPay = view.GetDouble(6);

                        PrepareBill_txt1.Clear();
                        go = 1;
if (go == 1)
        {

            Print open = new Print();

            Crystal_Bill cr = new Crystal_Bill();

            TextObject var1 = (TextObject)cr.ReportDefinition.Sections["Section1"].ReportObjects["Text17"];
            TextObject var3 = (TextObject)cr.ReportDefinition.Sections["Section1"].ReportObjects["Text19"];
            TextObject var4 = (TextObject)cr.ReportDefinition.Sections["Section1"].ReportObjects["Text20"];
            TextObject var6 = (TextObject)cr.ReportDefinition.Sections["Section1"].ReportObjects["Text22"];
            TextObject var7 = (TextObject)cr.ReportDefinition.Sections["Section4"].ReportObjects["Text23"];
            TextObject var8 = (TextObject)cr.ReportDefinition.Sections["Section4"].ReportObjects["Text24"];
            TextObject var9 = (TextObject)cr.ReportDefinition.Sections["Section4"].ReportObjects["Text25"];

            var1.Text = value;
            var3.Text = Date;
            var4.Text = Cus_Name;
            var6.Text = Cus_Tel;
            var7.Text = Total.ToString("0.00");
            var8.Text = Discount.ToString("0.00");
            var9.Text = ToPay.ToString("0.00");


            int a = System.Convert.ToInt32(value);
            cr.SetParameterValue("pInvoiceID", a);

            open.crystalReportViewer1.ReportSource = cr;

                    open.Show();

并且我为另一个 table 到 select 创建了一个方法。这是我对该方法的代码:

 Crystal_Bill cr = new Crystal_Bill();
        SqlConnection conect = new SqlConnection("Data Source=DESKTOP-R34C6VV\SQL;Initial Catalog=Restaurant;Integrated Security=True");
        string sql = "SELECT * from Orders WHERE InvoiceID ='"+PrepareBill_txt1.Text+"'";
        DataSet dt = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(sql,conect);
        adapter.Fill(dt,"Orders");
        cr.SetDataSource(dt.Tables["Orders"]);

所以这个方法单独工作很好,另一个单独工作也很好, 但是当我尝试将它们组合时,我没有得到任何结果,只有第一个代码适用于 Invoic table。我试图把那个方法放在程序周围,但它没有用。这是我的大学项目。

当我启动程序时,我得到了数据库中的所有数据。我想像我创建的方法一样通过 InvoiceID select 它。这是我的Crystal举报图片:

根据您的代码,您只需从 Orders table 中获取记录,但您需要 join 两个 table 订单和发票以及 select 根据您的需要选择所需的列,然后像以前一样将列拖放到 crystal 报告中。

i.g 查询应该写成如下所示。

SELECT o.InvoiceID,
    o.CustomerName,
    o.Telephone,
    o.Date,
    o.Total,
    o.Discount, 
    o.ToPay,
    i.ItemNO,
    i.Category,
    i.ItemName,
    i.Price, 
    i.Qty, 
    i.SubTotal
FROM orders As o
INNER JOIN Invouce AS i ON o.InvoiceID = i.InvoiceID  
WHERE i.InvoiceID  = 1