Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure how far you've gotten, but if you looking for a quick drag and drop operation you might want to look at creating a strongly-typed dataset that connects, and using the drag-drop features of the DataSources Tool Window in Visual Studio.</p> <p>There are definately samples out there, but you will want to.</p> <ol> <li>Create a new DataSet </li> <li>Drag-n-Drop from your DataConnection Tree in Server Explorer</li> <li>Create a new form </li> <li>Drag the table from the DataSources Tool</li> <li>Window on to the form.</li> <li>voila</li> </ol> <p><strong>Update:</strong></p> <p>First off, I'm not 100% that I understand your issue. If you can create some LinkTables between the access files that would be best, then you can copy the data between files using a sql statement like 'INSERT INTO Customers SELECT FirstName, LastName FROM File2.Customers'. If thats not and option I think your going to have to loop the DataTables and insert the records manually using INSERT statements similar to your last edit. As for the datagrid, you will probably have to keep track of whats changed by monitoring the RowChanged Event (not sure if thats the exact event) of even do the insert/update statements when the row changes.</p> <p><strong>Update:</strong></p> <p>to loop the datatable you would do something like this. not tested. I just updated this again to include the MakeValueDbReady function. This is not tested either and I'm not sure if I've handle all the cases or even all the cases correctly. You'll really have to debug the sql statement and make sure its generating the right value. Each database handles is values differently. Atleast this way the value parse is extracted away. I also realized that instead of hard coding the TableName you should be able to get it from a property on the DataTable</p> <pre><code>void CopyTable(DataTable table, string connectionStringB) { var connectionB = new OleDbConnection(connectionStringB); foreach(DataRow row in table.Rows) { InsertRow(row, table.Columns, table.TableName, connectionB); } } public static void InsertRow(DataRow row, DataColumnCollection columns, string table, OleDbConnection connection) { var columnNames = new List&lt;string&gt;(); var values = new List&lt;string&gt;(); // generate the column and value names from the datacolumns for(int i =0;i&lt;columns.Count; i++) { columnNames.Add("[" + columns[i].ColumnName + "]"); // datatype mismatch should be fixed by this function values.Add(MakeValueDbReady(row[i], columns[i].DataType)); } // create the sql string sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", table, string.Join(", ", columnNames.ToArray()), string.Join(", ", values.ToArray()) ); // debug the accuracy of the sql here and even copy into // a new Query in Access to test ExecuteNonQuery(sql, connection); } // as the name says we are going to check the datatype and format the value // in the sql string based on the type that the database is expecting public string MakeValueDbReady(object value, Type dataType) { if (value == null) return null; if (dataType == typeof(string)) { return "'" + value.ToString().Replace("'", "''") + "'" } else if (dataType == typeof(DateTime)) { return "#" + ((DateTime)value).ToString + "#" } else if (dataType == typeof(bool)) { return ((bool)value) ? "1" : "0"; } return value.ToString(); } public static void ExecuteNonQuery(string sql, OleDbConnection conn) { if (conn == null) throw new ArgumentNullException("conn"); ConnectionState prevState = ConnectionState.Closed; var command = new OleDbCommand(sql, conn); try { // the reason we are checking the prev state is for performance reasons // later you might want to open the connection once for the a batch // of say 500 rows or even wrap your connection in a transaction. // we don't want to open and close 500 connections prevState = conn.State; if (prevState != ConnectionState.Open) conn.Open(); command.ExecuteNonQuery(); } finally { if (conn.State != ConnectionState.Closed &amp;&amp; prevState != ConnectionState.Open) conn.Close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload