C#利用反射将数据插入数据库

#region 插入记录
        public bool insertData(string sql,Object obj) 
        {
            try
            {
                adapter = new SqlDataAdapter(sql, connect);
                sqlCBuilder = new SqlCommandBuilder(adapter);
                ds = new DataSet();
                adapter.Fill(ds);
                DataRow dr = ds.Tables[0].NewRow();
                Type type = obj.GetType();
                PropertyInfo[] propertyArray = type.GetProperties();
                foreach (PropertyInfo field in propertyArray)
                {
                    Object returnValue= field.GetGetMethod().Invoke(obj, null);
                    string fieldName = PropertiesName(field.Name);
                    dr[fieldName] = returnValue;

                }
                ds.Tables[0].Rows.Add(dr);
                adapter.Update(ds);

                return true;
            }
            catch
            {
                return false;
            }
            finally 
            {
                if (connect.State == ConnectionState.Open)
                {
                    connect.Close();
                }
            }

        }
        #endregion

        #region 将类的公共属性名转换为数据库对应的字段名
        public string PropertiesName(string PropertyInfoName) 
        {
            string str = PropertyInfoName;
            if (str != null) 
            {
                string toLower = str.ToLowerInvariant();
                string first = toLower.Substring(0,1);
                str = first + str.Substring(1, str.Length - 2);

            }
            return str;
        }
        #endregion
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C#中将Excel数据写入数据库,可以按照以下步骤: 1. 安装Microsoft.Office.Interop.Excel和System.Data.SqlClient这两个NuGet包 2. 使用Microsoft.Office.Interop.Excel读取Excel数据 下面是一个读取Excel数据的示例代码: ```csharp Excel.Application excel = new Excel.Application(); Excel.Workbook workbook = excel.Workbooks.Open(filePath); Excel.Worksheet sheet = workbook.Worksheets[1]; int lastRow = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row; for (int i = 2; i <= lastRow; i++) { string name = sheet.Cells[i, 1].Value.ToString(); string age = sheet.Cells[i, 2].Value.ToString(); string gender = sheet.Cells[i, 3].Value.ToString(); // 将数据插入数据库中 } workbook.Close(); excel.Quit(); ``` 3. 使用System.Data.SqlClient将数据写入数据库 下面是一个将Excel数据写入到SQL Server数据库的示例代码: ```csharp string connectionString = "Data Source=.;Initial Catalog=TestDB;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand("INSERT INTO UserInfo(Name, Age, Gender) VALUES(@Name, @Age, @Gender)", connection); command.Parameters.AddWithValue("@Name", name); command.Parameters.AddWithValue("@Age", age); command.Parameters.AddWithValue("@Gender", gender); command.ExecuteNonQuery(); } ``` 在这个示例中,我们首先创建了一个连接字符串,指定了数据库的连接信息。然后,我们使用 SqlConnection 类打开数据库连接,并创建一个 SqlCommand 对象,用于执行 INSERT 语句将数据插入数据库中。最后,我们通过 AddWithValue 方法向 INSERT 语句中添加参数,并调用 ExecuteNonQuery 方法执行 INSERT 语句。 通过以上步骤,我们就可以将Excel数据写入到数据库中了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值