| asp.net实现将Excel文件导入到SQL数据库的三种解决方案 |
| 来源:
发布时间:2008-06-05 发布人:
浏览:
人次
字体:
[大
中
小]
|
|
//第三种方法: 直接通过SQL语句执行SQL Server的功能函数将Excel文件转换到SQL Server数据库 OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Excel files(*.xls)|*.xls"; SqlConnection sqlConnection1 = null; if(openFileDialog.ShowDialog()==DialogResult.OK) { string filePath = openFileDialog.FileName; sqlConnection1 = new SqlConnection(); sqlConnection1.ConnectionString = "server=(local);integrated security=SSPI;initial catalog=Library"; //import excel into SQL Server 2000 /*string importSQL = "SELECT * into live41 FROM OpenDataSource" + "('Microsoft.Jet.OLEDB.4.0','Data Source=" + "\"" + "E:\\022n.xls" + "\"" + "; User ID=;Password=; Extended properties=Excel 5.0')...[Sheet1$]";*/ //export SQL Server 2000 into excel string exportSQL = @"EXEC master..xp_cmdshell 'bcp Library.dbo.live41 out " + filePath + "-c -q -S" + "\"" + "\"" + " -U" + "\"" + "\"" + " -P" + "\"" + "\"" + "\'"; try { sqlConnection1.Open(); //SqlCommand sqlCommand1 = new SqlCommand(); //sqlCommand1.Connection = sqlConnection1; //sqlCommand1.CommandText = importSQL; //sqlCommand1.ExecuteNonQuery(); //MessageBox.Show("import finish!"); SqlCommand sqlCommand2 = new SqlCommand(); sqlCommand2.Connection = sqlConnection1; sqlCommand2.CommandText = exportSQL; sqlCommand2.ExecuteNonQuery(); MessageBox.Show("export finish!"); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } if(sqlConnection1!=null) { sqlConnection1.Close(); sqlConnection1 = null; }
共2页: 上一页 [1] 2 下一页
| |
| |
|
|
|
|
| §最新评论:(评论内容只代表网友观点,与本站立场无关!) | |
|
|
|
|
| 注意:请勿在本站发布政治话题、色情及违反法律的内容。 |
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。 |