【问题标题】:dynamic sql generation is not supported against multiple base tables多个基表不支持动态 sql 生成
【发布时间】:2012-09-28 12:22:10
【问题描述】:

我尝试向 SQL DB 中的表添加新行,但遇到了问题:

不支持对多个基表进行动态 sql 生成

这是我尝试过的代码:

private MyClass myClass = new MyClass();
private SqlDataAdapter adapter;
private SqlDataAdapter adapter2;

private void GestionCollections_Load(object sender, EventArgs e)
{
     adapter = new SqlDataAdapter("select Id_Collection ID, Libelle_Collection Collection,Libelle_Editeur Editeur from Collection_ left join Editeur on Id_Editeur = Collection_.Id_Editeur_Editeur", myClass.cnx);
     adapter.Fill(myClass.ds, "Collection_");

     adapter2 = new SqlDataAdapter("Select Id_Editeur ID,Libelle_Editeur Editeur from Editeur", myClass.cnx);
     adapter2.Fill(myClass.ds, "Editeur");
}

private void AjouterBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    String newKeyWordCollectionName = ajoutCollection.KeyWordCollectionName;
    String newKeyWordAEditeurName = ajoutCollection.KeyWordEditeurName;        
    DataRow row = myClass.ds.Tables["Collection_"].NewRow();
    row[1] = newKeyWordCollectionName;

    foreach(var myRow in myClass.ds.Tables["Editeur"].AsEnumerable())
    {
         if (newKeyWordAEditeurName == myRow[1] as String)
              row[2] = (int)myRow[0];
    }
     myClass.ds.Tables["Collection_"].Rows.Add(row);
     SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
     adapter.Update(myClass.ds, "Collection_");

}

【问题讨论】:

    标签: c# sql-server ado.net


    【解决方案1】:

    更改您的选择查询并添加不同的内部联接。

    例如有两个查询,您可以从中了解我想告诉您的内容

    查询错误

    select iop.pob_id, iop.pob_product_id, iop.pob_qty, iop.pob_unit_id
        , iop.pob_rate, iop.pob_value, iop.pob_fiscalyear_id
        , **p.product_desc** as orderBy from inv_product_open_balc iop
    left join inv_product p on iop.pob_product_id = p.product_id
    where p.product_desc like 'Air Freshner%' and iop.pob_fiscalyear_id = 3
    

    正确查询

    select distinct iop.pob_id, iop.pob_product_id, iop.pob_qty
        , iop.pob_unit_id, iop.pob_rate, iop.pob_value, iop.pob_fiscalyear_id
        , **(select Product_desc from** inv_product p where p.product_id = iop.pob_product_id )as orderBy
    from inv_product_open_balc iop
    inner join inv_product p on iop.pob_product_id = p.product_id
    where p.product_desc like 'Air Freshner%' and iop.pob_fiscalyear_id = 3
    

    【讨论】:

    • 这是一个很好的答案,很惊讶它没有更多的支持。
    • 对我来说很好,但我不需要包含内部连接或不同:选择 iop.pob_id,iop.pob_product_id,iop.pob_qty,iop.pob_unit_id, iop.pob_rate,iop.pob_value ,iop.pob_fiscalyear_id,(select Product_desc from inv_product as p where p.product_id = iop.pob_product_id ) as orderBy from inv_product_open_balc as iop where p.product_desc like 'Air Freshner%' and iop.pob_fiscalyear_id = 3
    【解决方案2】:

    你不能在这里使用SqlCommandBuilder

    自动生成单表命令,用于协调对 DataSet 所做的更改与...

    这里的关键词是“单表”。它无法从SELECT 语句逆向工程应该如何应用特定更新(例如,如果您NULL 左连接右侧的所有列,它应该删除行还是设置每一列为空。

    您需要在 SqlDataAdapter 上编写适当的插入、更新和删除命令。

    【讨论】:

      【解决方案3】:

      使用SqlCommandBuilder,您可以生成CRUD operation on entity

      使用要求是定义Select command before inserting,并在选择命令中包含你的primary Key

      链接:http://msdn.microsoft.com/fr-fr/library/system.data.sqlclient.sqlcommandbuilder(v=vs.80).aspx

      MSDN定义:自动生成Transact-SQL语句更新single table

      注意:在您的更新 selectCommand 中,您定义了左连接查询,因此您可以创建左连接查询,将这个查询替换为仅选择。

      【讨论】:

      • 注意:在您的更新 selectCommand 中,您定义了左连接查询,因此您可以创建左连接查询,只需选择替换此查询。
      • 左连接查询不是单表,你有两个或更多的表
      • 我不想只选择语句我必须使用左连接查询,检查这个:link
      • 使用 SqlCommandBuilder 和左连接你不能用 Spoon 生成你的查询
      • 那么没有解决办法吗?因为我需要使用这个左连接查询来给我想要的正确结果
      【解决方案4】:

      使用内部联接在您的选择语句中添加 DISTINCT。

      这将解决问题。

      喜欢选择不同的员工.Ecode,......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-25
        • 1970-01-01
        • 2017-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-19
        相关资源
        最近更新 更多