private 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); //用bcp导入数据
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))
{
bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 1;//每次传输的行数
bcp.NotifyAfter = 1;//进度提示的行数
bcp.DestinationTableName = "Staff_Msg";//目标表
bcp.WriteToServer(ds.Tables[0]);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
} } //进度显示
void bcp_SqlRowsCopied(object sender, System.Data.SqlClient.SqlRowsCopiedEventArgs e)
{
this.Text = e.RowsCopied.ToString();
this.Update();
} private void btnInput_Click(object sender, EventArgs e)
{
string p = IniReadValue("StandIni", "ExportStaffForPath", configPath);
TransferData(p,"Sheet1",sql);
}数据库中只有1列【年龄】是int类型,还有两个是bit类型。Excel里的年龄数据我用过了数值类型,也加过了'.还是提示这个错误,请各位大侠帮我看看是什么原因?小弟初来CSDN,分数不多,请帮帮忙!
{
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); //用bcp导入数据
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))
{
bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 1;//每次传输的行数
bcp.NotifyAfter = 1;//进度提示的行数
bcp.DestinationTableName = "Staff_Msg";//目标表
bcp.WriteToServer(ds.Tables[0]);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
} } //进度显示
void bcp_SqlRowsCopied(object sender, System.Data.SqlClient.SqlRowsCopiedEventArgs e)
{
this.Text = e.RowsCopied.ToString();
this.Update();
} private void btnInput_Click(object sender, EventArgs e)
{
string p = IniReadValue("StandIni", "ExportStaffForPath", configPath);
TransferData(p,"Sheet1",sql);
}数据库中只有1列【年龄】是int类型,还有两个是bit类型。Excel里的年龄数据我用过了数值类型,也加过了'.还是提示这个错误,请各位大侠帮我看看是什么原因?小弟初来CSDN,分数不多,请帮帮忙!
解决方案 »
- 退出对话框
- 有没有专门编辑“时间”的控件呀,在文本框中修改时间,写错了不好检测
- 如何在后台把Repeater的某一列的特定的值传给一个变量?
- C#2.0连接sql server2000怪事!
- 跨线程全局变量怎样递增?
- 使用Dirctory.Move(dir1,dir2)?问题?
- 关于线程的问题,Monitor和Mutex的具体差别是什么??看书没看懂。
- VS2005与Server2003操作系统的兼容问题
- OleDbTransaction 对象的Begin();方法报错,不知为什么
- 请问C#中form怎么动态添加一个DATAVIEW及绑定它
- WPF在添加进度条控件后,出现如下异常
- C#.NET Compact Framework 绘图出现的一个很无语的简单问题
单元格数据是否存在问题
根据
的话,原来我DataTable里的字段和数据库里的字段位置不是完全一一对应的哦(我也完全没有想到必须要位置一一对应,以为只要 列名 完全相同就好)
哎,走了很多弯路,不过解决了就好,嘿嘿~