you need to pretty much retrieve all the data/columns from the first database. Then you need to generate an INSERT statement into SQL for example (your destination database). These syntax below will work on SQL:
INSERT INTO tableName (field1, field2) VALUES (@p1, @p2)
of course replace the tablename/fieldnames etc... and add the number of fields to add data for 1 record appropriately. Currently it only has 2 fields.
Here is an example of inserting data into a database in SQL using 2 fields with given values. Be sure to import the System.Data.SqlClient namespace:
| |
SqlCommand theSQLCommand = new SqlCommand("INSERT INTO tableName (field1, field2) VALUES (@p1, @p2)"); theSqlCommand.Connection = new SqlConnection(connectionString); SqlParameter p1 = new SqlParameter("@p1", SqlDbType.Int); p1.Value = "1"; SqlParameter p2 = new SqlParameter("@p2", SqlDbType.NVarChar, 50); p2.Value = "hello"; theSqlCommand.Parameters.Add(p1); theSqlCommand.Parameters.Add(p2); theSqlCommand.Connection.Open(); theSqlCommand.ExecuteNonQuery(); theSqlCommand.Connection.Close();
|
this simply creates 2 parameters, an Insert statement to insert the values for 1 record and executes the command. Its best practice to use Stored Procedures for Sql since its safer and securer but this is something for another day for you to learn perhaps, the above was a simple quick example.
As for creating a dataTable for example, similar thing would apply EXCEPT you need to again, get the columns you need, their values, and create a datatable with the amount of columns you like:
DataTable theDataTable = new DataTable();
theDataTable.Columns.Add("columnName");
theDataTable.Columns.Add("columnName2");
//Add a record:
object[] theRecord = new object[] {"Field1Value", "Field2Value"};
theDataTable.Rows.Add(theRecord);
this will create the columns in the datatable, and add a record of data, with the column values. You can also "strongly type" the datatable, meaning, each column must be of a specific data type, like string, or int etc... Example:
theDataTable.Columns.Add("columnName", System.Type.GetType("System.String"));
Does this help at all?
Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator |