Link to home
Start Free TrialLog in
Avatar of earngreen
earngreenFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of David L. Hansen
David L. Hansen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me know if you need more help.  I know a single link may not always give you what you need.
Avatar of earngreen

ASKER

I think that I am going to need a little more help. I don't think that the example will work.
Are you trying to update an existing table in your database or create a completely new one based on the datatable?
update an existing table
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

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.
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.
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.
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

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

I used the stored procedure to resolve. Thanks