• 大小: 49KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: C#
  • 标签: C#  

资源简介

以Excel表格形式导入到数据库,为此写了一个这样一个测试程序,当然,里面导入的Excel表格的字段、表名都可以自定义的,过程比较简单,主要用到了Sql Server和Excel的接口类库

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ExcelToSqlserver
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string _Constring = “SERVER=.;Database=szrunning;UID=sa;PWD=123“;
        private string openFilePath = ““;
        private void btnSelect_Click(object sender EventArgs e)
        {
            OpenFileDialog ofdOpenPath = new OpenFileDialog();
            ofdOpenPath.title = “打开Excel文件“;
            ofdOpenPath.Filter = “Excel文件(*.xls*.xlsx)|*.xls;*.xlsx;“;
            ofdOpenPath.Multiselect = false;
            if (ofdOpenPath.ShowDialog() == DialogResult.OK)
            {
                openFilePath = ofdOpenPath.FileName.ToString();
                this.txtExcelPath.Text = openFilePath.Replace(‘\\‘‘/‘);
            }
        }

        private void btnOk_Click(object sender EventArgs e)
        {
            string Msg = AddUserInfoFromExcel(txtExcelPath.Text);
            if (Msg.Equals(“SUCCESS“))
            {
                MessageBox.Show(“SUCCESS“);
                dataGridView1.DataSource = GetCustomerInfo();
            }
        }

        private string AddUserInfoFromExcel(string filePath)
        {
            string msg = ““;
            try
            {
                using (SqlConnection conn = new SqlConnection(_Constring))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(“ExcelImportCustomerInfo“ conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(“@FILEPATH“ SqlDbType.NVarChar 50).Value = filePath;
                    cmd.Parameters.Add(“@TABLENAME“ SqlDbType.NVarChar 50).Value = “CustomerInfo“;
                    cmd.Parameters.Add(“@RESULT“ SqlDbType.NVarChar 50).Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    msg = cmd.Parameters[“@RESULT“].Value.ToString();
                }
            }
            catch
            {
                msg = “ERROR“;
            }
            return msg;
        }

        private DataTable GetCustomerInfo()
        {
            DataTable dt=new DataTable();
            try
            {
                using (SqlConnection conn = new SqlConnection(_Constring))
                {
                    SqlDataAdapter sds = new SqlDataAdapter(“SELECT * FROM CustomerInfo“ conn);
                    sds.Fill(dt);
                }
            }
            catch
            {
                
            }
            return dt;
        }


        private void Form1_Load(object sender EventArgs e)
        {
            dataGridView1.DataSource = Ge

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-03-07 16:41  ExcelToSqlserver\
     目录           0  2012-03-09 11:16  ExcelToSqlserver\ExcelToSqlserver\
     文件         938  2012-03-07 16:41  ExcelToSqlserver\ExcelToSqlserver.sln
     文件       16384  2012-03-09 11:16  ExcelToSqlserver\ExcelToSqlserver.suo
     目录           0  2012-03-07 16:41  ExcelToSqlserver\ExcelToSqlserver\bin\
     目录           0  2012-03-07 16:47  ExcelToSqlserver\ExcelToSqlserver\bin\Debug\
     文件       11776  2012-03-09 11:00  ExcelToSqlserver\ExcelToSqlserver\bin\Debug\ExcelToSqlserver.exe
     文件       32256  2012-03-09 11:00  ExcelToSqlserver\ExcelToSqlserver\bin\Debug\ExcelToSqlserver.pdb
     文件       14328  2012-03-09 11:00  ExcelToSqlserver\ExcelToSqlserver\bin\Debug\ExcelToSqlserver.vshost.exe
     文件         490  2009-06-11 05:14  ExcelToSqlserver\ExcelToSqlserver\bin\Debug\ExcelToSqlserver.vshost.exe.manifest
     文件        4047  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\ExcelToSqlserver.csproj
     文件        4152  2012-03-09 11:16  ExcelToSqlserver\ExcelToSqlserver\Form1.cs
     文件        4576  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\Form1.Designer.cs
     文件        6017  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\Form1.resx
     文件         366  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\Form2.cs
     文件        1413  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\Form2.Designer.cs
     文件        5814  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\Form2.resx
     目录           0  2012-03-07 16:41  ExcelToSqlserver\ExcelToSqlserver\obj\
     目录           0  2012-03-09 11:00  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\
     文件        1176  2012-03-09 11:00  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\ExcelToSqlserver.csproj.FileListAbsolute.txt
     文件         905  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\ExcelToSqlserver.csproj.GenerateResource.Cache
     文件       11776  2012-03-09 11:00  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\ExcelToSqlserver.exe
     文件         180  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\ExcelToSqlserver.Form1.resources
     文件         180  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\ExcelToSqlserver.Form2.resources
     文件       32256  2012-03-09 11:00  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\ExcelToSqlserver.pdb
     文件         180  2012-03-08 17:32  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\ExcelToSqlserver.Properties.Resources.resources
     目录           0  2012-03-07 16:42  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\Refactor\
     目录           0  2012-03-07 16:41  ExcelToSqlserver\ExcelToSqlserver\obj\Debug\TempPE\
     文件         497  2012-03-07 16:41  ExcelToSqlserver\ExcelToSqlserver\Program.cs
     目录           0  2012-03-07 16:41  ExcelToSqlserver\ExcelToSqlserver\Properties\
     文件        1364  2012-03-07 16:41  ExcelToSqlserver\ExcelToSqlserver\Properties\AssemblyInfo.cs
............此处省略4个文件信息

评论

共有 条评论