代码优化:列表循环和查询c#sql server紧凑版

时间:2016-11-29 15:11:00

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

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

  • 前端:C#与VS2012 .Net 4.0
  • 后端:sql server compact edition 4.0

数据库架构:

  • 表产品:productId;名称

  • table product_druginteractionclass:productId; druginteractionclassId

  • 表格互动:interactionId; druginteractionclassId1;
    druginteractionclassId2; riskcomment

  • table druginteractionclass:druginteractionclassId;名称

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

方法是:

  • 循环投掷 Productgridview 以根据名称检索 productid 并根据 productid 选择 druginteractionclassId ,然后将结果放入另一个dataGridView( listclassification
  • 从dataGridView( listclassification )列druginteractionclassId中的数据创建2个列表///这里显示一个小问题它显示相同药物之间的相互作用,因为列表保持相同的所有药物干扰类数量并且双重迭代扔掉它们测试组合互动/// 如何以编程方式创建单独的列表,而我不知道将添加多少药物?
  • 循环投掷2列表并根据组合选择交互并将结果与​​药物交互类名称
  • 连接

是否可以动态创建列表并循环抛出它们        做组合而不知道运行时间有多少列表?

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

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

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

        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--;
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

>>> a = "$$$$abcd"
>>> a.count('$')
4
>>> a = '$abcd$dsf$'
>>> a.count('$')
3