Tobias Hoeft

Tobias Hoeft

  • NA
  • 3
  • 4.3k

.mdb data import in mysql Database

Sep 7 2011 4:01 AM
I must write a programm how grab a .mdb access database to an OTRS ( ticketsystem) .

This Programm must write the data in "kunden.mdb" to the OTRS mysql database customer_users instendly!


I dont have any idea how i write this for example i think i can do it with this code maybe!

using System;
using System.IO;
using System.Data ;
using MySql.Data.MySqlClient;
namespace otrs_datenabgleich
{
  class Program
  {

  static void Main(string[] args)
  {
 
  //  int nextTime;
  //  IAsyncResult asyncResult;
  //  Zuweisung globaler Variablen :
  bool reconnect = false;
  //Connectionstring mit parameter
  string connStr = "server=localhost;user=root;database=customer_user;port=3306;password=HannoveR;";
  MySqlConnection conn = new MySqlConnection(connStr);

  MySqlBulkLoader bl = new MySqlBulkLoader(conn);
  bl.TableName = "Kunden";
  bl.FieldTerminator = "\t";
  bl.LineTerminator = "\n";
  bl.FileName = "D:\\Projekte\\OTRS\\Ticketsystem\\itwhkunden.mdb";
  bl.NumberOfLinesToSkip = 3;
  FileStream fs = new FileStream(@"D:\logdatei.txt", FileMode.Append);
  StreamWriter sw = new StreamWriter(fs);

  if (conn != null)
  conn.Close();

  // Wenn true ->> Verbindung zum Server herstellen mit parametern aus connStr.
  do
  {
  try
  {
  Console.WriteLine("Connecting to MySQL...");
  conn.Open();

  // Upload data from file
  int count = bl.Load();
  Console.WriteLine(count + " lines uploaded.");

  string sql = "SELECT * from Kunden  ";
  MySqlCommand cmd = new MySqlCommand(sql, conn);
  MySqlDataReader rdr = cmd.ExecuteReader();

  while (rdr.Read())
  {
  Console.WriteLine(rdr[0] + " -- " + rdr[1] + " -- " + rdr[2]);
  }

  rdr.Close();

  conn.Close();
  }

  catch (MySql.Data.MySqlClient.MySqlException ex)
  {
  //Fehlerlog in ~fehlerlog.txt; Fehlermeldung + "ex" + Datum
  Console.WriteLine("Fehlermeldung: " + ex.Message);
  sw.WriteLine("Fehlermeldung :" + ex.Message + " " + DateTime.Now);
  reconnect = true;
  }


  }while (reconnect);
  Console.WriteLine("Done.");
  sw.Close();
  Console.Read();

 


  }


  }
  }

I am feel proud for any help to code this programm and ideas! thx u



Answers (1)