R
Rod
I am struggling with ado.net within an asp.net project.
I want to import an Excel spreadsheet and write it to a temporary table on a
SQL Server from where it can be manipulated.
I can import the Excel file to a dataset successfully but cannot move the
data to the SQL Server.
My attempts have been focussed round:
creating a SQL Server table based on the structure of the
dataset.datatable, (this has been successful),
using a dataadapter to create a new dataset based on the newly created
SQL Server table, (this has been apparently successful),
merging the new dataset with the one imported from Excel, (this has
been successful),
using dataadapter.Update(dataset) to write the data back to the SQL
Server. (This is a failure.)
I have tried to use the command builder to construct the insert command but
at the point at which the update should occur the insert sqlcommand is
nothing.
The code I use to create the SQL Server table and attempt to write the data
are below.
Any help would be greatly appreciated, including (especially) alternatives
to my idea.
Thanks in anticipation.
Rod Scoullar.
** Create Table Function
** The function accepts a DataTable object and the name of the SQL Server
table to be created
** It returns True if the table was successfully created
Private Function CreateSQLServerTable(ByVal tbl As System.Data.DataTable,
ByVal strTableName As String) As Boolean
Dim blnRVal As Boolean = False
' Drops table if it already exists
Try
Dim strSQL As String = "DROP TABLE " & strTableName
conn.Open() ' The connection is declared elsewhere
Dim cmd As System.Data.SqlClient.SqlCommand = conn.CreateCommand()
cmd.CommandText = strSQL
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
Catch e As Exception
System.Diagnostics.Debug.WriteLine(e.ToString())
Finally
conn.Close()
End Try
' Builds CREATE TABLE statement
Try
Dim strSQL As String = "CREATE TABLE " & strTableName & " (ID integer
IDENTITY (1,1) PRIMARY KEY"
Dim strFlds As String = ""
Dim strType As String
Dim fld As System.Data.DataColumn
tbl = dsSSData.Tables("Table")
For Each fld In tbl.Columns
Select Case fld.DataType.Name
Case "String"
Dim intFldLen As Integer = 1
Dim row As System.Data.DataRow
For Each row In tbl.Rows
If Not IsDBNull(row(fld)) Then
If row(fld).length() > intFldLen Then intFldLen = row(fld).length()
End If
Next
strType = "varchar(" & CStr(intFldLen) & ")"
Case "Double"
strType = "float"
Case Else
strType = fld.DataType.Name
End Select
strFlds &= ", [" & fld.ColumnName & "] " & strType & " NULL"
Next
strSQL &= strFlds & ")"
' Creates table
conn.Open()
Dim cmd As System.Data.SqlClient.SqlCommand = conn.CreateCommand()
cmd.CommandText = strSQL
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
blnRVal = True
Catch e As Exception
System.Diagnostics.Debug.WriteLine(e.ToString())
Finally
conn.Close()
End Try
Return (blnRVal)
End Function
** Fill Table Function
** The function accepts a DataTable object and the name of the SQL Server
table to be filled
Private Function FillSQLServerTable(ByVal tbl As System.Data.DataTable,
ByVal strTableName As String) As Boolean
Dim blnRVal As Boolean = False
Try
conn.Open()
Dim strSQL As String = "SELECT * FROM " & strTableName
Dim ds As New System.Data.DataSet
Dim da As New System.Data.SqlClient.SqlDataAdapter
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
da.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, conn)
Dim cb As System.Data.SqlClient.SqlCommandBuilder = New
System.Data.SqlClient.SqlCommandBuilder(da)
da.Fill(ds)
ds.Merge(tbl)
ds.AcceptChanges()
da.Update(ds)
blnRVal = True
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.ToString())
Finally
conn.Close()
End Try
Return(blnRVal)
End Function
This function returns True even though no data has been written.
I want to import an Excel spreadsheet and write it to a temporary table on a
SQL Server from where it can be manipulated.
I can import the Excel file to a dataset successfully but cannot move the
data to the SQL Server.
My attempts have been focussed round:
creating a SQL Server table based on the structure of the
dataset.datatable, (this has been successful),
using a dataadapter to create a new dataset based on the newly created
SQL Server table, (this has been apparently successful),
merging the new dataset with the one imported from Excel, (this has
been successful),
using dataadapter.Update(dataset) to write the data back to the SQL
Server. (This is a failure.)
I have tried to use the command builder to construct the insert command but
at the point at which the update should occur the insert sqlcommand is
nothing.
The code I use to create the SQL Server table and attempt to write the data
are below.
Any help would be greatly appreciated, including (especially) alternatives
to my idea.
Thanks in anticipation.
Rod Scoullar.
** Create Table Function
** The function accepts a DataTable object and the name of the SQL Server
table to be created
** It returns True if the table was successfully created
Private Function CreateSQLServerTable(ByVal tbl As System.Data.DataTable,
ByVal strTableName As String) As Boolean
Dim blnRVal As Boolean = False
' Drops table if it already exists
Try
Dim strSQL As String = "DROP TABLE " & strTableName
conn.Open() ' The connection is declared elsewhere
Dim cmd As System.Data.SqlClient.SqlCommand = conn.CreateCommand()
cmd.CommandText = strSQL
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
Catch e As Exception
System.Diagnostics.Debug.WriteLine(e.ToString())
Finally
conn.Close()
End Try
' Builds CREATE TABLE statement
Try
Dim strSQL As String = "CREATE TABLE " & strTableName & " (ID integer
IDENTITY (1,1) PRIMARY KEY"
Dim strFlds As String = ""
Dim strType As String
Dim fld As System.Data.DataColumn
tbl = dsSSData.Tables("Table")
For Each fld In tbl.Columns
Select Case fld.DataType.Name
Case "String"
Dim intFldLen As Integer = 1
Dim row As System.Data.DataRow
For Each row In tbl.Rows
If Not IsDBNull(row(fld)) Then
If row(fld).length() > intFldLen Then intFldLen = row(fld).length()
End If
Next
strType = "varchar(" & CStr(intFldLen) & ")"
Case "Double"
strType = "float"
Case Else
strType = fld.DataType.Name
End Select
strFlds &= ", [" & fld.ColumnName & "] " & strType & " NULL"
Next
strSQL &= strFlds & ")"
' Creates table
conn.Open()
Dim cmd As System.Data.SqlClient.SqlCommand = conn.CreateCommand()
cmd.CommandText = strSQL
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
blnRVal = True
Catch e As Exception
System.Diagnostics.Debug.WriteLine(e.ToString())
Finally
conn.Close()
End Try
Return (blnRVal)
End Function
** Fill Table Function
** The function accepts a DataTable object and the name of the SQL Server
table to be filled
Private Function FillSQLServerTable(ByVal tbl As System.Data.DataTable,
ByVal strTableName As String) As Boolean
Dim blnRVal As Boolean = False
Try
conn.Open()
Dim strSQL As String = "SELECT * FROM " & strTableName
Dim ds As New System.Data.DataSet
Dim da As New System.Data.SqlClient.SqlDataAdapter
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
da.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, conn)
Dim cb As System.Data.SqlClient.SqlCommandBuilder = New
System.Data.SqlClient.SqlCommandBuilder(da)
da.Fill(ds)
ds.Merge(tbl)
ds.AcceptChanges()
da.Update(ds)
blnRVal = True
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.ToString())
Finally
conn.Close()
End Try
Return(blnRVal)
End Function
This function returns True even though no data has been written.