如标题所示,openFileDialog选择.xls文件导入到.mdb中,字段名和数据格式都已经事先设置好,第一次导入没有问题,xls增添新内容后继续导入到Access时总是提示“标准表达式中数据类型不匹配”,我的SQL几乎一窍不通,代码如下,各位大德帮我看看问题在哪里:
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conExcel = new OleDbConnection();
try
{
OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框。
openFile.Filter = ("Excel 文件(*.xls)|*.xls");//后缀名。
if (openFile.ShowDialog() == DialogResult.OK)
{
string filename = openFile.FileName;
int index = filename.LastIndexOf("\\");//截取文件的名字
filename = filename.Substring(index + 1);
conExcel.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Application.StartupPath + @"\自己test.mdb"; string sql = "insert into table1 select distinct * from [Excel 8.0;database=" + filename + "].[Sheet1$] where 序号 not IN (select 序号 from table1)";
OleDbCommand com = new OleDbCommand(sql, conExcel);
conExcel.Open();
com.ExecuteNonQuery();
MessageBox.Show("导入数据成功", "导入数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conExcel.Close();
}
万分感激!!!!
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conExcel = new OleDbConnection();
try
{
OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框。
openFile.Filter = ("Excel 文件(*.xls)|*.xls");//后缀名。
if (openFile.ShowDialog() == DialogResult.OK)
{
string filename = openFile.FileName;
int index = filename.LastIndexOf("\\");//截取文件的名字
filename = filename.Substring(index + 1);
conExcel.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Application.StartupPath + @"\自己test.mdb"; string sql = "insert into table1 select distinct * from [Excel 8.0;database=" + filename + "].[Sheet1$] where 序号 not IN (select 序号 from table1)";
OleDbCommand com = new OleDbCommand(sql, conExcel);
conExcel.Open();
com.ExecuteNonQuery();
MessageBox.Show("导入数据成功", "导入数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conExcel.Close();
}
万分感激!!!!
http://www.cnblogs.com/cnzc/archive/2009/03/25/22787.html
我也知道我的问题应该是insert语句的事,可是你给我的网页我还是看不懂
string sql = "insert into table1 select distinct * from [Excel 8.0;database=" + filename + "].[Sheet1$] where 序号 not IN (select 序号 from table1)";
到底错在哪里呢?
为了测试简单,我的xls文件只有两个字段,一个序号一个姓名,都设成文本格式,mdb中也是同样设置,所以第一次直接导入没有问题,满帆就在后面继续导入xls中新增的记录时报错,各位帮忙啊~~~~
还有可能是主键问题...
一是下载频道不能评分了,这两天一百多分花没了啊,心疼啊~~~~~
二是上传文件之后啥也看不见,隔天才能看到;
三就是上面说的不能回复自己帖子——现在已经好了管理员来解决一下啊,到底是要升级?要改版?中毒了?还是怎么地?
实在不行先回复以前的功能再说啊