C# 连接数据库,查询数据

//获取数据
private void GetData()
{
    //查询条件
    int day11,day12,day21,day22,month;
    day11 = 61; day12 = 90; //时间段1
    day21 = 91; day22 = 180; //时间段2
    month = 6; //时间段3
    try
    {
        //连接字符串
        string connectString = "Data Source=192.168.0.196;Initial Catalog=AIS20200915160937;User ID=sa;Password=sa";
        //实例化“连接对象”,并打开连接
        using (SqlConnection conn = new SqlConnection(connectString))
        {
            //打开数据库连接
            conn.Open();
            string sql = "exec Stock_Age_table " + day11 + "," + day12 + "," + day21 +"," + day22 + "," + month + "";
            SqlCommand comm = new SqlCommand(sql, conn);        //定义执行SQL语句的对象,并将SQL语句赋予对象
            SqlDataAdapter da = new SqlDataAdapter(comm);       //执行连接
            da.Fill(ds);                                        //将数据存入数据容器
            conn.Close();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            gridControl1.DataSource = dt;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("数据库连接失败!" + ex.Message);
    }
}

完整样列GetData(获取数据,添加序号列,合计数据)

//获取数据
private void GetData()
{
    //查询条件
    int day11,day12,day21,day22,month;
    day11 = 61; day12 = 90; //时间段1
    day21 = 91; day22 = 180; //时间段2
    month = 6; //时间段3
    try
    {
        //连接字符串
        string connectString = "Data Source=192.168.0.197;Initial Catalog=AIS20201015150818;User ID=sa;Password=sa123";
        //实例化“连接对象”,并打开连接
        using (SqlConnection conn = new SqlConnection(connectString))
        {
            #region 连接数据库,查询数据
            //打开数据库连接
            conn.Open();
            string sql = "exec Stock_Age_table " + day11 + "," + day12 + "," + day21 +"," + day22 + "," + month + "";
            SqlCommand comm = new SqlCommand(sql, conn);        //定义执行SQL语句的对象,并将SQL语句赋予对象
            SqlDataAdapter da = new SqlDataAdapter(comm);       //执行连接
            da.Fill(ds);                                        //将数据存入数据容器
            conn.Close();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            #endregion
            #region 添加序号列
            //添加序列号
            int rowLength = dt.Rows.Count;
            int colLength = dt.Columns.Count;
            DataRow[] newRows = new DataRow[rowLength];
            DataTable dtNew = new DataTable();
            dtNew.Columns.Add("序号");
            for (int i = 0; i < colLength; i++)
            {
                dtNew.Columns.Add(dt.Columns[i].ColumnName);
                //复制dt中的数据
                for (int j = 0; j < rowLength; j++)
                {
                    if (newRows[j] == null)
                        newRows[j] = dtNew.NewRow();
                    //将其他数据填充到第二列之后,因为第一列为新增的序号列
                    newRows[j][i + 1] = dt.Rows[j][i];
                }
            }
            foreach (DataRow row in newRows)
            {
                dtNew.Rows.Add(row);
            }
            //对序号列填充,从1递增
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dtNew.Rows[i]["序号"] = i + 1;
            }
            #endregion
            #region 添加合计行,计算每列数据
            //合计行
            DataRow dr = dtNew.NewRow();
            dr["仓库名称"] = "合计";
            for (int j = 1; j < dt.Columns.Count; j++)
            {
                float value = 0;  //单个数据值
                float sum = 0;  //数据总和
                string returnstr = "";  //列字段返回值
                int k = 0;  //当前列数
                string str = "%";  //数据中的特殊字符
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //判断是否存在百分号,如果存在删除百分号,将数据转化为整形
                    string x1 = dt.Rows[i][j].ToString();
                    if (x1.Contains(str))
                    {
                        string result = x1.Replace(str, "");  //去除%号
                        value = float.Parse(result);
                        sum += value;
                    }
                    else
                    {
                        value = int.Parse(x1);
                        sum += value;
                    }   
                }
                //获取列字段名
                foreach (BandedGridColumn dc in this.bandedGridView1.Columns)
                {
                    if (k == j+1)
                    {
                        returnstr = dc.FieldName;
                    }
                    k++;
                }
                //判断列数据是否需要添加%号
                if (returnstr.Contains(str))
                {
                    dr[returnstr] = (sum/dt.Rows.Count).ToString()+str;  //算出平均值,并添加%号
                }
                else
                {
                    dr[returnstr] = sum;  
                }
            }
            dtNew.Rows.Add(dr);  //在dtNew表中添加合计行
            #endregion
            gridControl1.DataSource = dtNew;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郑先生Ethan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值