• No results found

C# Datenbank-Programmierung

N/A
N/A
Protected

Academic year: 2021

Share "C# Datenbank-Programmierung"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

____________________________________

C# Datenbank-Programmierung

____________________________________

Usings ... 2

Verbindung herstellen SQL und Acces... 2

Verbindung schliessen SQL und Acces ... 3

File open Dialog... 3

Lehar einfügen... 3

Lehar löschen ... 4

Radio Button SQL & Access ... 4

(2)

Usings

using System.Data;

using System.Data.;

using System.Data.SqlClient;

using System.Data.OleDb;

publicclass Form1 : System.Windows.Forms.Form {

private OleDbConnection cnn_OleDb; …;

}

public Form1() {

InitializeComponent();

cnn_OleDb= new OleDbConnection(); }

Verbindung herstellen SQL und Acces

privatevoid OnConnect_Click(object sender, System.EventArgs e) { try { if (cnn_OleDb.State == ConnectionState.Open) cnn_OleDb.Close(); String sTemp="";

if (this.rdBtnAccess.Checked == true) {

cnn_OleDb.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"; cnn_OleDb.ConnectionString += "Data Source=" + txtAccessPfad.Text;

cnn_OleDb.ConnectionString += ";User ID=" + txtUserName.Text; //BenutzerName

cnn_OleDb.ConnectionString += ";PASSWORD=" + txtPasswort.Text; //BenutzerPasswort cnn_OleDb.ConnectionString += ";Jet OLEDB:Database Password=bbzs"; //DatenbankPasswort

//cnn_Access.ConnectionString += ";Jet OLEDB:System Database="; //eigenes Benutzerdef-File

sTemp = "Access über OLEDB"; }

elseif(this.rdBtnSQLServer.Checked == true) {

cnn_OleDb.ConnectionString = "Provider=SQLOLEDB"; //SQL-Server über OLEDB cnn_OleDb.ConnectionString += ";Data Source=" + txtSqlIPAdresse.Text; //Name oder IP

des Servers cnn_OleDb.ConnectionString += ";User ID=" + txtUserName.Text;

//BenutzerName

cnn_OleDb.ConnectionString += ";PASSWORD=" + txtPasswort.Text; //BenutzerPasswort

(3)

cnn_OleDb.Open();

if (cnn_OleDb.State == ConnectionState.Open) {

lblVerbindungsStatus.Text = "Verbindung zu " + sTemp + " geöffnet"; lblVerbindungsStatus.ForeColor = System.Drawing.Color.Green; }

}

catch (OleDbException ex) {

string errorMessages = "";

for (int i=0; i < ex.Errors.Count; i++)

errorMessages += "Message: " + ex.Errors[i].Message + "\n"; MessageBox.Show(errorMessages);

} }

Verbindung schliessen SQL und Acces

privatevoid OnDisconnect_Click(object sender, System.EventArgs e) { try { if (cnn_OleDb.State == ConnectionState.Open) { cnn_OleDb.Close();

lblVerbindungsStatus.Text = "Verbindung getrennt";

lblVerbindungsStatus.ForeColor = System.Drawing.Color.Red; }

}

catch (OleDbException ex) {

string errorMessages = "";

for (int i=0; i < ex.Errors.Count; i++)

errorMessages += "Message: " + ex.Errors[i].Message + "\n"; MessageBox.Show(errorMessages);

} }

File open Dialog

privatevoid btnAccessFile_Click(object sender, System.EventArgs e) {

this.openFileDialog1.ShowDialog(this);

this.txtAccessPfad.Text = openFileDialog1.FileName; }

Lehar einfügen

privatevoid btnLehareinfuegen_Click(object sender, System.EventArgs e) {

try

{

OleDbCommand cmd = new OleDbCommand();

cmd.CommandText = "insert into Komponisten values (11, 'Lehar','Franz', 1870, 1948)"; cmd.Connection = cnn_OleDb;

cmd.ExecuteNonQuery(); }

catch (OleDbException ex) {

string errorMessages = "";

for (int i=0; i < ex.Errors.Count; i++) {

errorMessages += "Message: " + ex.Errors[i].Message + "\n"; }

MessageBox.Show(errorMessages); }

(4)

Lehar löschen

privatevoid btnLeharloeschen_Click(object sender, System.EventArgs e) {

try

{

OleDbCommand cmd = new OleDbCommand();

cmd.CommandText = "delete from Komponisten where Name = 'Lehar'"; cmd.Connection = cnn_OleDb;

cmd.ExecuteNonQuery(); }

catch (OleDbException ex) {

string errorMessages = "";

for (int i=0; i < ex.Errors.Count; i++) {

errorMessages += "Message: " + ex.Errors[i].Message + "\n"; }

MessageBox.Show(errorMessages); }

}

Radio Button SQL & Access

privatevoid OnVerbindungChanged(object sender, System.EventArgs e) {

if (this.rdBtnAccess.Checked == true) {

this.panelAccess.Visible = true;

this.panelSQLServer.Visible = false;

this.txtUserName.Text = "Admin"; }

elseif(this.rdBtnSQLServer.Checked == true) {

this.panelSQLServer.Visible = true;

this.panelAccess.Visible = false;

this.txtUserName.Text = "inf"; }

}

Anzeigen (alle Komponisten auflisten)

privatevoid btnAlleKomponisten_Click(object sender, System.EventArgs e) {

OleDbCommand cmd = new OleDbCommand(); cmd.CommandText = "Select * From Komponisten"; cmd.Connection = cnn_OleDb;

try

{

OleDbDataReader myReader = cmd.ExecuteReader(); listBoxAnswer.Items.Clear(); while(myReader.Read()) { listBoxAnswer.Items.Add(myReader.GetValue(0).ToString() + " " + myReader.GetString(1) + " " + myReader.GetString(2) ); } myReader.Close(); }

catch (OleDbException ex) {

string errorMessages = "";

for (int i=0; i < ex.Errors.Count; i++) {

errorMessages += "Message: " + ex.Errors[i].Message + "\n"; }

MessageBox.Show(errorMessages); }

(5)

try

{

SqlConnection conSQL = new SqlConnection();

conSQL.ConnectionString = "Server="+txtSQLServer.Text; conSQL.ConnectionString += ";uid="+txtSQLBenutzer.Text; conSQL.ConnectionString += ";pwd="+txtSQLPasswort.Text; conSQL.ConnectionString += ";database="+txtSQLDatenbank.Text; conSQL.Open();

OleDbConnection conAcc = new OleDbConnection();

conAcc.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"; conAcc.ConnectionString += "Data Source=" + txtAccessPfad.Text;

conAcc.ConnectionString += ";User ID=" + txtAccessBenutzer.Text; //BenutzerName conAcc.ConnectionString += ";PASSWORD=" + txtAccessPasswort.Text;

OleDbCommand cmdAcc = new OleDbCommand("select * from " + txtTable.Text + " where klasse='"+txtExKlasse.Text+"'", conAcc);

conAcc.Open();

try

{

OleDbDataReader myReader = cmdAcc.ExecuteReader(); SqlCommand cmdSQL = new SqlCommand();

cmdSQL.Connection = conSQL;

while(myReader.Read()) {

cmdSQL.CommandText = "insert into "+txtTable.Text+"(name,vorname,klasse) values('" + myReader.GetString(0) + "','" + myReader.GetString(1) + "','" + myReader.GetString(2) + "')"; cmdSQL.ExecuteNonQuery(); } myReader.Close(); conSQL.Close(); }

catch(System.Data.OleDb.OleDbException ex) {

MessageBox.Show(ex.Message); }

}

catch(InvalidOperationException e2) {

MessageBox.Show(e2.Message); }

References

Related documents

Athens University Law Degree, 1979. Three associate attorneys. Specializes in: civil, criminal, real estate &amp; inheritance Office address: Fidiou 14-16, GR-106 78 Athens Tel.

The results show that unilateral extensive sanctions have a large negative impact, while limited and moderate ones induce a slight positive effect on other G-7 countries

Our approach to resource management of a program is a profiling tool that collects information about the resources per function and also provides statistics regarding I/O operations

Artichoke (New York 2007) Photos and graphic design for advertising campaign for New York based modern dance company. Anna Sokolow Dance/Theatre Ensemble (New York 2007)

The findings in this study indicate that fiscal decentralization ratio, the level of dependence on the central government, capital expenditure, the age of local

LuAnn Volkmer, ARNP-C, Pediatric Care Mental Health Specialist, Blank Children’s Hospital Adolescent Clinic Stephen Wolfe, MD, Professor Emeritus, Department of Family

Where appropriate this study will refer to the proper names that predominate in the Chinese language literature and in existing English language studies: in mid-1927, the