Sie sind vermutlich noch nicht im Forum angemeldet - Klicken Sie hier um sich kostenlos anzumelden  

Jawa 500 OHC - Forum

1952-1958

Sie können sich hier anmelden
Dieses Thema hat 0 Antworten
und wurde 21 mal aufgerufen
 English section
xiaxue123 ( Gast )
Beiträge:

26.04.2019 12:32
Harry Winks Shirt Antworten

Mysql Schema in C#

Author: rajib bahar









Figure 1.0: Screenshot of MySQL Utility.


Introduction


This article covers how to talk to MySQL database and extract the schema from it.


Background


As many of you know Kieran Trippier Hotspur Shirt , MySQL is an open source database. It is free for non-commercial purposes. This article would be helpful for anybody doing development in MySQL and C#. What is the motivation behind this utility? Well, an application could crash in the middle of database activities. Often, these databases may not reside on the same site as your developmenttesting machine. We need a utility that would help us get a snapshot of the database at client site. If you have an utility that would capture the state of the client database Hugo Lloris Hotspur Shirt , then, you can load it in your testing machine. The idea is to reproduce the situation your client faced. That way you can address any undiscovered issues.


Functionality Supported

Saving Schema as Text
Viewing Schema
Viewing Entire Database

Required Tools

ODBC.NET Data Provider from Microsoft
MySQL Database
MySQL ODBC Connector

Using the code


First, add a reference to Microsoft ODBC. Then use the using microsoft.odbc statement to tell it that you want to you MS ODBC. In short Blank Hotspur Shirt , OdbcConnection will be used to open connection, OdbcCommand to execute queries, and OdbcDataReader to read the resulting row set. The code shown below documents each step. You will notice Customized Hotspur Shirt , it runs the MySQL specific command SHOW TABLES to get the list of tables. Then it runs another query based on that particular table, SHOW COLUMNS IN CURRENT_TABLE. This is all the code does.


The Code


Collapse *

@@ Function:
@f ::PrepareSchema

@@ Description:
@d when this is called the widget is updated and everything
about this database and tables are displayed

@@ Type:
@t public

@@ Arguments:
none.
@@ Returns:
@r void

@@ Preconditions:
@c Provided that the GUI is running and DB Connection is made.

@@ Postconditions:
@o DB schema displayed

@@ References:
@e Query MySql with C#.

*

public void PrepareSchema()
{
create the connection object by setting the DSN

OdbcConnection ocConnection = new OdbcConnection("DSN="+ strDSN);

second connection is created so we could make

queries while executing one

OdbcConnection ocConnection2 = new OdbcConnection("DSN="+ strDSN);

this will open up both connections

ocConnection.Open();
ocConnection2.Open();

declare the commands for each table and column

OdbcCommand ocTableCommand;
OdbcCommand ocColumnCommand;

create a command object. this will execute SHOW TABLES

query. In mysql, it shows all of the tables contained in

the database in use.

ocTableCommand = new OdbcCommand("SHOW TABLES" Kyle Walker-Peters Shirt , ocConnection);

declare reader objects for tables and columns

OdbcDataReader odrTableReader;
OdbcDataReader odrColumnReader;

queries that return result set are executed by ExecuteReader()

If you are to run queries like insert, update, delete then

you would invoke them by using ExecuteNonQuery()

odrTableReader = ocTableCommand.ExecuteReader();

place create db statement in rich text box

rchtxtSchema.Text += "CREATE DATABASE ";
rchtxtSchema.Text += ocConnection.Database;
rchtxtSchema.Text += ";rnrn";

rchtxtSchema.Text += "USE DATABASE ";
rchtxtSchema.Text += ocConnection.Database;
rchtxtSchema.Text += ";rnrn";

string strTable = "";
string strColumnName = "";
string strColumnType = "";
string strColumnNull = "";
string strColumnPKey = "";
string strColumnDflt = "";
string strColumnExtr = "";

reader the set of tables

while(odrTableReader.Read())
{
here we are expecting rows with only 1 column

containing the table name. that's why explcity

call GetString() at 0th index

strTable = odrTableReader.GetString(0);

rchtxtSchema.Text += "CREATE TABLE ";
rchtxtSchema.Text += strTable;
rchtxtSchema.Text += "rn(rn";

build up the command for each table

ocColumnCommand = new OdbcCommand("SHOW COLUMNS IN " +
strTable Georges-Kevin N'Koudou Shirt , ocConnection2);

run the query

odrColumnReader = ocColumnCommand.ExecuteReader();

reading the set of columsn

while(odrColumnReader.Read())
{
This query returns the name of column, Type,

wherther it's Null Harry Winks Shirt , whether it's primary Key,

the default value, and extra info such as

whether it's autoincrement or not

strColumnName = odrColumnReader.GetString(0);
strColumnType = odrColumnReader.GetString(1);
strColumnNull = odrColumnReader.GetString(2);
strColumnPKey = odrColumnReader.GetString(3);
strColumnDflt = odrColumnReader.GetString(4);

strColumnExtr = odrColumnReader.GetString(5);

if (!strColumnNull.Equals("YES"))
strColumnNull = " NOT NULL ";
else
strColumnNull = "";

if (strColumnPKey.Equals("PRI"))
strColumnPKey = " PRIMARY KEY ";

this.rchtxtSchema.Text += "n";
TEHRAN Ben Davies Shirt , April 19 (Xinhua) -- Iran and Afghanistan agreed to cooperate against illicit drugs trafficking and the perils of terrorism in the region, Iranian President Hassan Rouhani said Sunday in joint press conference with his visiting Afghani counterpart Ashraf Ghani Ahmadzai.


"Concerning the issue of narcotics, which is a menace beleaguering the region Lucas Moura Shirt , including Iran and Afghanistan, we . Cheap Air Max Cheap Air Max Mens Cheap Asics White Shoes Asics Gel Nimbus Shoes Clearance Cheap Mens Asics Shoes Online Cheap Asics Shoes Online Cheap Asics Running Shoes Cheap Womens Puma Suede Cheap Puma Platform Black Cheap Mens Puma Slippers

 Sprung  

Willkommen im Oldtimer Forum für Jawa 500 OHC Motorräder von 1952-1958!

Dies ist ein kostenloses Forum, das alleinig der Erhaltung und Pflege von Jawa 500 OHC Motorrädern und der Kommunikation der Fans dieser Modelle untereinander dienen soll. Gäste können alle Beiträge lesen und beantworten, aber keine eigenen Themen erstellen. Die volle Funktionalität steht nur Mitgliedern zur Verfügung, die sich vorher mit Ihrer e-mail Adresse kostenlos angemeldet und den Link in der Bestätigungs e-mail angeklickt haben. Die Anmeldungsdaten werden beim Host des Forums gespeichert und nicht für Werbung oder ähnliche Zwecke verwendet. ACHTUNG! Das Einstellen von Bildern (Fotos, Screenschots...), die nicht nachweislich selbst erstellt, vom Urheberrechtsinhaber freigegeben oder frei von Rechten Dritter sind, ist ausdrücklich untersagt. Das Einstellen politischer oder sittenwidriger Inhalte ist ebenfalls untersagt. Der Betreiber des Forums ist nicht verantwortlich für den Inhalt von verlinkten Seiten!

Xobor Einfach ein eigenes Forum erstellen | ©Xobor.de
Datenschutz