当我从 Excel 电子表格导入数据时,我一直在尝试找到一种将数据类型字符串转换为 nvarchar 的方法。到目前为止,我有这段代码可以通过 C# 导入数据。
// 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:\\Folder\\Folder\\Excel_File.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())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=localhost;Initial Catalog=DatabaseName;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "Table_Name";
bulkCopy.WriteToServer(dr);
MessageBox.Show("Data Exported To Sql Server Successfully");
}
}
我一直在寻找从字符串转换为 nvarchar 的方法,但我发现的唯一示例仅用于转换从文本框或通过控制台收集的字符串。我确实有一个日期类型的列,但我已经在 MSSQL 中对其进行了补偿。
如果有人能给我一个我应该做什么的示例代码,将不胜感激。