Ejemplo n.º 1
0
        /// <summary>
        /// 从System.Data.DataTable导入数据到数据库
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private int InsertData(DataTable dt)
        {
            int      i = 0;
            string   cmonth = dateTimePickerMonth.Text;
            string   mat_code = "", mat_name = "";
            string   sql, sql2, msg;
            decimal  price = 0;
            DateTime begin_date, end_date;
            ConnDB   conn = new ConnDB();
            DataSet  ds;
            bool     err = false;

            foreach (DataRow dr in dt.Rows)
            {
                sql2 = "select price from COST_MATL_PRICE where matl_no ='" + dr["料号"].ToString().Trim() + "' and not (begin_date > '" + dr["价格结束日期"].ToString().Trim() + "' or end_date <'" + dr["价格开始日期"].ToString().Trim() + "')";
                //rows = conn.ReturnRecordCount(sql2);
                ds = conn.ReturnDataSet(sql2);
                if (dr["料号"].ToString().Trim() == null || dr["料号"].ToString().Trim() == "")
                {
                    MessageBox.Show("料号格式有错误!");
                    err = true;
                    break;
                }
                else if (dr["价格开始日期"].ToString().Trim() == null || dr["价格开始日期"].ToString().Trim() == "")
                {
                    MessageBox.Show("价格开始日期格式有错误!");
                    err = true;
                    break;
                }
                else if (dr["价格结束日期"].ToString().Trim() == null || dr["价格结束日期"].ToString().Trim() == "")
                {
                    MessageBox.Show("价格结束日期格式有错误!");
                    err = true;
                    break;
                }
                else if (ds.Tables[0].Rows.Count > 0)
                {
                    msg = dr["料号"].ToString().Trim() + "已存在重叠时间范围的价格信息!";
                    MessageBox.Show(msg);
                    showDetail(ds);
                    err = true;
                    break;
                }
            }
            if (!err)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    mat_code   = dr["料号"].ToString().Trim();
                    mat_name   = dr["物料名称"].ToString().Trim();
                    price      = Common.StrToDecimal(dr["单价"].ToString().Trim());
                    begin_date = Convert.ToDateTime(dr["价格开始日期"].ToString().Trim());
                    end_date   = Convert.ToDateTime(dr["价格结束日期"].ToString().Trim());
                    sql        = string.Format("Insert into COST_MATL_PRICE(MATL_NO,MATL_NAME,PRICE,BEGIN_DATE,END_DATE) Values ('{0}','{1}','{2}','{3}',{4})", mat_code, mat_name, price, begin_date, end_date);
                    conn.EditDatabase(sql);
                    i++;
                }
            }

            return(i);
        }