开发经验备忘

来源:互联网 发布:类似知北游走小说 编辑:程序博客网 时间:2024/04/27 21:20

1.系统资源文件损坏

找到资源文件损坏的语句,去掉有问题的资源文件的引用。

到:项目-》Properties-》Resources.resx 中删除报错的资源文件

重新生项目。在程序中重新引用资源文件及可。

 

/// <summary>        /// 将实体转换成具有相同结构的DataTable        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="model">要转换的实体</param>        /// <returns></returns>        public static DataTable ToDataTable<T>(T model)        {            //检查实体集合不能为空            if (model == null)            {                throw new Exception("需转换的集合为空");            }            //取出第一个实体的所有Propertie            Type entityType = model.GetType();            PropertyInfo[] entityProperties = entityType.GetProperties();            //生成DataTable的structure            //生产代码中,应将生成的DataTable结构Cache起来,此处略            DataTable dt = new DataTable();            for (int i = 0; i<entityProperties.Length; i++)            {                dt.Columns.Add(entityProperties[i].Name, entityProperties[i].GetType());            }            return dt;        }        


 

   /// <summary>        /// DataRow转化为实体        /// </summary>        /// <typeparam name="T">实体</typeparam>        /// <param name="pDataRow">DataRow</param>        /// <param name="model"></param>        /// <returns></returns>        public static T ConvertToModel<T>(DataRow pDataRow, T model)        {            object proValue = null;            PropertyInfo propertyInfo = null;            try            {                if (pDataRow != null)                {                    foreach (DataColumn dc in pDataRow.Table.Columns)                    {                        //忽略绑定时的大小写                        propertyInfo = model.GetType().GetProperty(dc.ColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);                        proValue = pDataRow[dc];                        //当值不为空时                        if (proValue != DBNull.Value)                        {                            try                            {   //给属性赋值                                propertyInfo.SetValue(model, Convert.ChangeType(proValue, dc.DataType), null);                            }                            catch //如果有错误,继续下一个属性的赋值                            {                                continue;                            }                        }                    }                }            }            catch            {                model = default(T);            }            return model;        }        /// <summary>        /// DataTable转化为泛型        /// </summary>        /// <typeparam name="T">实体</typeparam>        /// <param name="dt">DataTable</param>        /// <param name="model"></param>        /// <returns></returns>        public static List<T> ConvertToList<T>(DataTable dt, T model)        {            List<T> _list = new List<T>();            foreach (DataRow dr in dt.Rows)            {                _list.Add(ConvertToModel<T>(dr, model));            }            return _list;        }


 

 /// <summary>        /// 转换成半角字符(全角字符会转换成半角字符,半角字符会保持不变)        /// </summary>        /// <param name="input">要转换的字符串</param>        /// <returns>转换好的半角字符串</returns>        ///<remarks>        ///全角空格为12288,半角空格为32        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248        ///</remarks>        public static string ToDBC(string input)        {            char[] c = input.ToCharArray();            for (int i = 0; i < c.Length; i++)            {                if (c[i] == 12288)                {                    c[i] = (char)32;                    continue;                }                if (c[i] > 65280 && c[i] < 65375)                    c[i] = (char)(c[i] - 65248);            }            string str = c.ToString().Replace("。", ".");            return new string(c);        }


 

C# 遍历枚举值  

public void int()    {            foreach (int Code in Enum.GetValues(typeof(eErrorCode)))            {                string strName = Enum.GetName(typeof(eErrorCode), Code);//获取名称                string strVaule = Code.ToString();//获取值                ListItem myListItem = new ListItem(strName, strVaule);                ddlCategory.Items.Add(myListItem);//添加到DropDownList控件            }    }    public enum eErrorCode    {        登陆成功 = 0,        登出 = 1,    }
//绑定到列表
            comboBoxEdit会员状态.Properties.Items.AddRange(Enum.GetNames(typeof(MemberStatus)));

 

WCF共享实体类:

添加服务引用时,选择如下参数

当客户端引用不到实体类时:
删除D:\BeautySalonSolution\Bll\bin\Debug目录下的所有文件,然后再生成

用反射对实例进行赋值:

 bool result = false;                    object ojb;                    ojb = dataReader["值"];                    if (ojb != null && ojb != DBNull.Value)                    {                        if (Convert.ToInt32(ojb) == 1)                        {                            result = true;                        }                    }                    PropertyInfo propertyInfo = model.GetType().GetProperty(dataReader["名称"].ToString(), BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);                    propertyInfo.SetValue(model, result, null);


 当编辑项目遇到错误:LX.exe 退出,返回-1。引用了第三方控件,错误的强签名引起的错误。

删除编辑失败项目下的:项目\Properties\licenses.licx 文件

重新添加dll,重新生成项目。

       //从此程序集加载指定的清单资源     System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("GridTutorials.GridMasterDetail.Parents.xml"); 

利用反射,遍历实例中的所有属性

 private void LoadEmployeePerformance(Guid p_销售单明细Id,SalesOrderDetailsInfo salesOrderDetailsInfo)        {            salesOrderDetailsInfo.EmployeePerformanceInfoList.Clear();            List<EmployeePerformanceInfo> _List= m_EmployeePerformance.GetModelList(p_销售单明细Id);            for(int i=0;_List.Count<i;i++)            {                EmployeePerformanceInfo model = _List[i];                Type type = model.GetType();//得到实例的类型                foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))//遍历实例的所有Public属性                {                    object value1 = pi.GetValue(model, null);//得到属性在实例中的值                    string name = pi.Name;//得到实例的在类中的名称                    if (value1.GetType() == typeof(decimal))//判断属性的类型                    {                        if (Convert.ToDecimal(value1) != 0)                        {                            pi.SetValue(model, -Convert.ToDecimal(value1), null);                        }                    }                }                salesOrderDetailsInfo.EmployeePerformanceInfoList.Add(model);            }        }
DevExpress.XtraTreeList.Nodes 设置数据源后不会立即绑定,所以导致节点为null值。
 /// <summary>        /// 设置当前焦点        /// </summary>        /// <param name="keyId">主键字段的值</param>        /// <returns></returns>        public TreeListNode SetFocusedNode(object keyId)        {            treeList1.BeginUpdate();            treeList1.ExpandAll();            //TreeList设置了数据源,但是节点并没有实时绑定,所以还需执行以上两步操作            TreeListNode node = treeList1.FindNodeByKeyID(keyId);            treeList1.CollapseAll();            treeList1.EndUpdate();            string a =  treeList1.Nodes[0].Nodes.Count.ToString();            if (node != null)            {                treeList1.SetFocusedNode(node);                m_SelectedNode = node;                popupContainerEdit1.Text = node[DisplayFieldName].ToString();            }            return node;        }

2.访问生成操作为“内容”的文件(选择文件->属性->生成操作=内容,复制到输出目录=如果较新则复制),例如数据文件,报表模板等

文件路径:Path.Combine(Application.StartupPath,@("目录\文件名.扩展名")),如无目录,直接输入文件名即可。

此类文件发布时:应用程序文件-》包括(自动)



3.云服务器放在网上容易受到攻击,一般把SQL的sa用户名禁止掉,把Windows防火墙开启,如无远程连接数据库就不要打开1433端口。

4.网站测试时,可以通过“SqlServer外围配置器”打开允许远程连接,同时在Win防火墙中打开1433商品,测试完成后,要逆向操作,以便给黑客机会。

5.在利用EF开发时,更新生产数据库,先执行条目4,使程序能够访问到远程数据,然后在VS的PM中执行:PM->update-database, 迁移成功后,记得执行4的后半部分。


当添加现有窗体时,出现 Form 文件 .Desiner.cs、.resx 三个文件分离

先添加.Desiner.cs、.resx两个文件,然后再添加Form文件,当添加Form文件时系统询问是否替换选择“是”

执行事务语句时超时

出现错误原因:是在事务语句块中执行了查询语句

解决方法:把查询语句提取到事务之外执行,即事务执行之前




 

原创粉丝点击