【问题标题】:Crystal report Ambiguous column name error.水晶报表不明确的列名错误。
【发布时间】:2013-12-08 14:27:29
【问题描述】:

我正在制作一个发票程序,我需要从我的数据库中的 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();

【问题讨论】:

    标签: c# sql-server winforms crystal-reports


    【解决方案1】:

    当您在一个语句中连接 2 个或多个表并且这些表中有同名的列并且您在引用语句中的列时没有在列名前加上表名时,会发生此错误。

    Items_Sold.Invoice_No=Invoice_Info.Invoice_No
    

    【讨论】:

    • 那是怎么加前缀的?
    • 您说您有 2 张桌子。让我们将它们命名为 table1 和 table2。假设每个表都有一个字段。我们将其命名为 field1。因此,您可以将其前缀为 table1.field1 和 table2.field1。
    【解决方案2】:

    您需要在此处使用表名,因为该列在两个表中都存在。 Sql Server 对您指的是哪一列感到困惑:)

    select * 
    from Invoice_Info,Items_Soled 
    where Items_Soled.Invoice_No=Invoice_Info.Invoice_No 
    and [TableName].Invoice_No = ????
    

    加入
    实现相同结果的更好方法是使用 JOIN with 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();
    

    【讨论】:

    • 我想在哪里使用它。可以得到更多这方面的信息
    • 你在代码中使用过 sql 查询的地方用这个查询替换它
    • @user3021112 现在看看我已经更新了我的答案
    • 您是否将 [TableName] 替换为实际的表名 Invoice_Info 或 Items_Soled ?
    【解决方案3】:

    您需要更改 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,它们会使查询更难阅读。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多