Best approach to update server db from local machine (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


DaveKstl -> Best approach to update server db from local machine (2/4/2008 9:28:03)

Periodically I receive a spreadsheet with the latest customer information. I need to use this data to update an access db on a server. What is a good approach to this update process?

The process I develop has to be something that an average computer skills person might be able to do. Presently I pull the file down, manipulate the access data on my machine and then push it up. Going forward I am looking to turn the db maintenance to someone else, writing an update query for existing records and a append or insert for the records that are new.

I would appreciate comments and thoughts.

I can link to the spreadsheet with an access file on a local machine, but I am hoping someone has already created something similar and can share their experience.

Dave




Spooky -> RE: Best approach to update server db from local machine (2/4/2008 13:48:04)

Id be wanting to perhaps have an upload routine for the csv to the server then allow the user to insert the csv into the current database?




rdouglass -> RE: Best approach to update server db from local machine (2/4/2008 13:51:48)

Is the spreadsheet just 1 worksheet? DO you have ASP.NET?

I have a .NET script I use that is straightforward in terms of dealing with a text file update source. It currently gets a tab-delimited text file (that can easily be a CSV file) and reads it into the TextStream object (.NET version of FSO's). Then runs a SQL connection and checks a current table. It then either UPDATE's or INSERT's based on whether their is a match or not.

I use 2 fields to determine a match and yours will probably be different. This is the full page and the exact code I use (with filenames and paths changed) and it's a page I execute as part of a daily updating sequence. Here it is if it helps any:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  sub Page_Load(sender as Object, e as EventArgs)
        Dim FILENAME As String = Server.MapPath("/mydownloads/download.txt")

        Dim filetoread As String
        filetoread = (FILENAME)
        Dim filestream As StreamReader
        filestream = File.OpenText(filetoread)
        Dim readcontents As String
        readcontents = filestream.ReadToEnd()
        Dim textdelimiter As String
        textdelimiter = Chr(10)
        Dim splitout = Split(readcontents, textdelimiter)

        filestream.Close()

        Dim sConnStr As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("SF").ConnectionString
        Dim cnBKTest As New SqlConnection(sConnStr)

        cnBKTest.Open()

        Dim strSQL As String = ""
        
        Dim i As Integer
        Dim j As Integer
        Dim myTempVal As Integer = 0
        Dim myTempString As String = ""
        
        For i = 1 To UBound(splitout) - 1

            myTempVal = 0

            Dim splitout2 = Split(splitout(i), vbTab)
            For j = 0 To UBound(splitout2)
                If splitout2(j).ToString & "" = "" Then
                    splitout2(j) = "0"
                End If
            Next
                            
            strSQL = "SELECT * FROM tlkp_NSD_Report  WHERE (ReportDate ='" & splitout2(0) & "') AND (AdvertiserID = " & splitout2(1) & ")"
 
            Dim cmdTest As New SqlCommand(strSQL, cnBKTest)
 
            cmdTest.CommandType = Data.CommandType.Text
            
            cmdTest.ExecuteScalar()
            
            Dim reader As SqlDataReader = cmdTest.ExecuteReader()

            If reader.Read() Then
                myTempVal = 1
            End If
            reader.Close()

            If myTempVal = 1 Then
                strSQL = "UPDATE tlkp_NSD_Report SET ReportDate = '" & splitout2(0) & "', AdvertiserID = " & splitout2(1) & ", AdvertiserName  = '" & splitout2(2) & "', AdvertiserCode  = '" & splitout2(3) & "', CampaignID = '" & splitout2(4) & "', CampaignName = '" & splitout2(5) & "', CampaignSpend = " & splitout2(6) & ", CampaignAdjustment = " & splitout2(7) & ", CampaignFees = " & splitout2(8) & ", Visits = " & splitout2(9) & ", Impressions  = " & splitout2(10) & ", Calls = " & splitout2(11) & ", Emails = " & splitout2(12) & ", Coupons = " & splitout2(13) & ", WebLinks = " & splitout2(14) & ", WebEvents  = " & splitout2(15) & ", LastUpdated = '" & splitout2(16) & "'  WHERE (ReportDate ='" & splitout2(0) & "') AND (AdvertiserID = " & splitout2(1) & ")"
            Else
                strSQL = "INSERT INTO tlkp_NSD_Report (ReportDate, AdvertiserID, AdvertiserName, AdvertiserCode, CampaignID, CampaignName, CampaignSpend, CampaignAdjustment, CampaignFees, Visits, Impressions, Calls, Emails, Coupons, WebLinks, WebEvents , LastUpdated) VALUES ('" & splitout2(0) & "'," & splitout2(1) & ",'" & splitout2(2) & "','" & splitout2(3) & "','" & splitout2(4) & "','" & splitout2(5) & "'," & splitout2(6) & "," & splitout2(7) & "," & splitout2(8) & "," & splitout2(9) & "," & splitout2(10) & "," & splitout2(11) & "," & splitout2(12) & "," & splitout2(13) & "," & splitout2(14) & "," & splitout2(15) & ",'" & splitout2(16) & "')"
            End If
            Dim cmdTest2 As New SqlCommand(strSQL, cnBKTest)

            myTempString = myTempString & i & strSQL & "<br />"
            cmdTest2.CommandType = Data.CommandType.Text
            cmdTest2.ExecuteScalar()
            
        Next
        lblOutput.Text = myTempString
        cnBKTest.Close()

    End Sub
</script>

<html>
<head>
<title>Updating NSD Records...</title>
</head>
<body>

<form id="Form1" runat="server">

<asp:label runat="server" id="lblOutput" />

</form>

</body>
</html>


So if something like this works, you'd essentially save the Excel file as a text doc(TSV or CSV) and upload it to the server and then kick off this page / script. Just remeber all bets are off if the Excel file has multiple worksheets.

Hope it helps.




rdouglass -> RE: Best approach to update server db from local machine (2/4/2008 13:53:49)

Sorry Spooky, the post been sittin' there for a while and of course you posted while I was typing....[8|] [;)]




Spooky -> RE: Best approach to update server db from local machine (2/4/2008 16:12:58)

Yours was a longer answer, Ill forgive you ;-)




DaveKstl -> RE: Best approach to update server db from local machine (2/11/2008 11:30:25)

I am not sure if we have a .net server. Is there a way to do this using asp?

Using Spooky's scenario, do I push the file to a folder on a server, open a connection then do an update?

Thanks

Dave





rdouglass -> RE: Best approach to update server db from local machine (2/11/2008 14:38:33)

You can use an Excel spreadsheet just like an Access DB by doing something like this for your ASP connection object:

myDSN = "DRIVER=Microsoft Excel Driver (*.xls); DBQ=" & Server.MapPath("/myExcelFiles/MySpreadsheet.xls")

Then (at least as far as I can see) it would involve:

1. Read the records into an array.
2. While loop thru the array, generate and execute a database UPDATE statement for each Excel record.

The trick is making sure you have an ID in each source to match against.

Some hints:

1. Don't use calculated fields. Do a SaveAs with just values if you have to.
2. Keep the spreadsheet simple. More worksheets just cause confusion to me and you want to start simple.
3. Make sure your column data is consistent. If you have dates or numbers, make sure *all* your column cells have valid data for the type in there. I've found this type of connection to an Excell SS is not very forgiving sometimes and very often difficult to troubleshoot so try to get rid of any data validation errors right up front.
4. Try not to use special characters as well. Same reasons as #3.

Hope that helps.

EDIT: Oh yeah, do that first:

quote:

do I push the file to a folder on a server




DaveKstl -> RE: Best approach to update server db from local machine (2/11/2008 17:26:13)

Thanks for the feedback

Dave




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.046875