首页 > 数据库 >C# Sql多字段插入(反射!)

C# Sql多字段插入(反射!)

时间:2022-09-21 14:03:14浏览次数:84  
标签:par wbgzd C# properties item Sql new 多字段 string

 public bool Insert(object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties();
            string[] col = new string[properties.Length ];
            string[] par = new string[properties.Length ];
            string[] value = new string[properties.Length ];
            int count = 0;
            foreach (var item in properties)
            {
                if (item.Name!=null)
                {
                    col[count] = item.Name;
                    par[count] = "@"+item.Name;
                    if (item.GetValue(obj) != null)
                    {
                        value[count] = item.GetValue(obj).ToString();
                    }
                    else
                    {
                        value[count] = "-";
                    }
                    count++;
                }
            }
            string cols= string.Join(",", col);
            string parm = string.Join(",", par);
            string insert = "insert into wbgzd (" + cols + ") values ("+ parm +")";
            SqlParameter[] sqls = new SqlParameter[par.Count()];
            for (int i = 0; i < par.Count(); i++)
            {
                sqls[i] = new SqlParameter(par[i], value[i]);
            };
            Console.WriteLine(insert);
            return true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            wbgzd wbgzd = new wbgzd();
            Type type = wbgzd.GetType();
            PropertyInfo[] properties = type.GetProperties();
            foreach (var item in this.panel1.Controls)
            {
                if (item as TextBox!=null)
                {
                    foreach (var item2 in properties)
                    {
                        if (item2.Name.ToString()== (item as TextBox).Tag.ToString())
                        {
                            if ((item as TextBox).Text=="")
                            {
                                MessageBox.Show((item as TextBox).Name.ToString() + "不能为空!");
                                return;
                            }
                            else
                            {
                                item2.SetValue(wbgzd, (item as TextBox).Text.ToString());
                            }
                        }
                    }
                }
            }
            Console.WriteLine(wbgzd.gzID);
            Insert(wbgzd);
        }
View Code

 

标签:par,wbgzd,C#,properties,item,Sql,new,多字段,string
From: https://www.cnblogs.com/jjyy2008cn/p/16715330.html

相关文章

  • 在.net平台C#程序防止被反编译效果测试
    在.net平台C#程序是目前较常用的编程语言,但其易被反编译的特点,一直是一个难题。今天看到一个防止反编译利器—外壳加密工具。加密后的效果还是杠杠滴。(看说明,对java程......
  • LC1143 最长公共子序列
    intlongestCommonSubsequence(stringtext1,stringtext2){//dp[i][j]记录text1前i序列和text2前j序列的最长公共序列intdp[1005][1005];m......
  • oracle biee 日常运维,BIEE日志及几个重要的配置文件
    转至:https://blog.csdn.net/weixin_35717696/article/details/116386729BIEE目录有很多log日志,可以很方便的定位错误,分别为:1.BI的安装日志:~\OracleBI\log\install.log2.......
  • C++ time_t与格式化日期时间字符串的转换
    开发中对于特定格式的日期和格式获取很常见,这里给出一系列工具函数:#include<time.h>#include<sstream>usingnamespacestd;//time转格式化字符串=============......
  • mapreduce和yarn集群
    mapreduce: 先分再合,分而治之      分布式计算概念:计算方式,与集中式计算相对。将应用拆分成小的部分,分配给多台计算机处理,mapreduce是分布式的计算框架。......
  • MySQL查看数据库表容量大小
    1.查看所有数据库容量大小selecttable_schemaas'数据库',sum(table_rows)as'记录数',sum(truncate(data_length/1024/1024,2))as'数据容量(MB)',sum(truncate......
  • react hooks组件父组件调用子组件方法
    函数组件父组件调用子组件方法需要使用useImperativeHandle和forwardRef两个方法1.子组件    2.父组件 注意:一定要使用ref来接从子组件传过来的实例值,用......
  • IfcDimensionCount
    IfcDimensionCount类型定义IfcDimensionCount定义坐标空间的维度。在本规范中,尺寸限制为1、2或3。 注:分配给几何表示上下文的形状表示可能包括低维度的几何表示项目,特......
  • cesium教程7-官方示例模型要素选择翻译
     源代码示例:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>天地图</title><!--引用cesium的js和css,天地图的扩展js--><!--<s......
  • 实验2:Open vSwitch虚拟交换机实践
    (一)基本要求a)/home/用户名/学号/lab2/目录下执行ovs-vsctlshow命令、以及p0和p1连通性测试的执行结果截图;执行ovs-vsctlshow命令p0和p1连通性测试的执行结果截图......