Executing Queries and Commands c# In Hindi

Executing Queries and Commands c# In Hindi

C# प्रोग्रामिंग लैंग्वेज में ADO.NET में डेटाबेस क्वेरी और कमांड रिज़र्व स्टेटमेंट होते है, जिसमे SqlCommand या SQL सर्वर के लिए या दूसरे डेटाबेस प्रोवाइडर-स्पेसिफिक कमांड ऑब्जेक्ट जैसे, OleDbCommand, OdbcCommand, और OracleCommand का यूज़ करके डायरेक्ट एग्जीक्यूट किए जाते हैं. यह कम्प्लीटली इस बात पर डिपेंड करता है कि C# यूजर डेटाबेस एक्सट्रेक्ट और मैनेज ऑपरेशन में किस डेटा प्रोवाइडर सॉफ्टवेयर या एप्लीकेशन का यूज़ कर रहे हैं। ये सभी कमांड SQL डेटाबेस क्वेरी जैसे, SELECT, INSERT, UPDATE, DELETE, कमांड स्टेटमेंट या डेटाबेस पर यूजर डिफाइन डेटाबेस स्टोर्ड प्रोसीजर ऑपरेशन को एग्जीक्यूट करने में हेल्प करते हैं।

Executing Queries and Commands c# In Hindi

So, let’s learn more about how to execute queries and commands in ADO.NET using C# programming.

Executing Database Queries with SqlCommand.

SELECT Query Concept with SqlDataReader.

किसी डेटाबेस क्वेरी में डेटा की रो को रिटर्न करने वाली SELECT कमांड स्टेटमेंट क्वेरी को एग्जीक्यूट करने के लिए, C# यूजर ExecuteReader() डेटाबेस फंक्शन मेथड के साथ SqlCommand स्टेटमेंट ऑब्जेक्ट का यूज़ कर सकते हैं। यह मेथड मौजूदा डेटाबेस में एक SqlDataReader वैल्यू को रिटर्न करता है. जो किसी यूजर डिफाइन डेटाबेस डेटा को रिट्रीव करने का एक फॉरवर्ड-ओनली, रीड-ओनली मेथड या स्टेप प्रोवाइड करता है।

Example of executing a SELECT query to fetch data from a database.

using System;

using System.Data.SqlClient;

class Program

{

    static void Main()

    {

        string connectionString =

            “Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True”;

        string query = “SELECT Id, Name, Age, FROM Employees”;

        using (SqlConnection conn = new SqlConnection(connectionString))

        {

            conn.Open();

            using (SqlCommand command = new SqlCommand(query, conn))

            {

                using (SqlDataReader reader = command.ExecuteReader())

                {

                    // here it Read the data from the SqlDataReader method

                    while (reader.Read())

                    {

                        Console.WriteLine(

                            $”Emp Id – {reader[“Id”]}, Employee Age – {reader[“Age”]}, Employee Name – {reader[“Name”]}”

                        );

                    }

                }

            }

        }

    }

}

SELECT query to fetch data Explanation.

  • यहाँ इस एक्साम्प्ल में  ExecuteReader() मेथड फाइल डेटा रीड करने के लिए एक SqlDataReader वैल्यू को रिटर्न करता है। जहा हर डेटाबेस रिकॉर्ड को रो-बाय-रो डिस्प्ले करने के लिए SqlDataReader.Read() फंक्शन या मेथड को लूप में कॉल किया जाता है।
  • यहाँ डेटाबेस कनेक्शन SqlConnection का यूज़ करके इसे ओपन किया जाता है, और कमांड SqlCommand क्वेरी स्टेटमेंट का यूज़ करके इसे रन किया जाता है।

Insert / Update / Delete ExecuteNonQuery concept.

यहाँ हम एम्प्लॉई टेबल में डेटाबेस कनेक्शन एस्टाब्लिशड कर इन्सर्ट, अपडेट, डिलीट sql डेटाबेस कमांड ऑपरेशन अप्लाई करने के लिए यूजर डायरेक्ट एम्प्लॉई डेटाबेस कनेक्शन क्रिएट कर नई डेटाबेस टेबल रौ को इन्सर्ट, अपडेट और जरूरत पड़ने पर डिलीट भी कर सकते है.

Example of Insert/Update/Delete ExecuteNonQuery.

using System.Data.SqlClient;

string connectionString = “your_connection_string”;

using (SqlConnection conn = new SqlConnection(connectionString))

{

    conn.Open();

    string query = “INSERT INTO Employee(Id, Name, Age) VALUES(@id, @name, @age)”;

    using (SqlCommand cmd = new SqlCommand(query, conn))

    {

        cmd.Parameters.AddWithValue(“@id”, “101”);

        cmd.Parameters.AddWithValue(“@name”, “Siddhi”);

        cmd.Parameters.AddWithValue(“@age”, 19);

        int rowsAffected = cmd.ExecuteNonQuery();

        Console.WriteLine($”New employee database Rows inserted – {rowsAffected}”);

    }

}

Executing a Query / ExecuteReader concept.

यहाँ हम एम्प्लॉई टेबल में ADO.NET ExecuteReader मेथड फंक्शन का यूज़ करके एम्प्लॉई डेटाबेस टेबल से पर्टिकुलर रिकॉर्ड डेटाबेस को एक्सट्रेक्ट कर रिट्रीव कर सकते है.

example of Executing a Query / ExecuteReader.

using System.Data.SqlClient;

string query = “SELECT Id, Name, Age FROM Employees”;

using (SqlConnection conn = new SqlConnection(connectionString))

{

    conn.Open();

    using (SqlCommand cmd = new SqlCommand(query, conn))

    using (SqlDataReader reader = cmd.ExecuteReader())

    {

        while (reader.Read())

        {

            Console.WriteLine(

                $”{reader[“Id”]} – {reader[“Name”]} – {reader[“Age”]}”

            );

        }

    }

}

Getting a Single database Value with ExecuteScalar concept.

यहाँ हम एम्प्लॉई टेबल में ADO.NET ExecuteScalar फंक्शन मेथड का यूज़ करके किसी डेटाबेस टेबल से नंबर ऑफ़ रिकार्ड्स को काउंट कर वैल्यू एक्सट्रेक्ट कर सकते है.

example of Single database Value with ExecuteScalar.

string query = “SELECT COUNT(*) FROM Employees”;

using (SqlConnection conn = new SqlConnection(connectionString))

{

    conn.Open();

    using (SqlCommand cmd = new SqlCommand(query, conn))

    {

        int count = (int)cmd.ExecuteScalar();

        Console.WriteLine($”Here it Total Count Employees Numbers – {count}”);

    }

}

Leave a Reply