C#中excel导入sql

using Microsoft.Office.Interop.Excel;

public int ledinExcel(string file, object sender, EventArgs e)
        {
            //try
            //{
                System.Data.DataTable dt = xsldata(file);
                int errorcount = 0;
                int insertcount = 0;
                //int updatecount = 0;

                //string strcon = "server=ROCKEN;database=checkatt;uid=sa;pwd=000123";
                //SqlConnection conn = new SqlConnection(strcon);
                //conn.Open();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into checkatt.DBO.att_table (Person_num,Per_name,Work_att,Up1,Off2,Up3,Off4,Up5,Off6)");
                    strSql.Append(" VALUES (@Person_num,@Per_name,@Work_att,@Up1,@Off2,@Up3,@Off4,@Up5,@Off6)");
                    SqlParameter[] praexcel = {
                                              new SqlParameter("@Person_num", SqlDbType.Int),
                                              new SqlParameter("@Per_name", SqlDbType.NVarChar,50),
                                              new SqlParameter("@Work_att", SqlDbType.Date),
                                              new SqlParameter("@Work_lable",  SqlDbType.NVarChar,50),
                                              new SqlParameter("@Up1", SqlDbType.Time),
                                              new SqlParameter("@Off2", SqlDbType.Time),
                                              new SqlParameter("@Up3", SqlDbType.Time),
                                              new SqlParameter("@Off4", SqlDbType.Time),
                                              new SqlParameter("@Up5", SqlDbType.Time),
                                              new SqlParameter("@Off6", SqlDbType.Time)};
                    praexcel[3].Precision = 0;
                    praexcel[4].Precision = 0;
                    praexcel[5].Precision = 0;
                    praexcel[6].Precision = 0;
                    praexcel[7].Precision = 0;
                    praexcel[8].Precision = 0;

                    praexcel[0].Value = dt.Rows[i][0];
                    praexcel[1].Value = dt.Rows[i][1].ToString();
                    praexcel[2].Value = Convert.ToDateTime(dt.Rows[i][2].ToString());
                    praexcel[3].Value = dt.Rows[i][3];
                    if (dt.Rows[i][4].ToString().Length == 0)
                    {
                        praexcel[4].Value = DBNull.Value;
                    }
                    else if (dt.Rows[i][4].ToString().Length > 5)
                    {
                        if (string.IsNullOrEmpty(dt.Rows[i][4].ToString().Trim()))
                        {
                            praexcel[4].Value = DBNull.Value;
                        }
                        else
                        {
                             praexcel[4].Value = dt.Rows[i][4].ToString().Trim();
                        }

                    }
                    else
                    {
                        praexcel[4].Value = dt.Rows[i][4];
                    }
                    //
                    if (dt.Rows[i][5].ToString().Length == 0)
                    {
                        praexcel[5].Value = DBNull.Value;
                    }
                    else if (dt.Rows[i][5].ToString().Length > 5)
                    {
                        if (string.IsNullOrEmpty(dt.Rows[i][5].ToString().Trim()))
                        {
                            praexcel[5].Value = DBNull.Value;
                        }
                        else
                        {
                             praexcel[5].Value = dt.Rows[i][5].ToString().Trim();
                        }
                    }
                    else
                    {
                        praexcel[5].Value = dt.Rows[i][5];
                    }
                    //
                    if (dt.Rows[i][6].ToString().Length == 0)
                    {
                        praexcel[6].Value = DBNull.Value;
                    }
                    else if (dt.Rows[i][6].ToString().Length > 5)
                    {
                        if (string.IsNullOrEmpty(dt.Rows[i][6].ToString().Trim()))
                        {
                            praexcel[6].Value = DBNull.Value;
                        }
                        else
                        {
                             praexcel[6].Value = dt.Rows[i][6].ToString().Trim();
                        }
                    }
                    else
                    {
                        praexcel[6].Value = dt.Rows[i][6];
                    }
                    //
                    if (dt.Rows[i][7].ToString().Length == 0)
                    {
                        praexcel[7].Value = DBNull.Value;
                    }
                    else if (dt.Rows[i][7].ToString().Length > 5)
                    {
                        if (string.IsNullOrEmpty(dt.Rows[i][7].ToString().Trim()))
                        {
                            praexcel[7].Value = DBNull.Value;
                        }
                        else
                        {
                             praexcel[7].Value = dt.Rows[i][7].ToString().Trim();
                        }
                    }
                    else
                    {
                        praexcel[7].Value = dt.Rows[i][7];
                    }
                    //
                    if (dt.Rows[i][8].ToString().Length == 0)
                    {
                        praexcel[8].Value = DBNull.Value;
                    }
                    else if (dt.Rows[i][8].ToString().Length > 5)
                    {
                        if (string.IsNullOrEmpty(dt.Rows[i][8].ToString().Trim()))
                        {
                            praexcel[8].Value = DBNull.Value;
                        }
                        else
                        {
                             praexcel[8].Value = dt.Rows[i][8].ToString().Trim();
                        }
                    }
                    else
                    {
                        praexcel[8].Value = dt.Rows[i][8];
                    }
                    //
                    if (dt.Rows[i][9].ToString().Length == 0)
                    {
                        praexcel[9].Value = DBNull.Value;
                    }
                    else if (dt.Rows[i][9].ToString().Length > 5)
                    {
                        if (string.IsNullOrEmpty(dt.Rows[i][9].ToString().Trim()))
                        {
                            praexcel[9].Value = DBNull.Value;
                        }
                        else
                        {
                             praexcel[9].Value = dt.Rows[i][9].ToString().Trim();
                        }
                    }
                    else
                    {
                        praexcel[9].Value = dt.Rows[i][9];
                    }
                    //if (dt.Rows[i][5].ToString().Length == 0) { praexcel[5].Value = DBNull.Value; } else { praexcel[5].Value = dt.Rows[i][5]; }
                    //if (dt.Rows[i][6].ToString().Length == 0) { praexcel[6].Value = DBNull.Value; } else { praexcel[6].Value = dt.Rows[i][6]; }
                    //if (dt.Rows[i][7].ToString().Length == 0) { praexcel[7].Value = DBNull.Value; } else { praexcel[7].Value = dt.Rows[i][7]; }
                    //if (dt.Rows[i][8].ToString().Length == 0) { praexcel[8].Value = DBNull.Value; } else { praexcel[8].Value = dt.Rows[i][8]; }
                    //if (dt.Rows[i][9].ToString().Length == 0) { praexcel[9].Value = DBNull.Value; } else { praexcel[9].Value = dt.Rows[i][9]; }
                    //praexcel[4].Value = dt.Rows[i][4];dt.Rows[i][9].ToString().Trim()
                    //praexcel[5].Value = dt.Rows[i][5];
                    //praexcel[6].Value = dt.Rows[i][6];
                    //praexcel[7].Value = dt.Rows[i][7];
                    //praexcel[8].Value = dt.Rows[i][8];
                    //praexcel[9].Value = dt.Rows[i][9];
                    //MessageBox.Show(dt.Rows[i][1].ToString() + dt.Rows[i][2].ToString());
                    try
                    {
                        if (SqlHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), praexcel) > 0)
                        {

                            insertcount++;

                        }
                        else
                        {
                            errorcount++;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("导入成功:" + insertcount.ToString() + "条数据;    共" + dt.Rows.Count.ToString() + "条记录");
                        return insertcount;
                    }
                }
                MessageBox.Show("导入成功:"+insertcount.ToString()+"条数据;    失败"+errorcount.ToString());
                return insertcount;
            //}
            //catch (Exception ex)
            //{
                //Console.Write(ex.Message);
            //    return insertcount;
            //}
        }
时间: 2024-08-24 08:20:19

C#中excel导入sql的相关文章

C#将SQL数据库中数据导入Excel中,并将Excel中反导入SQL数据库中

实际的开发中,我们会经常遇到数据的转化的需要,将Excel中的数据转入到SQL中,或将SQL在数据库表中的数据导入到Excel中.代码如下: Code using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windo

将Excel导入SQL Server 只能导入数字,其他数据变为NULL怎么解决?

先新建一个TXT文件,把数据粘贴进去 再新建一个Excel文件,在菜单栏中选Data再选From Text 找到txt文件,点import 一定要选Text 点Finish,点OK. 接下来在往数据库中导入 打开SQL Server Management Studio 在需要导入数据的数据库名上点击鼠标右键选Task,选Import Date 点Next Data Soure选Microsoft Excel file path选你的Excel文件 点Next Authentication中选Us

Java中Excel导入功能实现、excel导入公共方法_POI -

这是一个思路希望能帮助到大家:如果大家有更好的解决方法希望分享出来 公司导入是这样做的 每个到导入的地方 @Override public List<DataImportMessage> materialDataImport2(byte[] fileBytes, String fileName) { //return DataImport(fileBytes, fileName, "inv_m"); File file = FileUtils.getFileFromByte

php开发中Excel导入功能的具体实现方法

对于在做php开发项目中,一般的项目设计到最多用到的是Excel的导出功能,而对于其导入功能一般是很少见的,而且对于php开发中Excel的导入功能也要比导出功能开发起来更困难一些,那么今天就来分享一下php开发中Excel导入功能的具体实现步骤: 1.首先将下载下来的PHP Excel插件代码放入项目/Thinkphp/Extend/Vendor/下; 2.在模板添加导入功能; html代码实现如下:<form action="{:U('Turntable/imports')}"

Excel 导入 Sql Server出错——“文本被截断,或者一个或多个字符在目标代码页中没有匹配项”错误的解决

有人说应该先转成Access, 再转到Sql Server. 其实用处并不大, 要截断的还是被截断了. 原因是,SQL Server的导入导出为了确定数据表的字段类型,取excel文件的前8行来判别.如果前8条是少于255的字符串,则设成nvarchar(255)类型,但如果Excel后面的记录中有长度超过255的,导入时就会出错.即使在导入设置映射时调整了该字段的长度也不行. 解决的办法是改注册表 如果是:Jet引擎.HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\

MVC中Excel导入

1.在项目中添加对NPOI的引用,NPOI下载地址:http://npoi.codeplex.com/releases/view/38113. 前端代码 <div class="filebtn"> @using (Html.BeginForm("importexcel", "foot", FormMethod.Post, new { enctype = "multipart/form-data" })) { <

java中excel导入\导出工具类

1.导入工具 1 package com.linrain.jcs.test; 2 3 4 import jxl.Cell; 5 import jxl.Sheet; 6 import jxl.Workbook; 7 import jxl.write.Label; 8 import jxl.write.WritableSheet; 9 10 import java.io.InputStream; 11 import java.lang.reflect.Field; 12 import java.te

asp.net 中excel 导入数据库

protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LYConnectionString"].ConnectionString); ;//链接数据库 conn.Open(); try { string fileurl = t

MySQL导入.sql文件及常用命令

在MySQL Qurey   Brower中直接导入*.sql脚本,是不能一次执行多条sql命令的,在mysql中执行sql文件的命令: mysql> source   d:/myprogram/database/db.sql; 另附mysql常用命令: 一) 连接MYSQL: 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL 首先在打开DOS窗口,然后进入mysql安装目录下的bin目录下,例如: D:/mysql/bin,再键入命令mysql -