【问题标题】:code optimisation : list loop and query c# sql server compact edition代码优化:列表循环和查询 c# sql server compact edition
【发布时间】:2016-11-29 15:11:00
【问题描述】:

此处的代码用于检查 dataGridView ( Productgridview ) 中添加的药物之间是否有任何交互

  • 前端:带有 VS2012 .Net 4.0 的 C#
  • 后端:sql server compact edition 4.0

数据库架构:

  • 表产品:productId;姓名

  • 表 product_druginteractionclass : productId ; druginteractionclassId

  • 表交互:interactionId; druginteractionclassId1 ;
    druginteractionclassId2 ;风险评论

  • table druginteractionclass : druginteractionclassId ;姓名

许多产品可以有相同的药物相互作用类别编号

方法是:

  • 循环抛出 Productgridview 以根据 name 检索 productid 并根据productid选择druginteractionclassId,然后将结果放入另一个dataGridView(listclassification
  • 从 dataGridView (listclassification) 列 druginteractionclassId 中的数据创建 2 个列表 /// 一个小问题是这里显示了相同药物之间的交互,因为列表包含相同的所有 druginterctionclass 编号并重复抛出它们测试组合相互作用 /// 如何以编程方式创建单独的列表,而我不知道会添加多少药物?
  • 循环抛出 2 个列表并根据组合选择相互作用,并将结果与​​药物相互作用类名连接起来

是否可以动态创建列表并循环将它们扔到 在运行前不知道有多少列表的情况下进行组合?

我还想在交互网格视图中循环,如果没有删除不适当的交互,则检查产品是否重复。

  • 可能全部关闭,可以在一个查询中总结,如何写 一个(那种选择的选择)?

实际上我是药学系的学生(我只是喜欢编码但做得不好,我什至没有完成任何 c# 书籍,在 c# / sql / ado.net 之间只有大约 600 页,所以请原谅)

        var ds2 = new DataSet();
        for (var i = 0; i < Productgridview.Rows.Count; i++)
        {
            var listclasse = Productgridview.Rows[i].Cells["Productid"].Value.ToString();
            var datadrug2 = "SELECT *  FROM product_druginteractionclass" +
                            " where productId = '" +
                            listclasse + "'"; // listclasse is the list of  manually added drugClass
            var connection1 = new SqlCeConnection(connectionString);

            var dataadapter1 = new SqlCeDataAdapter(datadrug2, connection1);
            //var ds = new DataSet();
            connection1.Open();
            dataadapter1.Fill(ds2, "product_druginteractionclass");
            connection1.Close();
        }
        listclassification.DataSource = ds2;
        listclassification.DataMember = "product_druginteractionclass";

/////////////// put the druginteractionclass into 2 lists 
        var list1 = new List<string>();
        var list2 = new List<string>();
        foreach (DataGridViewRow item in listclassification.Rows)
            if ((item.Cells.Count >= 2) && //atleast two columns
                (item.Cells[1].Value != null)) //value is not null
            {
                list1.Add(item.Cells[1].Value.ToString());
                list2.Add(item.Cells[1].Value.ToString());
            }
        //for (var i = 0; i <= list.Count - 1; i++)
        //{
        //   // MessageBox.Show(list[i].ToString());
        //}

        //////////// select interaction based on druginteractionclass
        var ds = new DataSet();
        for (var i = 0; i <= list1.Count - 1; i++)
            for (var j = 0; j <= list2.Count - 1; j++)
            {
                var value = list1[i];
                var value1 = list2[j];
                var datadrug3 = "SELECT u1.name, u2.name  , m.* " +
                                "FROM druginteractionclass u1 " +
                                "left outer JOIN interaction m" +
                                " ON u1.druginteractionclassId = m.druginteractionclassId1 " +
                                "left outer JOIN druginteractionclass u2 " +
                                "ON u2.druginteractionclassId = m.druginteractionclassId2" +
                                " where m.druginteractionclassId1 = '" + value +
                                "' and m.druginteractionclassId2 ='" + value1 + "'" +
                                "Order by m.severity ";

                var connection = new SqlCeConnection(connectionString);

                var dataadapter = new SqlCeDataAdapter(datadrug3, connection);

                connection.Open();
                dataadapter.Fill(ds, "interaction");
                connection.Close();
            }
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "interaction";

        /////// remove duplicated interactions  
        for (var currentRow = 0; currentRow < dataGridView1.Rows.Count - 1; currentRow++)
        {
            var rowToCompare = dataGridView1.Rows[currentRow];

            for (var otherRow = currentRow + 1; otherRow < dataGridView1.Rows.Count; otherRow++)
            {
                var row = dataGridView1.Rows[otherRow];

                var duplicateRow = true;

                for (var cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
                    if (!rowToCompare.Cells[2].Value.Equals(row.Cells[2].Value))
                    {
                        duplicateRow = false;
                        break;
                    }

                if (duplicateRow)
                {
                    dataGridView1.Rows.Remove(row);
                    otherRow--;
                }
            }
        }

【问题讨论】:

    标签: c# sql-server-ce-4


    【解决方案1】:
                            var datadrug3 =
                            @" SELECT m.interactionId , u1.name , n.name , u2.name,n2.name   , m.riskComment , m.precautionComment , m.severity 
    
                             FROM druginteractionclass u1 
                             left outer JOIN interaction m 
                             ON u1.druginteractionclassId = m.druginteractionclassId1 
                             left outer JOIN druginteractionclass u2 
                             ON u2.druginteractionclassId = m.druginteractionclassId2 
    
                             left outer join product_druginteractionclass p1 
                             ON  p1.druginteractionclassId = m.druginteractionclassId1 
    
                             left outer JOIN product_druginteractionclass p2 
                             ON  p2.druginteractionclassId = m.druginteractionclassId2 
    
                             left outer join product n 
                             ON  n.productId= p1.productId 
    
                             left outer join product n2 
                             ON n2.productId= p2.productId 
                             where n.productId = @value  and n2.productId = @value1 ";
    
                        var connection = new SQLiteConnection(connectionString3);
                        var com = new SQLiteCommand(datadrug3, connection);
                            com.Parameters.AddWithValue("@value", value);
                            com.Parameters.AddWithValue("@value1",value1 );
                            var dataadapter = new SQLiteDataAdapter(com);
    
                        connection.Open();
                        dataadapter.Fill(ds, "interaction");
                        connection.Close();
    

    【讨论】:

    • 考虑使用verbatim string literal 而不是连接字符串(很容易出错)并使用sqlParameters
    • 感谢 Gian Paolo,逐字字符串有助于很好地解析查询,并且从不同的断点我注意到 /// dataadapter.Fill(ds, "interaction");//// 需要很长时间,我在 SQL Compact 查询分析器中测试查询,抛出异常(内存不足)我得出结论,sql server compact 的引擎达到了他的极限(sql server 2014 执行速度如此之快,条件是“where”没有冻结在应用程序中并且没有条件是他在 2 分钟内完成 350 万行)
    • 实际上,逐字字符串应该只是提高代码的可读性,而不是性能:将字符串与 + 连接不会花费任何时间。相反,使用 SQLParameters 而不是连接参数的值将提高 SQL Server 性能:sql server 将在第一次执行查询时“编译”查询,然后它将使用缓存的编译版本。
    • @Gian Paolo,我使用参数和 SqlCe 渲染结果,但速度很慢;我改用 sqlite 更好(大约 13 秒来测试一种组合);我现在知道是引擎无法处理查询,您能否建议另一个查询(sql server 2014 中的此查询没有条件在 14 分钟内结果 1700 万行并且最后一次左连接的成本更高)跨度>
    猜你喜欢
    • 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
    相关资源
    最近更新 更多