How do I create a new VFP (OLEDB) table from an existing one using .NET?

北城余情 提交于 2019-12-01 09:32:59

To get an exact copy of the data, table, and records, you can do via a single SQL-Select via

OleDbConnection oConn = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=C:\\SomePath");
OleDbCommand oCmd = new OleDbCommand();
oCmd.Connection = oConn;
oCmd.Connection.Open();
oCmd.CommandText = "select * from SomeTable where someCondition into table YourNewTable";
oCmd.ExecuteNonQuery();         
oConn.Close();

Your where clause could be almost anything, and the Into TABLE clause tells the VFP engine to create the result set AS A NEW TABLE, so no need to explicitly declare types, columns, etc, query data from one and push into another...

One issue of consideration... Verify the user access to obviously be able to create, read, write wherever you are trying to create the new table. You can even specify a fully qualified path, such as C:\SomeOtherPath\Monthly\MyTable1 if need be...

Try something like this (note written in VB.NET and converted use www.developerfusion.co.uk/tools ):

using System.Data.OleDb; 
using System.IO; 

static class Module1 
{ 
    public static void Main() 
    { 
        OleDbConnection oConn = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=C:\\"); 
        OleDbCommand oCmd = new OleDbCommand(); 

        { 
            oCmd.Connection = oConn; 
            oCmd.Connection.Open(); 
            // Create a sample FoxPro table 
            oCmd.CommandText = "CREATE TABLE Table1 (FldOne c(10))"; 
            oCmd.CommandType = CommandType.Text; 
            oCmd.ExecuteNonQuery(); 
        } 

        oConn.Close(); 
        oConn.Dispose(); 
        oCmd.Dispose(); 
    } 
} 

You can simply do a:

select * from myTable into table newTable [database dbName]

as DRapp showed. However you may want to get indexes as well (if any) (BTW creating indexes via VFPOLEDB is not supported directly but you can do so using ExecScript() function). Then the easiest would be to copy the table's DBF, CDX (and FPT) files. VFP is file based.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!