我想将我的excel文件保存在数据库中! ! ! [英] I wanna save my excel file in database! ! !

查看:91
本文介绍了我想将我的excel文件保存在数据库中! ! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public void Savefile()
{
//连接到Excel工作簿的字符串,将DataSource值替换为指向excel文件位置
string excelConnectionString =Provider = Microsoft.Jet.OLEDB.4.0;数据源= C:\\ cdr.xls;扩展属性= Excel 8.0;


//创建与Excel工作簿的连接
使用(OleDbConnection连接=
新OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand
(Select * FROM [Sheet1 $],连接);

connection.Open();

//使用(DbDataReader dr = command.ExecuteReader())创建DbDataReader到数据工作表
//编译器不会在这里继续处理
{
/ / SQL Server连接字符串
字符串sqlConnectionString =数据源= ACHRAF \\SQLEXPRESS;初始目录= PFEBD;集成安全性=真;

//批量复制到SQL Server
使用(SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName =CDR ;
bulkCopy.WriteToServer(dr);

}
}


}
}





我尝试过:



类型'System.Data.OleDb的未处理异常.OleDbException'发生在System.Data.dll 

解决方案

,连接);

connection.Open ();

//使用(DbDataReader dr = command.ExecuteReader())创建DbDataReader到数据工作表
//编译器不会在这里继续处理
{
// SQL Server连接字符串
字符串sqlConnectionString =数据源= ACHRAF \\SQLEXPRESS;初始目录= PFEBD;集成安全性=真;

//批量复制到SQL Server
使用(SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName =CDR;
bulkCopy.WriteToServer(dr);

}
}


}
}





我尝试过:



类型'System.Data.OleDb的未处理异常.OleDbException'发生在System.Data.dll 


Quote:

数据库引擎找不到'Sheet1


'。确保参数或别名有效,不包含不正确的字符或标点,并且它不会太长。



请检查您的Excel文件,然后查看你有什么床单。如果您已从默认的Sheet1重命名工作表,那么它将无法找到它,除非您更改SELECT语句以解决正确的工作表名称,并在其后面加上美元符号。



但请帮个忙:不要将数据存储在任何驱动器的根目录中,而且肯定不会存储在启动驱动器的根目录中。写入这些文件变得越来越难,因为它看起来像病毒活动。将您的数据存储在安全的地方 - 这可能有所帮助:我应该在哪里存储我的数据? [ ^ ]


public void Savefile()
       {
           // Connection String to Excel Workbook,Replace DataSource value to point to your excel file location
           string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\cdr.xls ;Extended Properties=Excel 8.0";


           // Create Connection to Excel Workbook
           using (OleDbConnection connection =
                        new OleDbConnection(excelConnectionString))
           {
               OleDbCommand command = new OleDbCommand
                       ("Select * FROM [Sheet1$]", connection);

               connection.Open();

               // Create DbDataReader to Data Worksheet
               using (DbDataReader dr = command.ExecuteReader())//the compiler won't continue processing here
               {
                   // SQL Server Connection String
                   string sqlConnectionString = "Data Source=ACHRAF\\SQLEXPRESS;Initial Catalog=PFEBD;Integrated Security=True";

                   // Bulk Copy to SQL Server
                   using (SqlBulkCopy bulkCopy =
                              new SqlBulkCopy(sqlConnectionString))
                   {
                       bulkCopy.DestinationTableName = "CDR";
                       bulkCopy.WriteToServer(dr);

                   }
               }


           }
       }



What I have tried:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

解决方案

", connection); connection.Open(); // Create DbDataReader to Data Worksheet using (DbDataReader dr = command.ExecuteReader())//the compiler won't continue processing here { // SQL Server Connection String string sqlConnectionString = "Data Source=ACHRAF\\SQLEXPRESS;Initial Catalog=PFEBD;Integrated Security=True"; // Bulk Copy to SQL Server using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString)) { bulkCopy.DestinationTableName = "CDR"; bulkCopy.WriteToServer(dr); } } } }



What I have tried:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll


Quote:

The database engine can not find 'Sheet1


'. Make sure that the parameter or alias name is valid, that it does not include an incorrect character or punctuation, and that it is not too long.


So check your Excel file, and look at what sheets you have. If you have renamed the sheet from the default "Sheet1" then it won't be able to find it unless you change your SELECT statement to address the right sheet name, with a dollar sign after it.

But do yourself a favour: don't store data in the root of any drive, and most certainly not in the root of your boot drive. It is becoming harder and harder to write to such files as it can look like virus activity. Store your data in a "safe place" - this may help: Where should I store my data?[^]


这篇关于我想将我的excel文件保存在数据库中! ! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆