Winform、WPF根据textbox、label、combobox等对数据库进行查询并显示内容

Winform、WPF根据textbox、label、combobox等对数据库进行查询并赋值给DataGirdView控件

private void SearchInvestInfo(string year, string printId, string investId, string department)
        {
            using (SqlConnection con = new SqlConnection(conStr))
            {
                con.Open();
                string sqlYear = String.Empty;
                string sqlPrintId = String.Empty;
                string sqlInvestId = String.Empty;
                string sqlDepartment = String.Empty;
                string equalsYear = String.Empty;
                string equalsPrintId = String.Empty;
                string equalsInvestId = String.Empty;
                string equalsDepartmrnt = String.Empty;
                if (!String.IsNullOrEmpty(year))
                {
                    sqlYear = " DATEPART(YEAR,[TimeStamp])+1 ";
                    equalsYear = "='" + txt_Year.Text.Trim() + "' AND ";
                    if (!String.IsNullOrEmpty(printId))
                    {
                        sqlPrintId = " [PrintID]";
                        equalsPrintId = "='" + txt_PrintId.Text.Trim() + "' AND ";
                        if (!String.IsNullOrEmpty(investId))
                        {
                            sqlInvestId = " [InvestID]";
                            equalsInvestId = "='" + txt_InvestId.Text.Trim() + "' AND ";
                            if (department != "全部")
                            {
                                sqlDepartment = " [Department]";
                                equalsDepartmrnt = "='" + comBox_Department.Text.Trim() + "' AND ";
                            }
                            else
                            {

                            }
                        }
                        else
                        {
                            if (department != "全部")
                            {
                                sqlDepartment = " [Department]";
                                equalsDepartmrnt = "='" + comBox_Department.Text.Trim() + "' AND ";
                            }
                            else
                            {

                            }
                        }
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(investId))
                        {
                            sqlInvestId = " [InvestID]";
                            equalsInvestId = "='" + txt_InvestId.Text.Trim() + "' AND ";
                            if (department != "全部")
                            {
                                sqlDepartment = " [Department]";
                                equalsDepartmrnt = "='" + comBox_Department.Text.Trim() + "' AND ";
                            }
                            else
                            {

                            }
                        }
                        else
                        {
                            if (department != "全部")
                            {
                                sqlDepartment = " [Department]";
                                equalsDepartmrnt = "='" + comBox_Department.Text.Trim() + "' AND ";
                            }
                            else
                            {

                            }
                        }
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(printId))
                    {
                        sqlPrintId = " [PrintID]";
                        equalsPrintId = "='" + txt_PrintId.Text.Trim() + "' AND ";
                        if (!String.IsNullOrEmpty(investId))
                        {
                            sqlInvestId = " [InvestID]";
                            equalsInvestId = "='" + txt_InvestId.Text.Trim() + "' AND ";
                            if (department != "全部")
                            {
                                sqlDepartment = " [Department]";
                                equalsDepartmrnt = "='" + comBox_Department.Text.Trim() + "' AND ";
                            }
                            else
                            {

                            }
                        }
                        else
                        {
                            if (department != "全部")
                            {
                                sqlDepartment = " [Department]";
                                equalsDepartmrnt = "='" + comBox_Department.Text.Trim() + "' AND ";
                            }
                            else
                            {

                            }
                        }
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(investId))
                        {
                            sqlInvestId = " [InvestID]";
                            equalsInvestId = "='" + txt_InvestId.Text.Trim() + "' AND ";
                            if (department != "全部")
                            {
                                sqlDepartment = " [Department]";
                                equalsDepartmrnt = "='" + comBox_Department.Text.Trim() + "' AND ";
                            }
                            else
                            {

                            }
                        }
                        else
                        {
                            if (department != "全部")
                            {
                                sqlDepartment = " [Department]";
                                equalsDepartmrnt = "='" + comBox_Department.Text.Trim() + "' AND ";
                            }
                            else
                            {

                            }
                        }
                    }
                }
                string sqlCmd = "SELECT " +
                                    "[Id] AS [ID]" +
                                    ",[InvestID] AS[投资编号]" +
                                    ",DATEPART(YEAR,[TimeStamp]) + 1 AS[年份]" +
                                    ",[Department] AS[部门]" +
                                    ",[AssetType] AS[资产类型]" +
                                    ",[AssetDescription] AS[资产描述]" +
                                    ",[CostCenter] AS[成本中心]" +
                                    ",[CountNum] AS[数量]" +
                                    ",[Remark] AS[备注]" +
                                    ",[MoneyType] AS[合同币别]" +
                                    ",[Budget_ALL] AS[年预算(千元)]" +
                                    ",[Budget_Jan] AS[Jan]" +
                                    ",[Budget_Feb] AS[Feb]" +
                                    ",[Budget_Mar] AS[Mar]" +
                                    ",[Budget_Apr] AS[Apr]" +
                                    ",[Budget_May] AS[May]" +
                                    ",[Budget_Jun] AS[Jun]" +
                                    ",[Budget_Jul] AS[Jul]" +
                                    ",[Budget_Aug] AS[Aug]" +
                                    ",[Budget_Sep] AS[Sep]" +
                                    ",[Budget_Oct] AS[Oct]" +
                                    ",[Budget_Nov] AS[Nov]" +
                                    ",[Budget_Dec] AS[Dec]" +
                                    ",[Spend_Jan]" +
                                    ",[Spend_Feb]" +
                                    ",[Spend_Mar]" +
                                    ",[Spend_Apr]" +
                                    ",[Spend_May]" +
                                    ",[Spend_Jun]" +
                                    ",[Spend_Jul]" +
                                    ",[Spend_Aug]" +
                                    ",[Spend_Sep]" +
                                    ",[Spend_Oct]" +
                                    ",[Spend_Nov]" +
                                    ",[Spend_Dec]" +
                                " FROM [BudgetForm]" +
                                " WHERE " + sqlYear + equalsYear + sqlPrintId + equalsPrintId + sqlInvestId + equalsInvestId + sqlDepartment + equalsDepartmrnt + " [DeleteFlag]='False' " +
                                "ORDER BY [Id] DESC";
                using (SqlDataAdapter sda = new SqlDataAdapter(sqlCmd, con))
                {
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    ds.Tables[0].Rows.Add();


                    #region DataSet添加最后一行并初始化
                    ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][1] = "总计";
                    ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][7] = 0;

                    for (int i = 10; i < 35; i++)
                    {
                        ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][i] = 0;
                    }
                    //for (int i = 23; i < 35; i++)
                    //{
                    //    ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][i] = 0;
                    //}
                    #endregion


                    #region DataSet最后一行赋值
                    for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++)
                    {
                        ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][7] = decimal.Parse(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][7].ToString()) + decimal.Parse(ds.Tables[0].Rows[i][7].ToString());
                        for (int j = 10; j < 35; j++)
                        {
                            ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][j] = decimal.Parse(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][j].ToString()) + decimal.Parse(ds.Tables[0].Rows[i][j].ToString());
                        }
                        //for (int j = 23; j < 35; j++)
                        //{
                        //    ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][j] = decimal.Parse(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][j].ToString()) + decimal.Parse(ds.Tables[0].Rows[i][j].ToString());
                        //}
                    }
                    #endregion
                    dgv_InvestInfo.DataSource = ds.Tables[0];
                    dgv_InvestInfo.Columns["ID"].Visible = false;
                }
            }
        }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值