Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6381975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:27:47+00:00 2026-05-25T02:27:47+00:00

I’m reading a dBase database and then sending that table to a SQL Server

  • 0

I’m reading a dBase database and then sending that table to a SQL Server 2008.

My problem now is how am i able to get the column type correctly, right now i’m getting varchar(255) on all character types.

I know i did this once long time ago but cant remember how i did it.

Export from dBase database to a dataset:

 public DataSet getDataSetFromDBF(string path, string fileName)
 {
        OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277");
        String query = @"SELECT * FROM " + @"C:\spcs\" + fileName.ToString() + ".dbf";
        try
        {
            OdbcCommand MyCommand = new OdbcCommand(query, conn);
            OdbcDataAdapter adapter = new OdbcDataAdapter(MyCommand); 
            DataSet ds = new DataSet();                
            adapter.FillSchema(ds, SchemaType.Mapped);
            conn.Open();
            adapter.Fill(ds);
            return ds;
        }
        catch (Exception ex)
        {
            exceptionLog(ex);
            return null
        }
}

And then to MSSQL Server:

private bool createSqlQueryFromDbfFile(string fileName)
{
    StringBuilder sb = new StringBuilder();
    string query = "";
    string pathToDB = @"c:\spcs\";
    string tableName = fileName.Replace(".dbf", "");
    sb.Append("IF OBJECT_ID('" + tableName + "', 'U') IS NOT NULL " + Environment.NewLine);
    sb.Append("DROP TABLE " + tableName + ";" + Environment.NewLine);
    sb.Append("CREATE TABLE " + tableName + Environment.NewLine);
    sb.Append("(" + Environment.NewLine);
    webHallDB.DAL db = new webHallDB.DAL();        
    try
    {
        DataSet ds = db.getDataSetFromDBF(pathToDB, tableName);

        int kolumner = ds.Tables[0].Columns.Count;

        string[] kolumnNamn = new string[kolumner];
        bool primaryKeySet = false;
        for (int i = 0; i < kolumner; i++)
        {
            if (primaryKeySet == false)
            {
                sb.Append("id int PRIMARY KEY IDENTITY," + Environment.NewLine);
                primaryKeySet = true;
            }
            string kolumnTyp = getColumnType(ds.Tables[0].Columns[i]);
            // Sista kolumnen
            if (i == kolumner - 1)
            {

                kolumnNamn[i] = ds.Tables[0].Columns[i].ColumnName;
                sb.Append(ds.Tables[0].Columns[i].ColumnName + " " + kolumnTyp + ");" + Environment.NewLine);
            }
            else
            {
                kolumnNamn[i] = ds.Tables[0].Columns[i].ColumnName;
                sb.Append(ds.Tables[0].Columns[i].ColumnName + " " + kolumnTyp + "," + Environment.NewLine);
            }
        }            

        int rader = ds.Tables[0].Rows.Count;
        DateTime startTid = DateTime.Now;

        StringBuilder sbInsert = new StringBuilder();

        for (int row = 0; row < rader; row++)
        {
            sbInsert.Append(Environment.NewLine + "Insert Into " + tableName);

            sbInsert.Append(Environment.NewLine + " Values (");
            for (int col = 0; col < kolumner; col++)
            {
                if (col == (kolumner - 1))
                {
                    sbInsert.Append("'" + changeSpecialCharacters(ds.Tables[0].Rows[row][col].ToString()) + "');");
                }
                else
                {
                    sbInsert.Append("'" + changeSpecialCharacters(ds.Tables[0].Rows[row][col].ToString()) + "', ");
                }
            }
        }

        query = sb.ToString();
        string insertQuery = sbInsert.ToString();

        DateTime slutTid = DateTime.Now;

        db.executeQuery(query);
        if (db.executeQuery(insertQuery))
        {
            slutTid = DateTime.Now;
            logToGui("Tid för att köra query: " + (slutTid - startTid).TotalSeconds);
            return true;
        }
        else
        {
            slutTid = DateTime.Now;
            logToGui("Tid för att köra query: (failed)" + (slutTid - startTid).TotalSeconds);
            return false;
        }
    }
    catch (Exception ex)
    {
        logToGui("createSqlQueryFromDbfFile misslyckades!");
        logToGui(ex.Message);
        return false;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:27:47+00:00Added an answer on May 25, 2026 at 2:27 am

    I managed to figure it out:

    adapter.FillSchema(ds, SchemaType.Mapped); 
    

    Then i was able to use DataColumn.MaxLength.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.