Click here to Skip to main content
15,914,165 members
Home / Discussions / Database
   

Database

 
GeneralRe: Backing up and restoring tables in SQL Server Pin
John Fisher23-May-03 11:49
John Fisher23-May-03 11:49 
QuestionBeginners help? Pin
Anonymous12-May-03 5:44
Anonymous12-May-03 5:44 
AnswerRe: Beginners help? Pin
Mike Osbahr19-May-03 11:34
Mike Osbahr19-May-03 11:34 
QuestionHow to Filter a database with the help of a combobox ? Pin
geraldschaller10-May-03 2:26
geraldschaller10-May-03 2:26 
GeneralExecuteReader question Pin
kensai10-May-03 1:14
kensai10-May-03 1:14 
GeneralRe: ExecuteReader question Pin
jeff_martin12-May-03 7:38
jeff_martin12-May-03 7:38 
GeneralPerl Project Help Pin
micahk9-May-03 1:05
micahk9-May-03 1:05 
GeneralWriting a DataTable As DBF (Long Post) Pin
Mark Sanders8-May-03 10:32
Mark Sanders8-May-03 10:32 
This will be a long post but any insight will be greatly appreciated.

Process Description:
I have to write a C# method which will take as its parameter a System.Data.DataTable and then write this DataTable to a new dBASE IV table on disk.

Current Solution: (not a good one)
Here is my current solution in C# which is very very very slow (It takes 5+ minutes to output a 10MB .DBF on a dual 2.4 Zeon machine with 2GB of RAM running a RAID 5). Cry | :((

public void WriteTable(DataTable table)
{
  //First, I create a column string to use in the CREATE TABLE sql statement.
  string columnString="";
 
  foreach(DataColumn column in table.Columns)
  {
    string type = "";

    switch(column.DataType.ToString())
    {
      case "System.String":
        type = "varchar(" + MaxLength(column) + ")";
        break;
      case "System.Int32":
        type = "int";
        break;
      case "System.Double":
        type = "double";
        break;
      default:
        throw new ArgumentException("Data type not found.", column.DataType.ToString());
    }

    columnString += column.ColumnName + " " + type + ", ";
  }


  columnString = columnString.Substring(0, columnString.Length-2);
  //Done creating column string.
  //////////////////////////////////////////////////////////////////////
  
  //Second, I create the new table on disk.
  connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + directory + ";Extended Properties=dBase IV";
  sqlString = "CREATE TABLE " + name + " (" + columnString + ")";
  connection = new OleDbConnection(connectionString);
  command = new OleDbCommand(sqlString, connection);
			
  command.Connection.Open();
  command.ExecuteNonQuery();
  command.Connection.Close();
  //Done creating table.
  //////////////////////////////////////////////////////////////////////
  
  //Third, I insert each record into the new table.
  command.Connection.Open();

  int columnCount = table.Columns.Count;
  foreach(DataRow row in table.Rows)
  {
    string valueString="";
    for(int i=0; i < columnCount; i++)
    {
      switch(row[i].GetType().ToString())
      {
        case "System.String":
          valueString += "'" + ((string)row[i]).Replace("'", "''") + "', ";
          break;
        case "System.Int32":
          valueString += System.Convert.ToString((int)row[i]) + ", ";
          break;
        case "System.Double":
          valueString += System.Convert.ToString((double)row[i]) + ", ";
          break;
        case "System.DBNull":
          valueString += "'', ";
          break;
        default:
          throw new ArgumentException("Data type not found.", row[i].GetType().ToString());
      }
    }
    valueString = valueString.Substring(0, valueString.Length-2);
    sqlString = "INSERT INTO " + name + " VALUES (" + valueString + ")";
    command.CommandText = sqlString;
    command.ExecuteNonQuery();
  }


  command.Connection.Close();

}

Questions:
- Any suggestions on a much better way to do this?
- Do you think it would be possible/better to try and see if I could use an OleDbDataSet/OleDbDataAdapter solution?
- Can anyone give me some insight as to why this solution is incredibly slow other than the fact that there is a very large amount of records to write out?

Any comments, suggestions, samples, etc. will be greatly appreciated.

Thanks Smile | :)

Mark Sanders
GeneralRe: Writing a DataTable As DBF (Long Post) Pin
Daniel Turini8-May-03 10:58
Daniel Turini8-May-03 10:58 
GeneralSamples Pin
Mark Sanders9-May-03 8:10
Mark Sanders9-May-03 8:10 
GeneralBest Way Pin
Mark Sanders12-May-03 9:53
Mark Sanders12-May-03 9:53 
GeneralRe: Writing a DataTable As DBF (Long Post) Pin
User 31295915-May-03 2:27
User 31295915-May-03 2:27 
GeneralSQL Statment Question... Pin
Jason Weibel8-May-03 5:29
Jason Weibel8-May-03 5:29 
GeneralRe: SQL Statment Question... Pin
basementman12-May-03 6:01
basementman12-May-03 6:01 
GeneralRe: SQL Statment Question... Pin
Nick Parker28-Jul-03 8:03
protectorNick Parker28-Jul-03 8:03 
GeneralRe: SQL Statment Question... Pin
Jason Weibel28-Jul-03 8:07
Jason Weibel28-Jul-03 8:07 
GeneralRe: SQL Statment Question... Pin
Nick Parker31-Jul-03 10:30
protectorNick Parker31-Jul-03 10:30 
GeneralDetermining the oledbconnection is from oracle database Pin
Syed Abdul Khader7-May-03 23:46
Syed Abdul Khader7-May-03 23:46 
GeneralMaster-Detail WinForm Pin
itawil7-May-03 8:03
itawil7-May-03 8:03 
GeneralMS access relationships and ASP Pin
Sarvesvara (BVKS) Dasa6-May-03 22:40
Sarvesvara (BVKS) Dasa6-May-03 22:40 
GeneralRe: MS access relationships and ASP Pin
Jean-Marc Molina29-May-03 13:32
Jean-Marc Molina29-May-03 13:32 
Questionhow to access Paradox database? Pin
GreatSolar6-May-03 16:12
GreatSolar6-May-03 16:12 
GeneralHierarchical query Pin
Anonymous6-May-03 1:47
Anonymous6-May-03 1:47 
GeneralRe: Hierarchical query Pin
DiWa6-May-03 3:07
DiWa6-May-03 3:07 
GeneralRe: Hierarchical query Pin
James Simpson7-May-03 5:43
James Simpson7-May-03 5:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.