Save a Datatable to SQL

I am trying to save the results of the following datatable to SQL. Can anyone assist in how I would handle?

Thanks

DT as datatable = New DataTable() 
DT.Columns.Add(New DataColumn("SI_EMAIL_ADDRESS")) 
    'DT.Columns.Add(New DataColumn("SI_FORCE_PASSWORD_CHANGE")) 
DT.Columns.Add(New DataColumn("SI_NAME")) 
DT.Columns.Add(New DataColumn("SI_ID")) 
DT.Columns.Add(New DataColumn("SI_USERGROUPS")) 
DT.Columns.Add(New DataColumn("SI_USERFULLNAME")) 
DT.Columns.Add(New DataColumn("SI_ALIASES")) 
DT.Columns.Add(New DataColumn("SI_DESCRIPTION")) 
    'DT.Columns.Add(New DataColumn("NTServiceName")) 
DT.Columns.Add(New DataColumn("SI_LASTLOGONTIME")) 
    'DT.Columns.Add(New DataColumn("SI_PASSWORDEXPIRE")) 
For Each oInfoObject As InfoObject In oInfoObjects 
    DR = oDT.NewRow() 
    DR("EMAIL_ADDRESS") = oInfoObject.Properties("SI_EMAIL_ADDRESS").ToString() 
    DR("NAME") = oInfoObject.Properties("SI_NAME").ToString() 
    DR("ID") = oInfoObject.Properties("SI_ID").ToString() 
    DR("USERGROUPS") = oInfoObject.Properties("SI_USERGROUPS").ToString() 
    DR("USERFULLNAME") = oInfoObject.Properties("SI_USERFULLNAME").ToString() 
    DR("ALIASES") = oInfoObject.Properties("SI_ALIASES").ToString() 
    DR("DESCRIPTION") = oInfoObject.Properties("SI_DESCRIPTION").ToString() 
    DR("LASTLOGONTIME") = oInfoObject.Properties("SI_LASTLOGONTIME").ToString() 
    'oDR("NTServiceName") = oInfoObject.Properties("SI_NTSERVICE_NAME").ToString() 
    'oDR("RegisterToAPS") = oInfoObject.Properties("SI_REGISTER_TO_APS").ToString() 
    'oDR("ServerDescriptor") = oInfoObject.Properties("SI_SERVER_DESCRIPTOR").ToString() 
    DT.Rows.Add(DR) 
Next

Open in new window

earngreenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David L. HansenCEOCommented:
Let me know if you need more help.  I know a single link may not always give you what you need.
0
earngreenAuthor Commented:
I think that I am going to need a little more help. I don't think that the example will work.
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

David L. HansenCEOCommented:
Are you trying to update an existing table in your database or create a completely new one based on the datatable?
0
earngreenAuthor Commented:
update an existing table
0
David L. HansenCEOCommented:
Basically you need an object that knows how to connect to your database and has the know-how for updating, inserting, deleting, etc. rows from your database tables.  Just such an object comes prepackaged in .NET, see below for a good example (note if you just want UPDATE then just use the code at the beginning under UPDATE):

'-------------------------------------------------------------------
'UPDATE
 
Dim da As SqlDataAdapter = New SqlDataAdapter()
  Dim cmd As SqlCommand
  Dim parm As SqlParameter
 
  ' Create the SelectCommand.
 
  cmd = New SqlCommand("SELECT * FROM Customers " & _
                       "WHERE Country = @Country AND City = @City", conn)
 
  cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15)
  cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15)
 
  da.SelectCommand = cmd
 
  ' Create the UpdateCommand.
 
  cmd = New SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
                       "WHERE CustomerID = @oldCustomerID", conn)
 
  cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
  cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
 
  parm = cmd.Parameters.Add("@oldCustomerID", SqlDbType.NChar, 5, "CustomerID")
  parm.SourceVersion = DataRowVersion.Original
 
  da.UpdateCommand = cmd
 
'-----------------------------------------------------------------------
'Insert
 
Dim da As SqlDataAdapter = New SqlDataAdapter()
  Dim cmd As SqlCommand
 
  ' Create the SelectCommand.
 
  cmd = New SqlCommand("SELECT * FROM Customers " & _
                       "WHERE Country = @Country AND City = @City", conn)
 
  cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15)
  cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15)
 
  da.SelectCommand = cmd
 
  ' Create the InsertCommand.
 
  cmd = New SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _
                       "VALUES (@CustomerID, @CompanyName)", conn)
 
  cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
  cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
 
  da.InsertCommand = cmd
 
 
'------------------------------------------------------------------------------------
'Delete
 
Dim da As SqlDataAdapter = New SqlDataAdapter()
  Dim cmd As SqlCommand
  Dim parm As SqlParameter
 
  ' Create the SelectCommand.
 
  cmd = New SqlCommand("SELECT * FROM Customers " & _
                       "WHERE Country = @Country AND City = @City", conn)
 
  cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15)
  cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15)
 
  da.SelectCommand = cmd
 
  ' Create the DeleteCommand.
 
  cmd = New SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", conn)
 
  parm = cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
  parm.SourceVersion = DataRowVersion.Original
 
  da.DeleteCommand = cmd

Open in new window

0
earngreenAuthor Commented:
ok, Maybe I was not clear. I am pulling the data from a third parties SDK tool and then trying to save that to the SQL. The datarows that you see above are being filled from that SDK interface and then I need to add them to SQL Server. I am thinking that I might need to fill a data adapter from the data rows above and then commit to SQL Server but just not sure how to go about it.
0
David L. HansenCEOCommented:
If you have an SQL database of your own that you have control over, you know the name of the database, and the name of the table (as well as its fields/columns), then the code listed above will work.

If the table is hidden by the 3rd party's software then you can't place data in it unless the 3rd party SDK provides a method of doing so.
0
earngreenAuthor Commented:
The database that is being updated is SQL Server and is updatable. However, where the information is being pulled from is the third party database which is not updateable. So I am pulling from a source which fills the datatable and then I want to take those records to try to update the SQL Server database.
0
David L. HansenCEOCommented:
Great, it sounds like you have a SQL Server database which you have total control over.  So then, if you know the database name, the table name, and the fields within that table, you are ready to run updates through VB.  Just use the code below (it's the same as above without the parameter).
Dim WithEvents conn As SqlConnection
  conn = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI") 
 
 
'----place this in a sub where you pass in your 
'----variables (ie. myCustName, myPhone, etc.)
Dim da As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand
 
  ' Create the UpdateCommand.
   cmd = New SqlCommand("UPDATE Customer SET CustomerName = " & myCustName & ", Phone = " & myPhone & " & " WHERE CustomerID = " & myCustID & ")", conn)
 
  da.UpdateCommand = cmd
 

Open in new window

0
David L. HansenCEOCommented:
Oops, I forgot the "conn.Open()" statement above.

Also, this (code below) works without using a dataadapter.
Imports System.Data.SqlClient
 
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra as Integer 
 
 
 
myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
myConnection.Open()
 
'Careful with your double and single quotes
myCommand = New SqlCommand("Update Authors Set city='" & myCity & "' where empID =_
" & myEmpID & ", myConnection)
 
ra=myCommand.ExecuteNonQuery()
 
MessageBox.Show("Records affected" & ra)
myConnection.Close()

Open in new window

0
earngreenAuthor Commented:
I used the stored procedure to resolve. Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.