如何将Excel文件数据导入数据库?

时间:2015-11-17 23:13:10

标签: database excel datagridview import

我创建了一个表单,管理员可以将Excel文件导入DataGridView,但我无法将其保存到数据库中。

原因:因此当经理将rota Excel文件导入DataGridView时,我希望其他用户以不同的形式查看它。 我的表单代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace Login
{
    public partial class EmployeeRota : Form
    {
        string con = "Data Source=dqq5ndqef2.database.windows.net;Initial Catalog=Login;Integrated Security=False;User ID=richardjacobs97;Password=;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";

        SqlDataAdapter sda;
        SqlCommandBuilder scb;
        DataTable dt;
        public EmployeeRota()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string PathCpnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + textBox1.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
            OleDbConnection conn = new OleDbConnection(PathCpnn);

            OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + textBox2.Text + "$]", conn);
            DataTable dt = new DataTable();
            myDataAdapter.Fill(dt);
            dataGridView1.DataSource = dt;
            myDataAdapter.Update(dt);
        }

        private void EmployeeRota_Load(object sender, EventArgs e)
        {
            string connectionString = con;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO Rota (Id, Name, Date) Values (@Id, @Name, @Date)");
                cmd.CommandType = CommandType.Text;
                cmd.Connection = connection;
                int i = 0;
                i = dataGridView1.RowCount - 1;
                cmd.Parameters.AddWithValue("@Id", dataGridView1.Rows[i].Cells[0].Value);
                cmd.Parameters.AddWithValue("@Name", dataGridView1.Rows[i].Cells[1].Value);
                cmd.Parameters.AddWithValue("@Date", dataGridView1.Rows[i].Cells[0].Value);
                dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.DataSource = dt;

                scb = new SqlCommandBuilder(sda);
                sda.Update(dt);
            }
        }
    }
}

数据库:Database pic

Database pic

表格:Form pic

Form pic

为什么我收到此错误的任何建议?

  

mscorlib.dll中出现未处理的“System.ArgumentOutOfRangeException”类型异常

     

其他信息:指数超出范围。必须是非负数且小于集合的大小。

1 个答案:

答案 0 :(得分:0)

您收到该错误,因为首次打开页面时会运行“加载”功能。您应该将其移动到单独的函数中,以便可以在选定的时间调用它,也可以通过单击按钮来调用它。这是在应用于任何数据源之前 DataGridView的。您需要添加以下内容以确保表中有行。

jQuery.extend