[英]How to import data mocked into an Excel file into a MySql database table?
[英]How to import Excel file data into database?
我創建了一個表單,經理可以將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);
}
}
}
}
數據庫: 數據庫圖片
形式: 形式圖片
任何建議為什么我會出現此錯誤?
mscorlib.dll中發生類型為'System.ArgumentOutOfRangeException'的未處理異常
附加信息:索引超出范圍。 必須為非負數並且小於集合的大小。
您收到該錯誤的原因是,第一次打開頁面時,將運行加載功能。 您應該將其移到一個單獨的函數中,以便可以在選擇時調用它,也許可以單擊按鈕。 在將任何數據源應用到DataGridView之前。 您需要添加以下內容以確保表中有行。
i = dataGridView1.RowCount - 1;
if(i<0) // Meaning that the row count was 0
{
return;
}
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.