I am importing excel file into Sql Server database using a Console application script.
private static void ImportToSql(string ssqlconnectionstring, string query)
{
string query = "MERGE Inventory AS target USING (select LocalSKU,ItemName, QOH,ISNULL(Price,0.00),Discontinued,Integer2,Integer3 from @source) as source ON (source.LocalSKU = target.LocalSKU) WHEN MATCHED THEN UPDATE SET ItemName=source.ItemName,Price=source.Price,Discontinued=source.Discontinued,Integer2=source.Integer2,Integer3=source.QOH;";
string excelfilepath = Environment.CurrentDirectory + @"\Sheet1.csv";
// make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have different
// string myexceldataquery = "select LocalSKU,ItemName,QOH,Price,Discontinued,Barcode,Integer2,Integer3,SalePrice,SaleOn,Price2 from [sheet1$]";
string myexceldataquery = "select LocalSKU,ItemName, QOH,Price,Discontinued,Integer2,Integer3 from [sheet1$]";
try
{
// string sexcelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelfilepath + ";Extended Properties=Excel 12.0;";
string sexcelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + excelfilepath + "; Extended Properties=\"Excel 12.0; HDR=Yes; IMEX=2; ImportMixedTypes=Text;\"";
//string ssqlconnectionstring = "Data Source=User-PC\\Dell;Trusted_Connection=True;DATABASE=TestStore;CONNECTION RESET=FALSE";
SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
//series of commands to bulk copy data from the excel file into our sql table
OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
oledbconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlCommand sqlcmd = new SqlCommand(@query, sqlconn);
SqlParameter param;
param = sqlcmd.Parameters.AddWithValue("@source", dr);
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.DTTOW";
sqlconn.Open();
sqlcmd.ExecuteNonQuery();
sqlconn.Close();
oledbconn.Close();
Console.WriteLine(".xlsx file imported succssessfully into database. /r /n ");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
This code works when I select only two columns from excel sheet , LocalSKU and QOH
When I select few more columns , I get this error. "Error converting data type nvarchar to numeric."
I am using User defined datatypes :
CREATE TYPE [dbo].[DTTOW] AS TABLE(
[LocalSKU] [varchar](200) NOT NULL,
[ItemName] [varchar](200) NULL,
[QOH] [int] NULL,
[Price] [decimal](19, 4) NULL,
[Discontinued] [bit] NULL,
[Integer2] [int] NULL,
[Integer3] [int] NULL
)