using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.OleDb;
using System.Web;
using System.Configuration;
using System.Data;
using System.Collections;
using System.Windows.Forms;
//using System.Data.ConnectionState;
namespace QuanLiDSSV
{
public partial class Frm2 : Form
{
public Frm2()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog sh = new OpenFileDialog();
sh.FileName = txtDuongDanFile.Text;
if (sh.ShowDialog() == DialogResult.OK)
{
txtDuongDanFile.Text = sh.FileName;
}
//File Names, replae with your file names
string fileNameExcel = @"e:\luan van\DSSV.xls";
string fileNameAccess = @"e:\luan van\quanlythuhocphi.mdb";
//Connection string for Excel
string connectionStringExcel =
string.Format("Data Source= {0};Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;", fileNameExcel);
//Connection string for Access
string ConnectionStringAccess =
string.Format("Data Source= {0}; Provider=Microsoft.Jet.OLEDB.4.0; Persist security Info = false", fileNameAccess);
//Connection object for Excel
OleDbConnection connExcel = new OleDbConnection(connectionStringExcel);
//Connection object for Access
OleDbConnection connAccess = new OleDbConnection(ConnectionStringAccess);
//Command object for Excel
OleDbCommand cmdExcel = connExcel.CreateCommand();
cmdExcel.CommandType = CommandType.Text;
cmdExcel.CommandText = "SELECT * FROM [Sheet1$]";
//Command object for Access
OleDbCommand cmdAccess = connAccess.CreateCommand();
cmdAccess.CommandType = CommandType.Text;
cmdAccess.CommandText = "INSERT INTO quanlythuhocphi (MaSV, Ho, Ten, Nganh hoc, Khoa hoc) VALUES(@Mã SV, @Họ, @Tên, @Ngành học, @Khóa học)";
//Add parameter to Access command object
OleDbParameter param1 = new OleDbParameter("@Mã SV", OleDbType.VarChar);
cmdAccess.Parameters.Add(param1);
OleDbParameter param2 = new OleDbParameter("@Họ", OleDbType.VarChar);
cmdAccess.Parameters.Add(param2);
OleDbParameter param3 = new OleDbParameter("@Tên", OleDbType.VarChar);
cmdAccess.Parameters.Add(param3);
OleDbParameter param4 = new OleDbParameter("@Ngành học", OleDbType.VarChar);
cmdAccess.Parameters.Add(param4);
OleDbParameter param5 = new OleDbParameter("@Khóa học", OleDbType.Integer);
cmdAccess.Parameters.Add(param5);
//Open connections
connExcel.Open();
connAccess.Open();
//Read Excel
OleDbDataReader drExcel = cmdExcel.ExecuteReader();
while (drExcel.Read())
{
//Assign values to access command parameters
param1.Value = drExcel[0].ToString();
param2.Value = drExcel[1].ToString();
param3.Value = drExcel[2].ToString();
param4.Value = drExcel[3].ToString();
param5.Value = drExcel[4].ToString();
//Insert values in access
cmdAccess.ExecuteNonQuery();
}
//close connections
connAccess.Close();
connExcel.Close();
}
}
}
Have a error at "insert into" but i don't find.
Please help me!
Thank u!