我想将 excel 表数据上传到 sql sever 2008,我的所有数据,即我的 excel 表中的数字和字母表都将上传到数据库表中,但 $ 符号不会上传到那里,因为我必须使用货币作为一列,$sign 将上传文本数据但不上传数字.....请让我知道这个问题
这是我的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;
using System.Configuration;
using System.IO;
using log4net.Config;
using log4net;
namespace ExcelUpload
{
class Program
{
static ILog log = LogManager.GetLogger(typeof(Program));
//Variable declarations
public static string strSqlConnection, strExcelDataQry, strSqlTable, strExcelFilePath, sexcelconnectionstring;
public static int intRows;
static void Main(string[] args)
{
#region GET PARAMS FROM CONFIG FILE
strSqlConnection = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString();
strExcelFilePath = ConfigurationManager.AppSettings["ExcelFileName"].ToString();
string[] sqlSheets = ConfigurationManager.AppSettings["Sheets"].Split(',');
#endregion
#region SET CONNECTIONS
sexcelconnectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelFilePath + ";Extended Properties='Excel 12.0 xml;HDR=YES;'";
SqlConnection Sqlconn = new SqlConnection(strSqlConnection);
SqlBulkCopy bulkcopy = new SqlBulkCopy(strSqlConnection);
OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
#endregion
XmlConfigurator.Configure();
log4net.ThreadContext.Properties["Context"] = "ExcelUpload";
try
{
log.Info("Started Execution of ExcelUpload Process");
foreach (string sqlSheetName in sqlSheets)
{
#region GET PARAMS FROM CONFIG FILE BASED ON CURRENT SHEET
strExcelDataQry = "select " + ConfigurationManager.AppSettings[sqlSheetName + "ColumnNames"] + " from [" + sqlSheetName + "$]";
strSqlTable = ConfigurationManager.AppSettings[sqlSheetName + "Table"];
intRows = Convert.ToInt32(ConfigurationManager.AppSettings[sqlSheetName + "RowsToExclude"].ToString());
#endregion
#region TRUNCATE TABLE
SqlCommand SqlCommand = new SqlCommand("TRUNCATE TABLE " + strSqlTable, Sqlconn);
Sqlconn.Open();
SqlCommand.ExecuteNonQuery();
Sqlconn.Close();
log.Info("Table " + strSqlTable + " Truncated Successfully");
#endregion
#region BULK COPY DATA
log.Info("Started processing the sheet " + sqlSheetName);
OleDbCommand oledbcmd = new OleDbCommand(strExcelDataQry, oledbconn);
oledbconn.Open();
DataSet ds = new DataSet();
using (OleDbDataAdapter adapter = new OleDbDataAdapter(oledbcmd))
{
adapter.Fill(ds);
}
for (int iRow = 0; iRow < intRows; iRow++)
{
ds.Tables[0].Rows[iRow].Delete();
}
ds.Tables[0].AcceptChanges();
foreach (DataRow dr in ds.Tables[0].Rows)
{
foreach (DataColumn col in ds.Tables[0].Columns)
{
if (col.DataType == typeof(System.String))
{
dr[col] = dr[col].ToString().Trim();
}
}
}
bulkcopy.DestinationTableName = strSqlTable;
bulkcopy.WriteToServer(ds.Tables[0]);
oledbconn.Close();
log.Info("Sheet " + sqlSheetName + " successfully loaded to the table " + strSqlTable);
#endregion
}
log.Info("ExcelUpload Process completed successfully");
}
catch (System.Exception ex)
{
log.Info("Error while processing :- " + ex.Message);
}
finally
{
Sqlconn.Close();
oledbconn.Close();
}
}
}
}