Re: Nochmals Treeview

From: Michael Brockhoff (spammichzu_at_aol.com)
Date: 02/23/05


Date: Wed, 23 Feb 2005 08:11:24 +0100


"Peter Forstmeier" <Peter.Forstmeier@t-online.de> schrieb im Newsbeitrag news:OOo0iRMGFHA.2976@TK2MSFTNGP09.phx.gbl...
> Ich bins nochmals,
> zu meinen Thread weiter oben habe ich noch was vergessen.
> kennt jemand so einen Dialog der das vielleicht schon fertig eingebaut hat
> oder hat zufällig jemand sowas in seiner Werkzeugkiste liegen um es in einem
> OpenSourc project zu verwenden.
> Peter
>
>

sowas ähnliches hab ich mal benutzt hier der Code:
wenn du willst schick ich das fertige project

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.OleDb;

namespace JetSQLKonverter

{

/// <summary>

/// Zusammenfassung für Form1.

/// </summary>

public class FormMain : System.Windows.Forms.Form

{

private DBBrowser.IDBBrowser _dbBrowser;

private DBBrowser.IDBBrowser _dbBrowser2;

private System.Windows.Forms.Panel panel1;

private System.Windows.Forms.Splitter splitter1;

private System.Windows.Forms.Panel panel2;

private System.Windows.Forms.Splitter splitter2;

private System.Windows.Forms.Panel panel3;

private System.Windows.Forms.GroupBox grpBox1;

private System.Windows.Forms.GroupBox grpBox2;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Label label3;

private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;

private System.Windows.Forms.TextBox txtBxAccessPfad;

private System.Windows.Forms.TreeView treeView1;

private System.Windows.Forms.TreeView treeView2;

private System.Windows.Forms.TextBox txtBoxUserID;

private System.Windows.Forms.TextBox txtBxDataSource;

private System.Windows.Forms.TextBox txtBoxPassword;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.ImageList imageList1;

private System.Windows.Forms.Button button3;

private System.Windows.Forms.ListBox listBox1;

private System.Windows.Forms.Panel panelJet;

private System.Windows.Forms.Panel panelSQL;

private System.Windows.Forms.TextBox _txtBNewDBPath;

private System.Windows.Forms.Button _btnNewDbPath;

private System.ComponentModel.IContainer components;

public FormMain()

{

//

// Erforderlich für die Windows Form-Designerunterstützung

//

InitializeComponent();

//

// TODO: Fügen Sie den Konstruktorcode nach dem Aufruf von InitializeComponent hinzu

//

}

/// <summary>

/// Die verwendeten Ressourcen bereinigen.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Vom Windows Form-Designer generierter Code

/// <summary>

/// Erforderliche Methode für die Designerunterstützung.

/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormMain));

this.panel1 = new System.Windows.Forms.Panel();

this.panelJet = new System.Windows.Forms.Panel();

this.treeView1 = new System.Windows.Forms.TreeView();

this.imageList1 = new System.Windows.Forms.ImageList(this.components);

this.splitter1 = new System.Windows.Forms.Splitter();

this.panel2 = new System.Windows.Forms.Panel();

this.listBox1 = new System.Windows.Forms.ListBox();

this.button3 = new System.Windows.Forms.Button();

this.grpBox1 = new System.Windows.Forms.GroupBox();

this.txtBxAccessPfad = new System.Windows.Forms.TextBox();

this.button1 = new System.Windows.Forms.Button();

this.grpBox2 = new System.Windows.Forms.GroupBox();

this.button2 = new System.Windows.Forms.Button();

this.txtBoxPassword = new System.Windows.Forms.TextBox();

this.label3 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.txtBoxUserID = new System.Windows.Forms.TextBox();

this.label1 = new System.Windows.Forms.Label();

this.txtBxDataSource = new System.Windows.Forms.TextBox();

this.splitter2 = new System.Windows.Forms.Splitter();

this.panel3 = new System.Windows.Forms.Panel();

this.panelSQL = new System.Windows.Forms.Panel();

this.treeView2 = new System.Windows.Forms.TreeView();

this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();

this._txtBNewDBPath = new System.Windows.Forms.TextBox();

this._btnNewDbPath = new System.Windows.Forms.Button();

this.panel1.SuspendLayout();

this.panel2.SuspendLayout();

this.grpBox1.SuspendLayout();

this.grpBox2.SuspendLayout();

this.panel3.SuspendLayout();

this.SuspendLayout();

//

// panel1

//

this.panel1.BackColor = System.Drawing.Color.Red;

this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

this.panel1.Controls.Add(this.panelJet);

this.panel1.Controls.Add(this.treeView1);

this.panel1.Dock = System.Windows.Forms.DockStyle.Left;

this.panel1.Location = new System.Drawing.Point(0, 0);

this.panel1.Name = "panel1";

this.panel1.Size = new System.Drawing.Size(152, 629);

this.panel1.TabIndex = 0;

//

// panelJet

//

this.panelJet.BackColor = System.Drawing.SystemColors.Control;

this.panelJet.Dock = System.Windows.Forms.DockStyle.Fill;

this.panelJet.Location = new System.Drawing.Point(0, 313);

this.panelJet.Name = "panelJet";

this.panelJet.Size = new System.Drawing.Size(148, 312);

this.panelJet.TabIndex = 1;

//

// treeView1

//

this.treeView1.CheckBoxes = true;

this.treeView1.Dock = System.Windows.Forms.DockStyle.Top;

this.treeView1.ImageList = this.imageList1;

this.treeView1.Location = new System.Drawing.Point(0, 0);

this.treeView1.Name = "treeView1";

this.treeView1.Size = new System.Drawing.Size(148, 313);

this.treeView1.TabIndex = 0;

this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.OnAuswahl);

this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OnAfterSelectJETTree);

//

// imageList1

//

this.imageList1.ImageSize = new System.Drawing.Size(16, 16);

this.imageList1.TransparentColor = System.Drawing.Color.Transparent;

//

// splitter1

//

this.splitter1.Location = new System.Drawing.Point(152, 0);

this.splitter1.Name = "splitter1";

this.splitter1.Size = new System.Drawing.Size(8, 629);

this.splitter1.TabIndex = 1;

this.splitter1.TabStop = false;

//

// panel2

//

this.panel2.BackColor = System.Drawing.SystemColors.Control;

this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

this.panel2.Controls.Add(this.listBox1);

this.panel2.Controls.Add(this.button3);

this.panel2.Controls.Add(this.grpBox1);

this.panel2.Controls.Add(this.grpBox2);

this.panel2.Dock = System.Windows.Forms.DockStyle.Left;

this.panel2.Location = new System.Drawing.Point(160, 0);

this.panel2.Name = "panel2";

this.panel2.Size = new System.Drawing.Size(408, 629);

this.panel2.TabIndex = 2;

//

// listBox1

//

this.listBox1.Dock = System.Windows.Forms.DockStyle.Bottom;

this.listBox1.HorizontalScrollbar = true;

this.listBox1.Location = new System.Drawing.Point(0, 348);

this.listBox1.Name = "listBox1";

this.listBox1.Size = new System.Drawing.Size(404, 277);

this.listBox1.TabIndex = 3;

//

// button3

//

this.button3.Location = new System.Drawing.Point(16, 312);

this.button3.Name = "button3";

this.button3.Size = new System.Drawing.Size(168, 24);

this.button3.TabIndex = 2;

this.button3.Text = "Konvetierung starten";

this.button3.Click += new System.EventHandler(this.button3_Click);

//

// grpBox1

//

this.grpBox1.Controls.Add(this.txtBxAccessPfad);

this.grpBox1.Controls.Add(this.button1);

this.grpBox1.Location = new System.Drawing.Point(8, 8);

this.grpBox1.Name = "grpBox1";

this.grpBox1.Size = new System.Drawing.Size(384, 104);

this.grpBox1.TabIndex = 0;

this.grpBox1.TabStop = false;

this.grpBox1.Text = "Access Einstellungen";

//

// txtBxAccessPfad

//

this.txtBxAccessPfad.Location = new System.Drawing.Point(8, 40);

this.txtBxAccessPfad.Name = "txtBxAccessPfad";

this.txtBxAccessPfad.ReadOnly = true;

this.txtBxAccessPfad.Size = new System.Drawing.Size(368, 20);

this.txtBxAccessPfad.TabIndex = 1;

this.txtBxAccessPfad.Text = "c:\\atg\\database\\";

//

// button1

//

this.button1.Location = new System.Drawing.Point(8, 16);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(104, 23);

this.button1.TabIndex = 0;

this.button1.Text = "Pfad Einstellen";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// grpBox2

//

this.grpBox2.Controls.Add(this._txtBNewDBPath);

this.grpBox2.Controls.Add(this._btnNewDbPath);

this.grpBox2.Controls.Add(this.button2);

this.grpBox2.Controls.Add(this.txtBoxPassword);

this.grpBox2.Controls.Add(this.label3);

this.grpBox2.Controls.Add(this.label2);

this.grpBox2.Controls.Add(this.txtBoxUserID);

this.grpBox2.Controls.Add(this.label1);

this.grpBox2.Controls.Add(this.txtBxDataSource);

this.grpBox2.Location = new System.Drawing.Point(8, 120);

this.grpBox2.Name = "grpBox2";

this.grpBox2.Size = new System.Drawing.Size(384, 184);

this.grpBox2.TabIndex = 1;

this.grpBox2.TabStop = false;

this.grpBox2.Text = "SQL Einstellungen";

//

// button2

//

this.button2.Location = new System.Drawing.Point(24, 152);

this.button2.Name = "button2";

this.button2.Size = new System.Drawing.Size(136, 24);

this.button2.TabIndex = 7;

this.button2.Text = "Verbinden";

this.button2.Click += new System.EventHandler(this.button2_Click);

//

// txtBoxPassword

//

this.txtBoxPassword.Location = new System.Drawing.Point(136, 72);

this.txtBoxPassword.Name = "txtBoxPassword";

this.txtBoxPassword.Size = new System.Drawing.Size(192, 20);

this.txtBoxPassword.TabIndex = 6;

this.txtBoxPassword.Text = "wpd 01";

//

// label3

//

this.label3.Location = new System.Drawing.Point(32, 72);

this.label3.Name = "label3";

this.label3.Size = new System.Drawing.Size(104, 20);

this.label3.TabIndex = 5;

this.label3.Text = "Password";

this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// label2

//

this.label2.Location = new System.Drawing.Point(32, 48);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(104, 20);

this.label2.TabIndex = 3;

this.label2.Text = "User ID";

this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// txtBoxUserID

//

this.txtBoxUserID.Location = new System.Drawing.Point(136, 48);

this.txtBoxUserID.Name = "txtBoxUserID";

this.txtBoxUserID.Size = new System.Drawing.Size(192, 20);

this.txtBoxUserID.TabIndex = 2;

this.txtBoxUserID.Text = "sa";

//

// label1

//

this.label1.Location = new System.Drawing.Point(32, 24);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(104, 20);

this.label1.TabIndex = 1;

this.label1.Text = "Data Source";

this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// txtBxDataSource

//

this.txtBxDataSource.Location = new System.Drawing.Point(136, 25);

this.txtBxDataSource.Name = "txtBxDataSource";

this.txtBxDataSource.Size = new System.Drawing.Size(192, 20);

this.txtBxDataSource.TabIndex = 0;

this.txtBxDataSource.Text = "brockhoff";

//

// splitter2

//

this.splitter2.Location = new System.Drawing.Point(568, 0);

this.splitter2.Name = "splitter2";

this.splitter2.Size = new System.Drawing.Size(8, 629);

this.splitter2.TabIndex = 3;

this.splitter2.TabStop = false;

//

// panel3

//

this.panel3.BackColor = System.Drawing.Color.Lime;

this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

this.panel3.Controls.Add(this.panelSQL);

this.panel3.Controls.Add(this.treeView2);

this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;

this.panel3.Location = new System.Drawing.Point(576, 0);

this.panel3.Name = "panel3";

this.panel3.Size = new System.Drawing.Size(232, 629);

this.panel3.TabIndex = 4;

//

// panelSQL

//

this.panelSQL.BackColor = System.Drawing.SystemColors.Control;

this.panelSQL.Dock = System.Windows.Forms.DockStyle.Fill;

this.panelSQL.Location = new System.Drawing.Point(0, 313);

this.panelSQL.Name = "panelSQL";

this.panelSQL.Size = new System.Drawing.Size(228, 312);

this.panelSQL.TabIndex = 1;

//

// treeView2

//

this.treeView2.Dock = System.Windows.Forms.DockStyle.Top;

this.treeView2.ImageList = this.imageList1;

this.treeView2.Location = new System.Drawing.Point(0, 0);

this.treeView2.Name = "treeView2";

this.treeView2.Size = new System.Drawing.Size(228, 313);

this.treeView2.TabIndex = 0;

this.treeView2.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OnAfterSelectSQLTree);

//

// _txtBNewDBPath

//

this._txtBNewDBPath.Location = new System.Drawing.Point(8, 128);

this._txtBNewDBPath.Name = "_txtBNewDBPath";

this._txtBNewDBPath.ReadOnly = true;

this._txtBNewDBPath.Size = new System.Drawing.Size(368, 20);

this._txtBNewDBPath.TabIndex = 9;

this._txtBNewDBPath.Text = "c:\\atg\\database\\";

//

// _btnNewDbPath

//

this._btnNewDbPath.Location = new System.Drawing.Point(8, 104);

this._btnNewDbPath.Name = "_btnNewDbPath";

this._btnNewDbPath.Size = new System.Drawing.Size(192, 23);

this._btnNewDbPath.TabIndex = 8;

this._btnNewDbPath.Text = "Pfad für neue DB (*.mdf und *.ldf)";

this._btnNewDbPath.Click += new System.EventHandler(this._btnNewDbPath_Click);

//

// FormMain

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(808, 629);

this.Controls.Add(this.panel3);

this.Controls.Add(this.splitter2);

this.Controls.Add(this.panel2);

this.Controls.Add(this.splitter1);

this.Controls.Add(this.panel1);

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

this.Name = "FormMain";

this.Text = "JET -> SQL Konverter";

this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

this.panel1.ResumeLayout(false);

this.panel2.ResumeLayout(false);

this.grpBox1.ResumeLayout(false);

this.grpBox2.ResumeLayout(false);

this.panel3.ResumeLayout(false);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// Der Haupteinstiegspunkt für die Anwendung.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new FormMain());

}

private void button1_Click(object sender, System.EventArgs e) {

if(this.folderBrowserDialog1.ShowDialog() == DialogResult.OK){

Cursor.Current = Cursors.WaitCursor;

this.treeView1.Nodes.Clear();

this.txtBxAccessPfad.Text = folderBrowserDialog1.SelectedPath + "\\";

if(_dbBrowser == null)

_dbBrowser = new DBBrowser.JetBrowser(txtBxAccessPfad.Text, this.panelJet);

_dbBrowser.BuildTreeNode(this.treeView1);

Cursor.Current = Cursors.Default;

}

}

private void button2_Click(object sender, System.EventArgs e) {

Cursor.Current = Cursors.WaitCursor;

this.treeView2.Nodes.Clear();

string strSQLConn = "data source =" + this.txtBxDataSource.Text +

"; user id =" + this.txtBoxUserID.Text +

"; password =" + this.txtBoxPassword.Text ;

if(_dbBrowser2 == null)

_dbBrowser2 = new DBBrowser.SQLBrowser(strSQLConn, this.panelSQL);

_dbBrowser2.BuildTreeNode(this.treeView2);

Cursor.Current = Cursors.Default;

}

private void button3_Click(object sender, System.EventArgs e) {

Cursor.Current = Cursors.WaitCursor;

this.listBox1.Items.Clear();

AddTrace("Kovertierung gestartet");

foreach(TreeNode xNode in this.treeView1.Nodes){

foreach(TreeNode lvl2Node in xNode.Nodes){

if(lvl2Node.Checked){

//Datenbank loeschen und anlegen

DelAndCreateDB(lvl2Node.Text);

}

foreach(TreeNode lvl3Node in lvl2Node.Nodes){

if(lvl3Node.Checked){

// Tabelle untersuchen...

TabelleLoeschen(lvl3Node.Parent.Text.ToUpper().Replace(".MDB",""),lvl3Node.Text);

TabelleAnlegen(this.txtBxAccessPfad.Text, lvl3Node.Parent.Text, lvl3Node.Text);

TabellenInhaltKopieren(lvl3Node.Parent.Text, lvl3Node.Text, this.txtBxAccessPfad.Text);

}

}

}

}

AddTrace("FERTIG!");

this.treeView2.Nodes.Clear();

string strSQLConn = "data source =" + this.txtBxDataSource.Text +

"; user id =" + this.txtBoxUserID.Text +

"; password =" + this.txtBoxPassword.Text;

if(_dbBrowser2 == null)

_dbBrowser2 = new DBBrowser.SQLBrowser(strSQLConn, this.panelSQL);

_dbBrowser2.BuildTreeNode(this.treeView2);

Cursor.Current = Cursors.Default;

}

private void TabelleAnlegen(string strDBPfad , string strDB, string strTabellenName){

try{

string strConn = @"Provider=Microsoft.JET.OLEDB.4.0;data source="+ strDBPfad + strDB;

OleDbConnection conn = new OleDbConnection(strConn);

conn.Open();

DataTable pkTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys,

new object[] {null, null, strTabellenName});

DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,

new object[] {null, null, strTabellenName,null });

conn.Close();

string strSQLString = BuildCreateTableCommand(schemaTable, pkTable, strDB, strTabellenName);

SQLTabelleAnlegen(strDB.ToUpper().Replace(".MDB",""), strSQLString);

}catch(Exception ex){

MessageBox.Show(ex.Message);

}

}

private string BuildCreateTableCommand(DataTable schemaTable, DataTable dtPrimaryKey, string strDB, string strTable){

string strPKColumn = "";

//Schema Tabelle nach "ORDINAL_POSITION" ordnen

DataRow[] schemaRows = schemaTable.Select("", "ORDINAL_POSITION");

// PK heraussuchen bzw warnung ausgeben

if(dtPrimaryKey.Rows.Count == 0)

AddTrace("KEIN PK!" + strDB + "\\" + strTable);

else

strPKColumn = dtPrimaryKey.Rows[0]["COLUMN_NAME"].ToString();

string strCreateTableCommand = "CREATE TABLE ["

+ strDB.ToUpper().Replace(".MDB","")

+ "].[dbo].["

+ strTable + "](";

for(int i = 0; i < schemaRows.Length; i++){

strCreateTableCommand += "["

+schemaRows[i]["COLUMN_NAME"].ToString().Replace(" ","_")

+ "]";

switch(schemaRows[i]["DATA_TYPE"].ToString()){

case "2":

case "3":

case "5":

strCreateTableCommand += " int";

break;

case "7":

strCreateTableCommand += " datetime";

break;

case "11":

strCreateTableCommand += " bit";

break;

case "128":

strCreateTableCommand += " image";

break;

case "130":

strCreateTableCommand += " varchar (" + schemaRows[i]["CHARACTER_MAXIMUM_LENGTH"].ToString() +")";

break;

default:

AddTrace("FEHLER: Unbekannter DatenTyp " + schemaRows[i]["DATA_TYPE"].ToString() + schemaRows[i]["COLUMN_NAME"].ToString() +"\\"+
strDB +"\\" +strTable);

break;

}

if(schemaRows[i]["COLUMN_NAME"].ToString() == strPKColumn)

strCreateTableCommand += " PRIMARY KEY,";

else

strCreateTableCommand += " NULL ,";

}

//letztest komma löschen

strCreateTableCommand.TrimEnd(',');

strCreateTableCommand += ")";

return strCreateTableCommand;

}

private void SQLTabelleAnlegen(string strDB, string strSQLString){

try{

string connString = "Provider = SQLOLEDB.1"+

";Data Source = " + this.txtBxDataSource.Text +

";user id = " + this.txtBoxUserID.Text +

";password = " + this.txtBoxPassword.Text +

"; initial catalog = " + strDB;

OleDbConnection connection = new OleDbConnection(connString);

OleDbCommand command = new OleDbCommand();

command.CommandType = CommandType.Text;

command.CommandText = strSQLString;

command.Connection = connection;

connection.Open();

int cmdresult = command.ExecuteNonQuery();

connection.Close();

this.AddTrace("SQL " + strDB + "\\"+ strSQLString + " angelegt");

}

catch(Exception exc){

this.AddTrace(exc.Message);

}

}

private void TabellenInhaltKopieren(string strDB, string strTabellenName, string strJetPfad){

try{

string connSQL = "Provider = SQLOLEDB.1"+

";Data Source = " + this.txtBxDataSource.Text +

";user id = " + this.txtBoxUserID.Text +

";password = " + this.txtBoxPassword.Text +

"; initial catalog = " + strDB.ToUpper().Replace(".MDB","");

string connJet = @"Provider=Microsoft.JET.OLEDB.4.0;data source="+ strJetPfad + strDB;

string strSQLStr = string.Format("Select * from [{0}]",strTabellenName);

OleDbDataAdapter daJet = new OleDbDataAdapter(strSQLStr,connJet);

DataTable dtJet = new DataTable();

daJet.Fill(dtJet);

OleDbDataAdapter daSQL = new OleDbDataAdapter(strSQLStr,connSQL);

DataTable dtSQL = new DataTable();

daSQL.Fill(dtSQL);

OleDbCommandBuilder commandSQL = new OleDbCommandBuilder(daSQL);

commandSQL.QuotePrefix = "[";

commandSQL.QuoteSuffix = "]";

//daJet.InsertCommand

foreach(DataRow xRow in dtJet.Rows){

//DataRow newRow = this._dsMapFileSpy.Tables["MapFileList"].NewRow();

DataRow newRow = dtSQL.NewRow();

for(int i = 0; i < dtJet.Columns.Count; i++){

newRow[i] = xRow[i];

}

dtSQL.Rows.Add(newRow);

}

//this.dataGrid1.DataSource = dtSQL;

daSQL.Update(dtSQL);

this.AddTrace("SQL " + strDB + "\\"+ strTabellenName + " kopiert");

}catch(Exception ex){

AddTrace(ex.Message);

}

}

private void TabelleLoeschen(string strDB, string TabellenName){

try{

string connString = "Provider = SQLOLEDB.1"+

";Data Source = " + this.txtBxDataSource.Text +

";user id = " + this.txtBoxUserID.Text +

";password = " + this.txtBoxPassword.Text +

"; initial catalog = " + strDB;

string sqlString = string.Format("DROP TABLE [{0}]",TabellenName);

OleDbConnection connection = new OleDbConnection(connString);

OleDbCommand command = new OleDbCommand();

command.CommandType = CommandType.Text;

command.CommandText = sqlString;

command.Connection = connection;

connection.Open();

int cmdresult = command.ExecuteNonQuery();

connection.Close();

this.AddTrace("SQL " + strDB + "\\"+ TabellenName + " gelöscht");

}

catch(Exception exc){

//this.AddTrace(exc.Message);

}

}

private void AddTrace(string strText){

string strTemp = string.Format("{0} {1}",DateTime.Now,strText);

this.listBox1.Items.Add(strTemp);

this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;

}

private void OnAuswahl(object sender, System.Windows.Forms.TreeViewEventArgs e) {

TreeView trView = (TreeView) sender;

foreach(TreeNode xNode in e.Node.Nodes){

xNode.Checked = e.Node.Checked;

}

}

private void OnAfterSelectJETTree(object sender, System.Windows.Forms.TreeViewEventArgs e) {

if(this.treeView1.SelectedNode.Nodes.Count == 0){

this._dbBrowser.SelectDbAndTable(this.treeView1.SelectedNode.ToString().Replace("TreeNode:
",""),this.treeView1.SelectedNode.Parent.ToString().Replace("TreeNode: ",""));

}

}

private void OnAfterSelectSQLTree(object sender, System.Windows.Forms.TreeViewEventArgs e) {

if(this.treeView2.SelectedNode.Nodes.Count == 0){

this._dbBrowser2.SelectDbAndTable(this.treeView2.SelectedNode.ToString().Replace("TreeNode:
",""),this.treeView2.SelectedNode.Parent.ToString().Replace("TreeNode: ",""));

}

}

private void DelAndCreateDB(string strDB){

string connString = "Provider = SQLOLEDB.1"+

";Data Source = " + this.txtBxDataSource.Text +

";user id = " + this.txtBoxUserID.Text +

";password = " + this.txtBoxPassword.Text;

//string sqlString = string.Format("exec sp_detach_db N'{0}',N'true'",strDB.ToUpper().Replace(".MDB",""));

string sqlString = string.Format("DROP DATABASE [{0}]",strDB.ToUpper().Replace(".MDB",""));

OleDbConnection connection = new OleDbConnection(connString);

OleDbCommand command = new OleDbCommand();

try{

command.CommandType = CommandType.Text;

command.CommandText = sqlString;

command.Connection = connection;

connection.Open();

int cmdresult = command.ExecuteNonQuery();

connection.Close();

command.CommandText = string.Format("CREATE DATABASE [{0}] ON (NAME = {0},FILENAME =
'{1}.mdf')",strDB.ToUpper().Replace(".MDB",""),this._txtBNewDBPath.Text + strDB.ToUpper().Replace(".MDB",""));

//CREATE DATABASE Products2

//ON

//( NAME = prods2_dat,

// FILENAME = 'c:\program files\microsoft sql server\mssql\data\prods2.mdf' )

//USE master

//GO

//CREATE DATABASE Products

//ON

//( NAME = prods_dat,

// FILENAME = 'c:\program files\microsoft sql server\mssql\data\prods.mdf',

// SIZE = 4,

// MAXSIZE = 10,

// FILEGROWTH = 1 )

//GO

connection.Open();

cmdresult = command.ExecuteNonQuery();

connection.Close();

this.AddTrace("SQL " + strDB + "\\"+ strDB.ToUpper().Replace(".MDB","") + " gelöscht und neu angelegt");

}

catch(Exception exc){

//this.AddTrace(exc.Message);

try{

command.CommandText = string.Format("CREATE DATABASE [{0}] ON (NAME = {0},FILENAME =
'{1}.mdf')",strDB.ToUpper().Replace(".MDB",""),this._txtBNewDBPath.Text + strDB.ToUpper().Replace(".MDB",""));

if(connection.State != ConnectionState.Open)

connection.Open();

int cmdresult = command.ExecuteNonQuery();

connection.Close();

this.AddTrace("SQL " + strDB + "\\"+ strDB.ToUpper().Replace(".MDB","") + " gelöscht und neu angelegt");

}catch(Exception ex){

this.AddTrace(ex.Message);

}

}

}

private void _btnNewDbPath_Click(object sender, System.EventArgs e) {

if(this.folderBrowserDialog1.ShowDialog() == DialogResult.OK){

this._txtBNewDBPath.Text = folderBrowserDialog1.SelectedPath + "\\";

}

}

}

}