Dataset not updating to Excel
I"m completely stumped. I've looked here: http://www.connectionstrings.com/excel-2007. Changed my connectionstring appropriately. My dataset is fills with data. The Excel file already exists, but has nothing but a Header row. I can't understand why the dataset can't push the values to Excel. The only thing it's doing is inserting a bunch of rows. There's nothing so far indicates something is wrong. After two days of banging my head against this, I've come for your sage advice.
Protected Sub loadResxToExcel()
Dim connString As String = ConfigurationManager.ConnectionStrings("xlsx").ConnectionString
Dim oledbConn As OleDbConnection = New OleDbConnection(connString)
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn)
Dim updCmd As OleDbCommand = New OleDbCommand("UPDATE [Sheet1$] SET [Control] = @updCtrl, [Text] = @updText, [Comment] = @updCmnt, [English - en-US] = @updEngl, [Spanish - es-MX] = @updSpnh, [German - de] = @updGemn WHERE [ID] = @oldID", oledbConn)
Dim insCmd As OleDbCommand = New OleDbCommand("INSERT INTO [Sheet1$] ([ID], [Control], [Text], [Comment], [English - en-US], [Spanish - es-MX], [German - de]) VALUES (@id, @ctrl, @text, @cmnt, @engl, @spnh, @gemn)", oledbConn)
Dim updParamArray As OleDbParameter() = { _
New OleDbParameter("@updID", OleDbType.LongVarChar), _
New OleDbParameter("@updctrl", OleDbType.LongVarChar), _
New OleDbParameter("@updText", OleDbType.LongVarChar), _
开发者_StackOverflow New OleDbParameter("@updCmnt", OleDbType.LongVarChar), _
New OleDbParameter("@updEngl", OleDbType.LongVarChar), _
New OleDbParameter("@updSpnh", OleDbType.LongVarChar), _
New OleDbParameter("@updGemn", OleDbType.LongVarChar) _
}
Dim insParamArray As OleDbParameter() = { _
New OleDbParameter("@id", OleDbType.LongVarChar), _
New OleDbParameter("@ctrl", OleDbType.LongVarChar), _
New OleDbParameter("@text", OleDbType.LongVarChar), _
New OleDbParameter("@cmnt", OleDbType.LongVarChar), _
New OleDbParameter("@engl", OleDbType.LongVarChar), _
New OleDbParameter("@spnh", OleDbType.LongVarChar), _
New OleDbParameter("@gemn", OleDbType.LongVarChar) _
}
For Each oleParam As OleDbParameter In insParamArray
oleParam.Value = ""
Next
For Each oleParam As OleDbParameter In updParamArray
oleParam.Value = ""
Next
insCmd.Parameters.AddRange(insParamArray)
updCmd.Parameters.AddRange(updParamArray)
Dim oleda As OleDbDataAdapter = New OleDbDataAdapter()
oleda.SelectCommand = cmd
oleda.InsertCommand = insCmd
oleda.UpdateCommand = updCmd
Dim ds As DataSet = New DataSet()
Try
If Not xmlOriginFile Is Nothing Then
Dim fileNav As XPathNavigator = xmlOriginFile.CreateNavigator()
Dim fileItr As XPathNodeIterator = fileNav.Select("//data")
Dim childNav As XPathNavigator
' Open connection
oledbConn.Open()
oleda.Fill(ds)
ds.Tables(0).TableName = "Sheet1$"
Dim pKeys(1) As DataColumn
pKeys(0) = ds.Tables(0).Columns("ID")
ds.Tables(0).PrimaryKey = pKeys
Dim idnum As Integer = 1
While (fileItr.MoveNext())
Dim showCtrlName As String = fileItr.Current.GetAttribute("name", "")
Dim showDesc As String = String.Empty
Dim showLineNum As String = String.Empty
If fileItr.Current.HasChildren() Then
childNav = fileItr.Current
If childNav.MoveToChild("value", "") Then
showDesc = childNav.Value
End If
If childNav.MoveToNext("comment", "") Then
showLineNum = childNav.Value
End If
End If
Dim addRow As DataRow = ds.Tables(0).NewRow()
addRow.ItemArray() = New Object() {idnum, showCtrlName, showDesc, showLineNum, showDesc, String.Empty, String.Empty}
ds.Tables(0).Rows.Add(addRow)
idnum += 1
End While 'loop thru nodes
oleda.Update(ds, "Sheet1$")
End If
Catch ex As Exception
Finally
' Close connection
oledbConn.Close()
End Try
End Sub
<connectionStrings>
<add name="xls" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Sample1.xls;Extended Properties=Excel 8.0"/>
<add name="xlsx" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\cgramont\My Documents\Visual Studio 2008\WebSites\ResourceChanges\TU1371_v12.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES;'"/>
</connectionStrings>
What I did to finally get this to work.
- Change the OleDbParameter of "ID" from a LongVarChar to Integer
- Added size and sourceColumn to all parameters
- Commented out the assignments to values
Afterwards, it all seemed to work. Code changes:
Dim updParamArray As OleDbParameter() = { _
New OleDbParameter("@updID", OleDbType.Integer, 32, "ID"), _
New OleDbParameter("@updctrl", OleDbType.LongVarChar, 32, "Control"), _
New OleDbParameter("@updText", OleDbType.LongVarChar, 256, "Text"), _
New OleDbParameter("@updCmnt", OleDbType.LongVarChar, 32, "Comment"), _
New OleDbParameter("@updEngl", OleDbType.LongVarChar, 256, "English - en-US"), _
New OleDbParameter("@updSpnh", OleDbType.LongVarChar, 256, "Spanish - es-MX"), _
New OleDbParameter("@updGemn", OleDbType.LongVarChar, 256, "German - de") _
}
Dim insParamArray As OleDbParameter() = { _
New OleDbParameter("@id", OleDbType.Integer, 32, "ID"), _
New OleDbParameter("@ctrl", OleDbType.LongVarChar, 32, "Control"), _
New OleDbParameter("@text", OleDbType.LongVarChar, 256, "Text"), _
New OleDbParameter("@cmnt", OleDbType.LongVarChar, 32, "Comment"), _
New OleDbParameter("@engl", OleDbType.LongVarChar, 256, "English - en-US"), _
New OleDbParameter("@spnh", OleDbType.LongVarChar, 256, "Spanish - es-MX"), _
New OleDbParameter("@gemn", OleDbType.LongVarChar, 256, "German - de") _
}
'insParamArray(0).Value = 0
'For i As Integer = 1 To (insParamArray.Length - 1)
' insParamArray(i).Value = ""
'Next
'updParamArray(0).Value = 0
'For i As Integer = 1 To (updParamArray.Length - 1)
' updParamArray(i).Value = ""
'Next
精彩评论