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.OleDb; namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnImportExcel_Click(object sender, EventArgs e)
{
ImportExcel();
} public void ImportExcel()
{
openFileDialog1.Filter = "*.XLS | *.XLS";
openFileDialog1.Title = "请选择要导入的电子电子表格";
if (DialogResult.OK == openFileDialog1.ShowDialog(this))
{
//创建一个连接字符串 ......
string connectionString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;",
openFileDialog1.FileName);
//创建一个连接
OleDbConnection newConnection = new OleDbConnection(connectionString);
newConnection.Open();
string strExcel = "select * from 偏差报告";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, connectionString);
DataSet ds = new DataSet();
myCommand.Fill(ds, "偏差报告");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;
newConnection.Close(); string cs = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;",
Application.StartupPath + ".//1.mdb");
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from 偏差报告", cs);
da.Fill(ds);
da.Update(ds.Tables["偏差报告"]);
conn.Close();
}
} }
}
以上是我的代码,我的想法是将Excel中的数据保存到数据集ds中(这步已经成功了),再将ds中的数据保存到Access中,不过不知道为什么存不上。
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.OleDb; namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnImportExcel_Click(object sender, EventArgs e)
{
ImportExcel();
} public void ImportExcel()
{
openFileDialog1.Filter = "*.XLS | *.XLS";
openFileDialog1.Title = "请选择要导入的电子电子表格";
if (DialogResult.OK == openFileDialog1.ShowDialog(this))
{
//创建一个连接字符串 ......
string connectionString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;",
openFileDialog1.FileName);
//创建一个连接
OleDbConnection newConnection = new OleDbConnection(connectionString);
newConnection.Open();
string strExcel = "select * from 偏差报告";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, connectionString);
DataSet ds = new DataSet();
myCommand.Fill(ds, "偏差报告");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;
newConnection.Close(); string cs = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;",
Application.StartupPath + ".//1.mdb");
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from 偏差报告", cs);
da.Fill(ds);
da.Update(ds.Tables["偏差报告"]);
conn.Close();
}
} }
}
以上是我的代码,我的想法是将Excel中的数据保存到数据集ds中(这步已经成功了),再将ds中的数据保存到Access中,不过不知道为什么存不上。
你ACCESS中有没有同EXCEL中的SCHEMA?
否则你让DA如何update。
我也不知道这样行不行。
Access中表的结构和Excel的相同。如果这种方法不行,请告知有没有可行的方法?
我是想将Excel表读入到Access的表“偏差报告”中。
{
openFileDialog1.Filter = "*.XLS ¦ *.XLS";
openFileDialog1.Title = "请选择要导入的电子电子表格";
if (DialogResult.OK == openFileDialog1.ShowDialog(this))
{
//创建一个连接字符串 ......
string connectionString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;",
openFileDialog1.FileName);
//创建一个连接
OleDbConnection newConnection = new OleDbConnection(connectionString);
newConnection.Open();
string strExcel = "select * from 偏差报告";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, connectionString);
DataSet ds = new DataSet();
myCommand.Fill(ds, "偏差报告");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;
newConnection.Close();
string cs = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;",
Application.StartupPath + ".//1.mdb");
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from 偏差报告", cs); //加上这句你试试吧
OleDbCommandBuilder a = OleDbCommandBuilder (da) ;
da.Fill(ds);
da.Update(ds.Tables["偏差报告"]);
conn.Close();
}
}
应该是OleDbCommandBuilder a =new OleDbCommandBuilder (da)吧,也不行!
比如你插入的字段是一个外键,而插入的数据不符合约束,是不可以的我在C++里用Ado插入Sqlserver的时候就出了这问题,而且不会报错,很恶心