【求助】用c#将的Excel数据保存到ACCESS中
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中,不过不知道为什么存不上。
------解决方案--------------------这样如果行,那你告诉我。
你ACCESS中有没有同EXCEL中的SCHEMA?
否则你让DA如何update。
------解决方案--------------------Excel To Access,,,也只能做到这样
------解决方案--------------------
关注一下。
------解决方案--------------------
你把Excel中的数据每行按字段读出来,然后用insert语句插入数据库,这是一定可以的,只是有些麻烦
------解决方案--------------------
还有一个问题,首先是确认你的数据类型是对的,然后确认你Excel的数据在插入时符合数据完整性约束
比如你插入的字段是一个外键,而插入的数据不符合约束,是不可以的
我在C++里用Ado插入Sqlserver的时候就出了这问题,而且不会报错,很恶心