将数据库表中一列值取出到数组中与某一确定值进行比较

来源:互联网 发布:户外直播用的什么网络 编辑:程序博客网 时间:2024/06/05 07:28

写代码之前目的是达到,将数据库表中存放等级那一列值取出到数组中,再用系统原先等级与数组中的数一一进行比较,再得出最终的等级

连接数据库,定义一个数组,遍历数据库表中的值到数组中,再用一个for循环进行比较

private void button48_Click(object sender, EventArgs e)
        {
            String constr = "data source=.;initial Catalog=等级保护;user id=sa;password=123";
            SqlConnection conn = new SqlConnection(constr);
            string sql = "select Testres from  Clevel";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            DataSet thisdataset = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            adapter.Fill(thisdataset, "Clevel");
            int i;
            i = thisdataset.Tables[0].Rows.Count;
            int[] arr = new int[i];
            i = 0;
            foreach (DataRow row in thisdataset.Tables[0].Rows)
            {
                arr[i] = Convert.ToInt32(row["Testres"]); i++;
            }
            for (i = 0; i < arr.Length; i++)
            {
                m = arr[0];
                if (arr[i] < m)
                { m = arr[i]; }
            }
            String message = string.Format("该系统等级是:{0}", m);
            MessageBox.Show(message, "系统测评结果");
            Console.ReadLine();
        }

阅读全文
0 0
原创粉丝点击