我正在制作发票计划,我需要在我的数据库中准备2个表,我在两个表中建立了一个名为Invoice_No的关系联盟,我打电话给两个并从Invoice_No调用我的发票数据但是当我执行所有工作时好,在输入数据后和我按下时 打印按钮它给我一个错误不明确的列名Invoice_No,给我一个解决方案
Cursor = Cursors.WaitCursor;
frmSalesinvoice frm = new frmSalesinvoice();
invoice rpt = new invoice();
//The report you created.
SqlConnection myConnection = default(SqlConnection);
SqlCommand MyCommand = new SqlCommand();
SqlDataAdapter myDA = new SqlDataAdapter();
DS_Invoice_all myDS = new DS_Invoice_all();
myConnection = new SqlConnection(cs);
MyCommand.Connection = myConnection;
MyCommand.CommandText = "select * from Invoice_Info,Items_Soled where Items_Soled.Invoice_No=Invoice_Info.Invoice_No and Invoice_No= '" + textBoxInvoiceNo.Text + "'";
MyCommand.CommandType = CommandType.Text;
myDA.SelectCommand = MyCommand;
myDA.Fill(myDS, "Invoice_Info");
myDA.Fill(myDS, "Items_Soled");
rpt.SetDataSource(myDS);
frm.crystalReportViewer1.ReportSource = rpt;
frm.Show();
答案 0 :(得分:1)
您需要更改SQL查询,以便没有任何重复的列名。您可以为列添加别名,以便获得唯一的名称。
例如,如果两列都有一个名为ID的字段,则应使它们唯一。
select
info.Id As info_id,
soled.Id As soled_id,
--rest of your columns here with the table prefix
from Invoice_Info info
inner join Items_Soled soled
on soled.Invoice_No=info.Invoice_No
where info.Invoice_No= '
此外,您应该尝试避免using Old Style joins他们可以使查询更难以阅读..
答案 1 :(得分:0)
当您在一个语句中连接两个或多个表时会发生此错误,并且这些表中的列之间具有相同的名称,并且在引用语句中的列时未使用表名为列名添加前缀。
Items_Sold.Invoice_No=Invoice_Info.Invoice_No
答案 2 :(得分:0)
您需要在此处使用表名,因为两个表中都存在该列。 Sql Server对你指的是哪一列感到困惑:)
select *
from Invoice_Info,Items_Soled
where Items_Soled.Invoice_No=Invoice_Info.Invoice_No
and [TableName].Invoice_No = ????
<强> JOIN 强>
实现相同结果的更好方法是使用JOIN和ON子句,类似这样
select *
from Invoice_Info INNER JOIN Items_Soled
ON Items_Soled.Invoice_No=Invoice_Info.Invoice_No
WHERE [TableName].Invoice_No = ????
修改强>
Cursor = Cursors.WaitCursor;
frmSalesinvoice frm = new frmSalesinvoice();
invoice rpt = new invoice();
//The report you created.
SqlConnection myConnection = default(SqlConnection);
SqlCommand MyCommand = new SqlCommand();
SqlDataAdapter myDA = new SqlDataAdapter();
DS_Invoice_all myDS = new DS_Invoice_all();
myConnection = new SqlConnection(cs);
MyCommand.Connection = myConnection;
MyCommand.CommandText = "select * from Invoice_Info INNER JOIN Items_Soled ON
Items_Soled.Invoice_No=Invoice_Info.Invoice_No WHERE [TableName].Invoice_No = '" + textBoxInvoiceNo.Text + "'";
MyCommand.CommandType = CommandType.Text;
myDA.SelectCommand = MyCommand;
myDA.Fill(myDS, "Invoice_Info");
myDA.Fill(myDS, "Items_Soled");
rpt.SetDataSource(myDS);
frm.crystalReportViewer1.ReportSource = rpt;
frm.Show();