• Tools -> Add/Remove Toolbox Items... or Tools -> Choose Toolbox Items...
  • C:/MySql/MySqlConnector.Net/bin/.Net 2.0 / MySqlData.dll
  • string connectString = String.Format( "Data Source={0};Database={1};User Id={2};Password={3}", host, database, user, pass);
  • MySqlConnection myconn = null; try { myconn = new MySqlConnection( connectString ); myconn.Open(); } catch ( MySqlException e )
  • MySqlCommand cmd = new MySqlCommand("SHOW TABLES;", myconn); MySqlDataReader reader = cmd.ExecuteReader( )
  • MySqlDataReader reader = null; try { reader = cmd.ExecuteReader( ); if ( reader == null ) { Console.WriteLine("ExecuteReader failed");
  • Console.WriteLine("caught exception " + e.Message ); } finally { if (reader != null) reader.Close(); }
  • Mysql connection using ado. Net Connecting to Mysql from. Net languages




    Download 89,57 Kb.
    Sana23.05.2024
    Hajmi89,57 Kb.
    #251276
    Bog'liq
    6-Database-Csharp uz.pptx [Repaired]


    MySQL Connection using ADO.Net

    ADO.Net Arxitektura
    • ADO.Net ilova va ma'lumotlar manbai o'rtasida aloqa o'rnatish uchun arxitekturani taqdim etadi.
    • "Ma'lumotlar manbai" kerakli API-ga ega bo'lgan har qanday narsa bo'lishi mumkin, lekin odatda bu ma'lumotlar bazasi serveridir.

    Ma'lumot Manbai
    Ulanish Ob'ekti (ADO.Net)
    Ma'lumot Adapteri
    Ma'lumotlar To'plami (Mahalliy)
    Dastur
    MySQL ADO.Net Provayder
    MySqlConnection MySQL ma'lumotlar bazasiga asosiy ulanishmi
    MySqlCommand ma'lumotlar bazasiga qarshi har qanday buyruqni bajarishga imkon beradi.
    MySqlDataReader ma'lumotlar bazasiga tez, faqat oldinga o'qish imkoniyatini beradi.
    MySqlDataAdapter MySQL ma'lumotlar sinflari va Microsoft ma'lumotlar to'plami o'rtasida interfeys sifatida xizmat qiladi.
    MySqlParameter buyruq uchun dinamik parametrlarni saqlash uchun ishlatiladi.
    MySqlTransaction MySQL tranzaksiyasini ifodalash uchun ishlatiladi.

    Getting MySqlConnector.Net

    • Net dasturlari uchun sizga MySql "ulagichi" kerak.
    • Yuklab olish

    • http://dev.mysql.com/downloads/
    • O'rnatuvchini boshqaring.
    • DLL topilishi uchun ulagich o'zini "Global Assambleya Keshi" bilan ro'yxatdan o'tkazadi.
      • Java-dan farq: Java foydalanadi Sinf yo'li kodni topish uchun; Visual Studio resurslarni topish uchun oyna registridan foydalanadi.
    • (Vizual dasturlash uchun ixtiyoriy) Visual Studio-dagi Asboblar qutisiga komponentlarni qo'shing: Tools -> Add/Remove Toolbox Items... or Tools -> Choose Toolbox Items...

    C Undefined MySql nom maydoni#
    • O'rnatishdan keyin MySqlConnector.Net, loyihangizda siz uning nomini C # manba kodingizga qo'shasiz

    • using MySql.Data.MySqlClient;
    • lekin, agar "MySql" nomi kosmik topilmadi bir tuzuvchi xato olish mumkin.
    • bunday holda, ulagichning DLL fayliga havola qo'shing:
      • Loyiha - > Malumot Qo'shish - > Ko'rish
      • .Net2.0 MySqlData topish.DLL fayli, masalan:

      • C:/MySql/MySqlConnector.Net/bin/.Net 2.0 / MySqlData.dll
        Bu nom maydoni muammosini hal qilishi kerak.

    Ulanish ob'ektini yaratish
    • Ulanish ob'ekti ma'lumotlar bazasi serveriga ulanishni boshqaradi.
    • Siz ko'rsatishingiz kerak: server nomi, foydalanuvchi nomi, parol
    • Keraksiz atributlarni tashlab qo'yishi yoki bo'sh qoldirishi mumkin.

    string connectString = "Data Source=localhost;Database=bank;User Id=bank;Password=FatChance";
    MySqlConnection myconn = new MySqlConnection( connectString );
    MySqlConnection myconn = yangi MySqlConnection ( connectString );
    public DBConnection(string host, string database, string user, string pass) {
    string connectString = String.Format(
    "Data Source={0};Database={1};User Id={2};Password={3}",
    host, database, user, pass);
    MySqlConnection myconn = new MySqlConnection( connectString );
    }
    Better programming:
    Ulanishni ochish
    • Ulanishni Yaratgandan so'ng, uni oching.
    • Bu MySqlException otish mumkin

    MySqlConnection myconn = null;
    try {
    myconn = new MySqlConnection( connectString );
    myconn.Open();
    }
    catch ( MySqlException e )
    {
    Console.WriteLine("Error connecting to server: "+e.Message);
    }
    Buyruq ob'ektini yaratish
    • Cmds ma'lumotlar bazasini chiqarish uchun MySqlCommand ob'ektidan foydalaning
      • Buyruq ob'ekti Java bayonoti ob'ektiga o'xshaydi.
    • Siz buyruq ob'ektini qayta ishlatishingiz mumkin.
    • Agar ulanish ob'ektini talab (myconn) pul sifatida.

    Buyruqni bajarish usuli SQL bayonotiga bog'liq:
    • Yangilash, kiritish, o'chirish: cmd.ExecuteNonQuery () qaytadi int.
    • Ko'rsatish (so'rov): cmd.ExecuteReader () mysqldatareader qaytaradi

    Yarim yo'g'on ichak
    MySqlCommand cmd = new MySqlCommand("SHOW TABLES;", myconn);
    MySqlDataReader reader = cmd.ExecuteReader( )
    So'rov Ma'lumotlarini Qayta Ishlash
    • MySqlDataReader-da ustun raqami, ustun nomi yoki indeks bo'yicha ma'lumotlarni olishning ko'plab usullari mavjud.
    • Yordamida natijalari ustidan Iterate (boolean) o'quvchi.O'qish( )

    MySqlDataReader reader = null;
    try {
    reader = cmd.ExecuteReader( );
    if ( reader == null ) {
    Console.WriteLine("ExecuteReader failed");
    return;
    }
    while( reader.Read() ) {
    Console.WriteLine( reader.GetString(0) ); }
    } catch ( MySqlException e) {
    Console.WriteLine("caught exception " + e.Message );
    } finally {
    if (reader != null) reader.Close();
    }
    Manbalar
    MySQL
    http://dev.mysql.com/tech-resources/articles/dotnet/
    Learning SQL
    http://www.w3schools.com/sql/ nice tutorial and command reference
    Learning JDBC
    JDBC Trail in Sun's Java Tutorial.
    Dietel, Java How To Program, Chapter 25.
    ... and zillions of resources on the web
    Download 89,57 Kb.




    Download 89,57 Kb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Mysql connection using ado. Net Connecting to Mysql from. Net languages

    Download 89,57 Kb.