[英]Crystal Reports trying to select from 2 tables
我想从 2 个表中获取 select 数据。 这是2张桌子
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])
所以我想通过 Invoice ID 从这两个表中获取 select 数据。 这是我的代码
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();
我为另一个表从 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 表。 我试图将该方法放在程序中,但它不起作用。 这是我的大学项目。
当我启动程序时,我得到了数据库中的所有数据。 我想像我创建的方法一样通过 InvoiceID 来 select 它。 这是我的水晶报表图片:
根据您的代码,您只需从Orders
表中获取记录,但您需要根据需要join
两个表 order 和 Invoice 和 select 连接到所需的列,然后像以前一样在水晶报表中拖放列。
ig查询应该写成如下所示的内容。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.