Trouble with Insert Into .dbf file

℡╲_俬逩灬. 提交于 2019-12-11 06:44:14

问题


This code does not save any data in the dbf file. What is wrong here? Here is the code with the recommended changes. Thank you.

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TEMP;Extended Properties=dBase IV"; 
        using (OleDbConnection connection = new OleDbConnection(connectionString)) 
        using (OleDbCommand command = connection.CreateCommand()) 
        { 
            connection.Open(); 
            command.CommandText = @"CREATE TABLE TEST (Id Text, Changed Text, Tacos Text)"; 
            command.ExecuteNonQuery(); 
        }
        using (OleDbConnection connection2 = new OleDbConnection(connectionString))
        using (OleDbCommand command2 = connection2.CreateCommand())
        {
            connection2.Open();
            command2.CommandText = @"Insert into TEST (Id, Changed, Tacos) VALUES ('One','Two','Three')";

            try
            {
                command2.ExecuteNonQuery();
            }
            catch (Exception ee) 
            {
                MessageBox.Show(ee.ToString());
            }
        }

回答1:


Not positive, but looking at your connection, you are connecting to the root of C:.... bad... It could just be a matter of permissions and not able to write at the root directory.'

I would suggest creating your connection to SOME sub folder, even if its just

C:\MyDBFTesting\

After that, see if the "CREATE TABLE" result actually worked and you can see the "test.dbf".

Finally, put your executeNonQuery() in a try/catch

try
{
   command2.ExecuteNonQuery()
}
catch( Exception e )
{
   string x = e.Message;
}

put a break point at the catch and step into... see what OTHER element / messages may be in the "e" exception object to provide more possible answers for you.

Seeing the context of ".dbt" (which indicates more free-form text rather than fixed / standard types), i would change your create to explicitly identify CHARACTER of fixed size capacity... something like

create table Test ( ID C(10), Changed C(10), Tacos C(10) )

C = Character and 10 is the max size that would be allowed in the column. Other data types, such as I = int, D = date only, T = date/time, L = logical, etc. These are "known" types which provide a strict structure. The TEXT is freeform and its content goes into the .dbt file, but there SHOULD be a corresponding record, but otherwise not appearing to show up.

Then, your insert command would still work identically.



来源:https://stackoverflow.com/questions/9642944/trouble-with-insert-into-dbf-file

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