Here is a brute force Console Application module that will load Categories and Products into your Store from a standard Excel spreadsheet.
WARNING: DO NOT USE ON AN EXISTING STORE - THIS CODE IS FOR TESTING PURPOSES ONLY
In a nutshell, here is how it works...
1) Deletes Existing Records - in the proper order due to table constraints.
2) Export Category List - finds distinct (unique) categories and puts them into the database.
3) Loads the category IDs - from the newly ineserted data
4) Imports rows from Excel - one at a time, looking up the CategoryID for each Product insert
A few more notes:
The first row of the Excel spreadsheet must contain the fieldnames of the columns. They don't have to match the Store_Products table, but you have to modify the necessary transforms.
The SqlQuote function simply changes single quotes ' to double single quotes '' and puts single quotes around strings or dates.
Uncomment DeleteExistingData to run the program over and over again until you get it right.
You can also uncomment the ReadKey to pause for a keypress when an exception occurs.
Enjoy!
Imports
System.Data.OleDb
Imports System.Data.SqlClient
Module
Sandbox
'Excel stuff
Dim DataSource As String = "..\..\..\products.xls"
Dim Provider As String = "Microsoft.Jet.OLEDB.4.0;Extended Properties=" & """Excel 8.0;HDR=YES;""" 'don't ask
Dim xlsConn As New OleDbConnection
Dim xlsComm As OleDbCommand
'SQL Stuff
Dim sqlConn As New SqlConnection
Dim sqlComm As SqlCommand
Dim sql As String
'DNN Stuff
Dim SiteSqlServer As String = "Server=sqlserver;Database=sqldatabase;uid=sqluser;pwd=sqlpassword;"
Dim objectQualifier As String = "" 'use as required
'Other stuff
Dim PortalID As Integer = 0 'important in actual use
Dim CategoryIDs As New Collection
Sub Main()
xlsConn.ConnectionString = String.Format("Data Source={0};Provider={1}", DataSource, Provider)
xlsConn.Open()
sqlConn.ConnectionString = SiteSqlServer
sqlConn.Open()
'DeleteExistingData() 'very useful for repetitive testing
ImportCategories(ExportCategories()) 'save categories
LoadCategoryIDs()
ImportProducts(ExportProducts) 'save products
End Sub
'deletes all information before processing
Public Sub DeleteExistingData()
ExecuteNonQuery(String.Format("delete from {0}Store_CartItems", objectQualifier))
ExecuteNonQuery(String.Format("delete from {0}Store_Products", objectQualifier))
ExecuteNonQuery(String.Format("delete from {0}Store_Categories", objectQualifier))
End Sub
'export unique category list
Function ExportCategories() As OleDbDataReader
Dim sql As String = "select distinct category from [sheet1$] group by category"
Return New OleDbCommand(sql, xlsConn).ExecuteReader
End Function
'import category list into Store_Categories
Sub ImportCategories(ByVal dr As OleDbDataReader)
While dr.Read
sql = String.Format("insert into {0}Store_Categories (PortalID,CategoryName) Values ({1},'{2}')", objectQualifier, PortalID, dr("Category"))
ExecuteNonQuery(sql)
End While
End Sub
'load collection with category names and ids
Sub LoadCategoryIDs()
Dim sql As String = String.Format("select categoryid,categoryname from {0}Store_Categories where PortalID={1}", objectQualifier, PortalID)
Dim cat As SqlDataReader = New SqlCommand(sql, sqlConn).ExecuteReader
While cat.Read
If cat(1).ToString <> "" Then CategoryIDs.Add(cat(0), cat(1))
End While
End Sub
'export all products in spreadsheet
Function ExportProducts() As OleDbDataReader
sql = "select * from [sheet1$]"
Return New OleDbCommand(sql, xlsConn).ExecuteReader
End Function
'import all products into Store_Products
Sub ImportProducts(ByVal dr As OleDbDataReader)
While dr.Read
ImportProduct(dr)
End While
End Sub
'import single product into Store_Products
Sub ImportProduct(ByVal dr As OleDbDataReader)
Dim fields As String = "CategoryID,PortalID,Manufacturer,ModelNumber,ModelName,ProductImage," & _
"UnitCost,Summary,Description,Featured,Archived,CreatedByUser,CreatedDate"
Dim values As New ArrayList
Try
values.Add(CategoryIDs(dr("Category"))) 'lookup category ID
values.Add(PortalID)
values.Add(SqlQuote(dr("Manufacturer")))
values.Add(SqlQuote(dr("ModelNumber")))
values.Add(SqlQuote(dr("ModelName")))
values.Add(SqlQuote(dr("ProductImage")))
values.Add(dr("UnitCost"))
values.Add(SqlQuote(dr("Summary")))
values.Add(SqlQuote(dr("Description")))
values.Add(1) 'IIf(dr("Featured"), 1, 0))
values.Add(0) 'IIf(dr("Archived"), 1, 0))
values.Add(SqlQuote("System")) 'should be UserID
values.Add(SqlQuote(Now))
sql = String.Format("insert into {0}Store_Products ({1}) values ({2})", objectQualifier, fields, Join(values.ToArray, ","))
Console.WriteLine(dr("ModelNumber"))
ExecuteNonQuery(sql)
Catch ex As Exception
Console.WriteLine(ex.Message)
'Console.ReadKey() 'uncomment to pause on errors during processing
End Try
End Sub
'sql helper function
Function ExecuteNonQuery(ByVal sql As String) As Integer
Return New SqlCommand(sql, sqlConn).ExecuteNonQuery
End Function
'fixup single quotes and add quotes around value (required for String and DateTime values)
Function SqlQuote(ByVal value As String) As String
Return "'" & Replace(value, "'", "''") & "'"
End Function
End Module