??????oledb?????excel???????????sqlserver?У?????????????????sql??????????????????????Ч????£?????????? bcp?????System.Data.SqlClient.SqlBulkCopy ?????????????????????????????????????????6??????????sheet??????????????????????????????????????????10?????????????????????????4.5??
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender?? EventArgs e)
{
//???????excel?е?sheet1????sqlserver??
string connString = "server=localhost;uid=sa;pwd=sqlgis;database=master";
System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
TransferData(fd.FileName?? "sheet1"?? connString);
}
}
public void TransferData(string excelFile?? string sheetName?? string connectionString)
{
DataSet ds = new DataSet();
try
{
//??????????
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$]"?? sheetName);
myCommand = new OleDbDataAdapter(strExcel?? strConn);
myCommand.Fill(ds?? sheetName);