I'm currently working on a application in VB.NET 2003, with a SQL Server backend.
I was asked to make an import function that would allow a user to import an Excel worksheet into SQL Server. I'm aware you can do it from Excel directly to SQL Server, but the clients would like something that wouldn't involve the users of this application touching the SQL Server.
I'm using an OLEDB for the Excel connection, and a ODBC connection for the SQL Server connection.
Next, I have a button event that would open up a OpenFileDialog window, and a user can choose an Excel sheet. If the Excel sheet is valid, it would go on extracting the data into a dataset:
ds =
New DataSetds.Clear()
strSQL = "Select HEAT, Practice, C, Mn, P, S, Si, Cu, Ni, Cr, Mo, V, " & _
"Cb, Ti, Al, B, N, Ca, Sn from [Ladle Chems$]"
sqlDA = New OleDbDataAdapter(strSQL, con)
sqlDA.Fill(ds)
ds1 = New DataSet
Once that dataset is filled with the excel data, I fill another dataset with the table that data will go into (this is a connection to the SQL Server Database)
strSQL = "Select HeatNumber, SteelPractice, C, Mn, P, S, Si, Cu, Ni, Cr, Mo, V, " & _
"Cb, Ti, Al, B, N, Ca, Sn from tblElements"
sqlDAODBC = New OdbcDataAdapter(strSQL, conn)
sqlDAODBC.TableMappings.Add("Table", "tblElements")
sqlDAODBC.Fill(ds1)
Now with both datasets filled, I will merge them. They both have the same number of columns, and the Primary key in the SQL Server database is indexed (that's why you won't see the Primary key)
ds1.Merge(ds, True)
I then put the data from that merge into a Datatable. So far, this is getting me the exact number of rows so far. The SQL Table gives me 48 rows, and the excel sheet gives me 654 rows.
Dim table As DataTable
table = ds1.Tables(0) 'This has 654 rows
Now i check to see if the table is Null or has nothing in it. Finally, I update that table and accept the changes.
If (Not table Is System.DBNull.Value) And Not table Is Nothing Then
sqlDAODBC.Update(table)
ds1.Tables(0).AcceptChanges()
End If
After that procedure is done, i populate a datagrid (for testing purposes), with a SQL statement that counts the number of rows in the table (I should see 48+654= 702 rows), but I see 48 rows. What am I doing wrong

Datasets and SQL Server
XNA Rockstar
hiphop_fc
Johan Nordin
Im also facing the same problem, while importing excel data into sql server. i m getting the excel data into oledb dataset(as the connection i use is oledb, to import from excel). then i am using dataadapter's update method to insert ds into sql database. My code is..
Dim
conn As New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source='" & t1.Text & "';Extended properties=Excel 8.0;")Dim
conn1 As New SqlConnection("server=VANDY0;uid=sa;pwd=sa;database=master;")conn.Open()
str = "select * from [Sheet1$]"
da = New OleDbDataAdapter(str, conn)ds =
New System.Data.DataSetda.Fill(ds)
DataGrid1.DataSource = ds
''uptill this it is wrking fine
''inserting ds into sql table login in databse name masterconn1.Open()
Dim drow As DataRowdrow = dt.NewRow
drow("usr") = ds
drow("pass") = ds
dt.Rows.Add(drow)
da.Update(ds, "login")
IT is giving me error that colummn name "usr" does not belong to table.... Pls help..
Thanks
Green Apple
enric vives
Im also facing the same problem, while importing excel data into sql server. i m getting the excel data into oledb dataset(as the connection i use is oledb, to import from excel). then i am using dataadapter's update method to insert ds into sql database. My code is..
Dim
conn As New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source='" & t1.Text & "';Extended properties=Excel 8.0;")Dim
conn1 As New SqlConnection("server=VANDY0;uid=sa;pwd=sa;database=master;")conn.Open()
str = "select * from [Sheet1$]"
da = New OleDbDataAdapter(str, conn)ds =
New System.Data.DataSetda.Fill(ds)
DataGrid1.DataSource = ds
''uptill this it is wrking fine
''inserting ds into sql table login in databse name masterconn1.Open()
Dim drow As DataRowdrow = dt.NewRow
drow("usr") = ds
drow("pass") = ds
dt.Rows.Add(drow)
da.Update(ds, "login")
IT is giving me error that colummn name "usr" does not belong to table.... Pls help..
Thanks
Arindam Biswas
I tried this out and I was having difficulty. I'm using VS2003 (VB.NET), and I don't see the .SetAdded Method. I tried to look this up in the VS2003 Help, and couldn't find it. Sorry if I'm being a pain with this.
The approach that I took before you posted, was this:
Dim
intCounter As Integer Dim table As DataTable = New DataTable("test") Dim tblColumn As DataColumntblColumn =
New DataColumn("HeatNumber")table.Columns.Add(tblColumn)
tblColumn =
New DataColumn("SteelPractice")table.Columns.Add(tblColumn)
tblColumn =
New DataColumn("C")table.Columns.Add(tblColumn)
For intCounter = 0 To ds1.Tables(0).Rows.Count - 1
tblRow = ds1.Tables("dataSet").Rows(intCounter)
table.Rows.Add(tblRow)
Debug.WriteLine(table.Rows(intCounter).RowState)
NextBut this didn't work either.
EDIT: I got it to work, I made another dataset and just added the rows from the dataset with the SQL Query into the newly created Dataset. I appreciate all the help, you put me in the right direction.
goonfro
Waiman Li
gloriousgopi
not fully understand your approach. However, if I were to solve the problem, I would simply change the rowstate in ds:
foreach (DataRow dr in ds.Tables[0].Rows) {
dr.SetAdded();
}
Then merge it to ds1.
Mathew1972
you must write DATABIND() in end of your Code.
mammoo
yavi
Markus Sch.
Amos Soma