datatable的案例

//始终要对比的数据表,查看是否新添加
string sglCheckmodule = “select * from XXX”;
//体检项目对应_左gridcontrol显示数据
dtCheckmodule = BLLUtil.DoQueryBySql(sglCheckmodule);
grdCheckmodule.DataSource = dtCheckmodule;

 try
	            {
	                DataTable dtsaveMapping = BLLUtil.DoQueryBySql("select * from T_HNSYB_TAIHE_MODULES_MAPPING");
	                dtsaveMapping.TableName = "T_HNSYB_TAIHE_MODULES_MAPPING";
	                DataColumn[] col = new DataColumn[] { dtsaveMapping.Columns["hosp_code"] };
	                dtsaveMapping.PrimaryKey = col;
	                DataTable dtsaveModules = grdCheckmodule.DataSource as DataTable;

            DataRow row = null;
            if (dtsaveMapping.Rows.Count == 0)
            {
                for (int i = 0; i < dtCheckmodule.Rows.Count; i++)
                {
                    row = dtsaveMapping.NewRow();
                    row["1"] =  dtsaveModules.Rows[i]["1"].ToString();
                    row["2"] = dtsaveModules.Rows[i]["2"].ToString();
                    row["3"] = dtsaveModules.Rows[i]["3"].ToString();
                    row["4"] = dtsaveModules.Rows[i]["4"].ToString();
                    row["5"] = dtsaveModules.Rows[i]["5"].ToString();
                    row["6"] = dtsaveModules.Rows[i]["6"].ToString();
                    row["7"] = dtsaveModules.Rows[i]["7"].ToString();
                    row["8"] = dtsaveModules.Rows[i]["8"].ToString();
                    row["9"] = dtsaveModules.Rows[i]["9"].ToString();
                    row["10"] = dtsaveModules.Rows[i]["10"].ToString();
                    row["11"] = dtsaveModules.Rows[i]["11"].ToString();
                    row["12"] = dtsaveModules.Rows[i]["12"].ToString();
                    row["13"] = dtsaveModules.Rows[i]["13"].ToString();
                    row["14"] = dtsaveModules.Rows[i]["14"].ToString();
                    row["15"] = DateTime.Now.ToString();
                    dtsaveMapping.Rows.Add(row);
                }
            }
            else
            {
                //循环T_CHECKMODULE里面的内容
                for (int i = 0; i < dtCheckmodule.Rows.Count; i++)
                {
                    DataRow[] rowArry = dtsaveMapping.Select("hosp_code='" + dtCheckmodule.Rows[i]["hosp_code"].ToString() + "'");

                    if (rowArry.Length == 0)
                    {
                        row = dtsaveMapping.NewRow();
                        row["1"] =  dtsaveModules.Rows[i]["1"].ToString();
                        row["2"] = dtsaveModules.Rows[i]["2"].ToString();
                        row["3"] = dtsaveModules.Rows[i]["3"].ToString();
                        row["4"] = dtsaveModules.Rows[i]["4"].ToString();
                        row["5"] = dtsaveModules.Rows[i]["5"].ToString();
                        row["6"] = dtsaveModules.Rows[i]["6"].ToString();
                        row["7"] = dtsaveModules.Rows[i]["7"].ToString();
                        row["8"] = dtsaveModules.Rows[i]["8"].ToString();
                        row["9"] = dtsaveModules.Rows[i]["9"].ToString();
                        row["10"] = dtsaveModules.Rows[i]["10"].ToString();
                        row["11"] = dtsaveModules.Rows[i]["11"].ToString();
                        row["12"] = dtsaveModules.Rows[i]["12"].ToString();
                        row["13"] = dtsaveModules.Rows[i]["13"].ToString();
                        row["14"] = dtsaveModules.Rows[i]["14"].ToString();
                        row["15"] = DateTime.Now.ToString();
                        dtsaveMapping.Rows.Add(row);
                    }
                    else
                    {
                        //rowArry[0]["1"] = dtsaveModules.Rows[i]["1"].ToString();
                        rowArry[0]["2"] = dtsaveModules.Rows[i]["2"].ToString();
                        rowArry[0]["3"] = dtsaveModules.Rows[i]["3"].ToString();
                        rowArry[0]["4"] = dtsaveModules.Rows[i]["4"].ToString();
                        rowArry[0]["5"] = dtsaveModules.Rows[i]["5"].ToString();
                        rowArry[0]["6"] = dtsaveModules.Rows[i]["6"].ToString();
                        rowArry[0]["7"] = dtsaveModules.Rows[i]["7"].ToString();
                        rowArry[0]["8"] = dtsaveModules.Rows[i]["8"].ToString();
                        rowArry[0]["9"] = dtsaveModules.Rows[i]["9"].ToString();
                        rowArry[0]["10"] = dtsaveModules.Rows[i]["10"].ToString();
                        rowArry[0]["11"] = dtsaveModules.Rows[i]["11"].ToString();
                        rowArry[0]["12"] = dtsaveModules.Rows[i]["12"].ToString();
                        rowArry[0]["13"] = dtsaveModules.Rows[i]["13"].ToString();
                        rowArry[0]["14"] = dtsaveModules.Rows[i]["14"].ToString();
                        rowArry[0]["15"] = DateTime.Now.ToString();
                    }
                }
            }
          //自己封装的方法
            int save =XXX.SaveTable(dtsaveMapping);
            if (save > 0)
            {
                MessageBox.Show("保存成功!");
            }
            else
            {
                MessageBox.Show("保存失败!");
            }

你可能感兴趣的:(工作中问题)