日期:2014-05-20  浏览次数:20864 次

Excel导入到access
哪位高人救救我啊.
现在我把excel数据读到dataset里面,怎么操作才能把数据存到access数据库啊
今天搞了一天了.
先谢谢了

------解决方案--------------------
再开一个access连接, 从dataset中读出来,写到access数据库里面
------解决方案--------------------
OleDbConnection mycnn = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;data source=f:\\dazhu12.mdb ");
//新建dataset2,导入另外一个dataset中的数据
OleDbDataAdapter myada2 = new OleDbDataAdapter( "select * from t1 ", mycnn);
DataSet myds2 = new DataSet();
myada2.Fill(myds2, "tt ");

DataRow r = null;
for (int i = 0; i < myds.Tables[ "t "].Rows.Count; i++)
{
r = myds2.Tables[ "tt "].NewRow();
for (int j = 0; j < 5; j++)
{
r[j] = myds.Tables[ "t "].Rows[i][j].ToString();
}
myds2.Tables[ "tt "].Rows.Add(r);
}

this.dataGridView1.DataSource = myds2.Tables[ "tt "].DefaultView;
MessageBox.Show( "数据导入成功! ");

//更新数据库
OleDbCommand mycmd = new OleDbCommand( "insert into t1(ID,[m_id],[t_id],[order],topic) values(@1,@2,@3,@4,@5) ", mycnn);
mycmd.Parameters.Add( "@id1 ", System.Data.OleDb.OleDbType.Integer, 10, "ID ");
mycmd.Parameters.Add( "@m1 ", System.Data.OleDb.OleDbType.Integer, 10, "m_id ");
mycmd.Parameters.Add( "@t1 ", System.Data.OleDb.OleDbType.Integer, 10, "t_id ");
mycmd.Parameters.Add( "@o1 ", System.Data.OleDb.OleDbType.Integer, 10, "order ");
mycmd.Parameters.Add( "@t2 ", System.Data.OleDb.OleDbType.Char, 30, "topic ");

myada2.InsertCommand = mycmd;

myada2.Update(myds2, "tt ");
mycnn.Close();
MessageBox.Show( "数据更新成功! ");

------解决方案--------------------
如果你的数据库中的表有主键,那就简单多了...

用OleDbDataAdapter+OleDbCommandBuilder...再用DataAdapter.Update(DataTable)即可..