MatCode.cs
上传用户:tiancihang
上传日期:2014-03-12
资源大小:21387k
文件大小:10k
源码类别:

.net编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using com.etong.BusinessRule.Zwf;
  7. using com.etong.SqlDataConnect;
  8. namespace com.etong.BusinessRule.MatCode
  9. {
  10.     public class MatCode
  11.     {
  12.         private MSSqlDataAccess MSDA = new MSSqlDataAccess(0);       
  13.         public MatCode()
  14.         {
  15.             //
  16.             // TODO: 在此处添加构造函数逻辑
  17.             //
  18.         }
  19.         /*************************************************************************************************************  
  20.                                              * Module: 物资代码管理
  21.                                              * Desc: 添加、更新、删除、查询记录
  22.                                              * Author:徐秀梅
  23.                                              * Date: 2007-03
  24.         **************************************************************************************************************/
  25.         /// <summary>
  26.         /// 添加物资代码记录
  27.         /// </summary>
  28.         /// <returns>返回一bool类型值</returns>
  29.         public bool MatCodeAdd(string matcode,string matname,string matdecs)
  30.         {
  31.             QueryParameterCollection Params = new QueryParameterCollection();
  32.             Params.Clear();          
  33.             Params.Add("@matcode", matcode);
  34.             Params.Add("@matname", matname);
  35.             Params.Add("@matdecs", matdecs);
  36.             string sql = "insert into tb_sys_MatCode([MatCode],[MatName],[MatDecs]) values (@matcode,@matname,@matdecs)";
  37.             MSDA.Open();
  38.             NewObj obj=new NewObj();
  39.             bool returnvalue = obj.insert(sql, Params, CommandType.Text);           
  40.             MSDA.Close();
  41.             return returnvalue; 
  42.         }
  43.         /// <summary>
  44.         ///更新物资代码记录
  45.         /// </summary>
  46.         /// <returns>返回一bool类型值</returns>
  47.         public bool MatCodeUpdate(int id, string matcode, string matname, string matdecs)
  48.         {
  49.             QueryParameterCollection Params = new QueryParameterCollection();
  50.             Params.Clear();
  51.             Params.Add("@id", id);
  52.             Params.Add("@matcode", matcode);
  53.             Params.Add("@matname", matname);
  54.             Params.Add("@matdecs", matdecs);
  55.             string sql = "update tb_sys_MatCode set [MatCode]=@matcode,[MatName]=@matname,[MatDecs]=@matdecs where [ID]=@id";
  56.             MSDA.Open();
  57.             NewObj obj = new NewObj();
  58.             bool returnvalue = obj.Update(sql, Params, CommandType.Text);
  59.             MSDA.Close();
  60.             return returnvalue;
  61.         }
  62.         /// <summary>
  63.         ///删除物资代码记录
  64.         /// </summary>        
  65.         public void MatCodeDelete(int id)
  66.         {
  67.             string sql = "delete from tb_sys_MatCode where [ID]='" + id + "'";
  68.             MSDA.Open();
  69.             MSDA.ExecuteNonQuery(CommandType.Text, sql, null);
  70.             MSDA.Close();
  71.         }
  72.         /// <summary>
  73.         /// 查找指定或全部物资代码记录
  74.         /// </summary>
  75.         /// <param name="SearchTxt">查找内容</param>
  76.         /// <param name="i">查找关键字,例:1:物资代码;2:物资代码名称;3:物资代码ID;非1,2,3且SearchTxt为空:查找全部</param>
  77.         public DataView MatCodeSearch(string SearchTxt, string i)
  78.         {
  79.             string sql= "select [ID],[MatCode],[MatName],[MatDecs] from tb_sys_MatCode";
  80.             if (i == "1" && SearchTxt != "")
  81.             {
  82.                sql = sql + "  where [MatCode]='" + SearchTxt + "'";
  83.             }
  84.             if (i == "2" && SearchTxt != "")
  85.             {
  86.                sql = sql+ " where [MatName]='" + SearchTxt + "'";
  87.             }
  88.             if (i == "3" && SearchTxt != "")
  89.             {
  90.                 sql = sql + " where [ID]='" + SearchTxt + "'";
  91.             }  
  92.             DataSet ds = new DataSet();
  93.             MSDA.Open();
  94.             NewObj newobject = new NewObj();
  95.            
  96.             DataView dv = newobject.Search(sql, null, CommandType.Text).Tables[0].DefaultView;
  97.            
  98.             MSDA.Close();
  99.             return dv; 
  100.         }
  101.         /// <summary>
  102.         /// 判断物资代码是否符合要求
  103.         /// </summary>
  104.         /// <param name="SearchTxt">物资代码</param>  
  105.         ///  /// <returns>返回一int类型值,返回值-2:物资代码没校验码;-1:物资代码错误;0:物资代码校验码为0;其他:物资代码啊校验码为返回值</returns>
  106.         public bool MatCodeCheck(string matcode)
  107.         {
  108.             bool flag = false;
  109.             char[] str=null;
  110.             int len = matcode.Length;
  111.             for (int i = 0; i < len; i++)
  112.             {
  113.                 str = matcode.ToCharArray();
  114.             }
  115.             if (len<=5|| len == 8 || len == 10)
  116.             {
  117.               flag = true;
  118.             }
  119.             if (len == 14)
  120.             {
  121.                 //判断校验位是否正确
  122.                 int checkid=0;
  123.                 for (int i =12; i >= 0; i--,i--)
  124.                 {                  
  125.                     checkid +=int.Parse( str[i].ToString());                     
  126.                 }
  127.                 checkid = checkid * 3;
  128.                 for (int i = 11; i >= 0; i--,i--)
  129.                 {
  130.                     checkid +=int.Parse( str[i].ToString()); 
  131.                 }
  132.                 char[] checkstr = null;
  133.                 int len2 = checkid.ToString().Length;
  134.                 for (int i = 0; i < len2; i++)
  135.                 {
  136.                     checkstr =checkid.ToString().ToCharArray();
  137.                 }                
  138.                 if (Convert.ToInt32( checkstr[len2-1].ToString()) != 0)
  139.                 {
  140.                     checkid = 10 - Convert.ToInt32(checkstr[len2-1].ToString());
  141.                 }
  142.                 else
  143.                     checkid = 0;
  144.                 if (int.Parse(str[len-1].ToString()) == checkid)
  145.                     flag = true;
  146.                 
  147.             }
  148.             return flag;
  149.  
  150.         }
  151.     }
  152.  public class InExcel
  153.     {
  154.         private MSSqlDataAccess MSDA = new MSSqlDataAccess(0);
  155.         public InExcel()
  156.         {
  157.             //
  158.             // TODO: 在此处添加构造函数逻辑
  159.             //
  160.         }
  161.         /*************************************************************************************************************  
  162.                                              * Module: Excel表格导入
  163.                                              * Desc: 导入
  164.                                              * Author:徐秀梅
  165.                                              * Date: 2007-04
  166.         **************************************************************************************************************/
  167.         /// <summary>
  168.         /// 导入Excel表格原始数据
  169.         /// </summary>
  170.         /// <returns>返回一bool类型值</returns>
  171.         public string inexcel(string sFile)
  172.         {
  173.             string message = "";
  174.             //取出表格中数据放在数据集中
  175.             MSDA.Open();            
  176.             DataSet ds = new DataSet();
  177.             DataTable dt = new DataTable();            
  178.             string sql = "select * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source=" + sFile + ";User ID=Admin;Password=;Extended properties=Excel 8.0')...Sheet1$";
  179.             ds = MSDA.ExecuteDataset(CommandType.Text, sql, null, ds, "excel");
  180.             MSDA.Close();
  181.             dt = ds.Tables["excel"];
  182.             if (dt!=null||dt.Rows.Count > 0)
  183.             {
  184.                 //Excel表格存在数据就删除tb_sys_MatCode表的数据
  185.                 MSDA.Open();
  186.                 string delsql = "delete from tb_sys_MatCode";
  187.                 MSDA.ExecuteNonQuery(CommandType.Text, delsql, null);
  188.                 MSDA.Close();
  189.                 //向tb_sys_MatCode表写入Excel表格导入的数据
  190.                 for (int i =0; i <dt.Rows.Count; i++)
  191.                 {
  192.                     MatCode mymatcode = new MatCode();
  193.                     if (dt.Rows[i][0].ToString() == "")
  194.                     {
  195.                         message = "第" + (i+1) + "行物资代码不能为空!";
  196.                         return message;
  197.                     }                    
  198.                     string matcode = dt.Rows[i][0].ToString();
  199.                     DataView dv = mymatcode.MatCodeSearch(matcode, "1");
  200.                     if (dv.Count > 0)
  201.                     {
  202.                         message = "第" + (i + 1) + "行物资代码已经存在!";
  203.                         return message;
  204.                     }
  205.                     if (mymatcode.MatCodeCheck(matcode)==false)
  206.                     {
  207.                         message = "第" + (i + 1) + "行物资代码格式错误!";
  208.                         return message;
  209.                     }
  210.                     string matname = dt.Rows[i][2].ToString();
  211.                     string matdecs = dt.Rows[i][1].ToString();
  212.                     string insertsql = "insert into tb_sys_MatCode([MatCode],[MatName],[MatDecs]) values('"+matcode+"','"+matname+"','"+matdecs+"')";
  213.                     MSDA.Open();
  214.                     SqlTransaction tran = MSDA.BeginTransaction();
  215.                     try
  216.                     {
  217.                         MSDA.ExecuteNonQuery(CommandType.Text,insertsql,null);
  218.                         tran.Commit();                        
  219.                     }
  220.                     catch
  221.                     {
  222.                         tran.Rollback();
  223.                         throw;                       
  224.                     }
  225.                     finally
  226.                     {
  227.                         MSDA.Close();
  228.                     }
  229.                 }
  230.             }
  231.             return message;
  232.         }
  233.     }
  234. }