खोज…


C # में SQLite का उपयोग करके सरल CRUD बनाना

सबसे पहले हमें अपने आवेदन में SQLite समर्थन जोड़ना होगा। ऐसा करने के दो तरीके हैं

  • अपने सिस्टम को SQLite डाउनलोड पृष्ठ से डाउनलोड करने वाला DLL डाउनलोड करें और फिर मैन्युअल रूप से प्रोजेक्ट में जोड़ें
  • NuGet के माध्यम से SQLite निर्भरता जोड़ें

हम इसे दूसरे तरीके से करेंगे

सबसे पहले NuGet मेनू खोलें

यहाँ छवि विवरण दर्ज करें

और System.Data.SQLite के लिए खोज करें, इसे चुनें और इंस्टॉल को हिट करें

यहाँ छवि विवरण दर्ज करें

इंस्टालेशन पैकेज मैनेजर कंसोल से भी किया जा सकता है

PM> Install-Package System.Data.SQLite

या केवल मुख्य विशेषताओं के लिए

PM> Install-Package System.Data.SQLite.Core 

यह डाउनलोड के लिए है, इसलिए हम कोडिंग में सही जा सकते हैं।

सबसे पहले इस तालिका के साथ एक सरल SQLite डेटाबेस बनाएं और इसे प्रोजेक्ट में फ़ाइल के रूप में जोड़ें

CREATE TABLE User(
  Id INTEGER PRIMARY KEY AUTOINCREMENT,
  FirstName TEXT NOT NULL,
  LastName TEXT NOT NULL
);

अपनी आवश्यकताओं के आधार पर हमेशा कॉपी की नई , तो कॉपी करने के लिए फ़ाइल की आउटपुट निर्देशिका संपत्ति में कॉपी सेट करने के लिए मत भूलना

यहाँ छवि विवरण दर्ज करें

उपयोगकर्ता नामक एक वर्ग बनाएं, जो हमारे डेटाबेस के लिए आधार इकाई होगा

private class User
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }
}

हम क्वेरी निष्पादन के लिए दो तरीके लिखेंगे, पहला डेटाबेस से डालने, अपडेट करने या निकालने के लिए

private int ExecuteWrite(string query, Dictionary<string, object> args)
{
    int numberOfRowsAffected;

    //setup the connection to the database
    using (var con = new SQLiteConnection("Data Source=test.db"))
    {
        con.Open();
        
        //open a new command
        using (var cmd = new SQLiteCommand(query, con))
        {
            //set the arguments given in the query
            foreach (var pair in args)
            {
                cmd.Parameters.AddWithValue(pair.Key, pair.Value);
            }

            //execute the query and get the number of row affected
            numberOfRowsAffected = cmd.ExecuteNonQuery();
        }

        return numberOfRowsAffected;
    }
}

और डेटाबेस से पढ़ने के लिए दूसरा

private DataTable Execute(string query)
{
    if (string.IsNullOrEmpty(query.Trim()))
        return null;

    using (var con = new SQLiteConnection("Data Source=test.db"))
    {
        con.Open();
        using (var cmd = new SQLiteCommand(query, con))
        {
            foreach (KeyValuePair<string, object> entry in args)
            {
                cmd.Parameters.AddWithValue(entry.Key, entry.Value);
            }

            var da = new SQLiteDataAdapter(cmd);

            var dt = new DataTable();
            da.Fill(dt);

            da.Dispose();
            return dt;
        }
    }
}

अब हमारे CRUD तरीकों में शामिल होने दें

उपयोगकर्ता जोड़ना

private int AddUser(User user)
{
    const string query = "INSERT INTO User(FirstName, LastName) VALUES(@firstName, @lastName)";

    //here we are setting the parameter values that will be actually 
    //replaced in the query in Execute method
    var args = new Dictionary<string, object>
    {
        {"@firstName", user.FirstName},
        {"@lastName", user.Lastname}
    };

    return ExecuteWrite(query, args);
}

उपयोगकर्ता का संपादन

private int EditUser(User user)
{
    const string query = "UPDATE User SET FirstName = @firstName, LastName = @lastName WHERE Id = @id";

    //here we are setting the parameter values that will be actually 
    //replaced in the query in Execute method
    var args = new Dictionary<string, object>
    {
        {"@id", user.Id},
        {"@firstName", user.FirstName},
        {"@lastName", user.Lastname}
    };

    return ExecuteWrite(query, args);
}

उपयोगकर्ता हटाना

private int DeleteUser(User user)
{
    const string query = "Delete from User WHERE Id = @id";

    //here we are setting the parameter values that will be actually 
    //replaced in the query in Execute method
    var args = new Dictionary<string, object>
    {
        {"@id", user.Id}
    };

    return ExecuteWrite(query, args);
}

Id द्वारा उपयोगकर्ता प्राप्त करना

private User GetUserById(int id)
{
    var query = "SELECT * FROM User WHERE Id = @id";

    var args = new Dictionary<string, object>
    {
        {"@id", id}
    };

    DataTable dt = ExecuteRead(query, args);

    if (dt == null || dt.Rows.Count == 0)
    {
        return null;
    }

    var user = new User
    {
        Id = Convert.ToInt32(dt.Rows[0]["Id"]),
        FirstName = Convert.ToString(dt.Rows[0]["FirstName"]),
        Lastname = Convert.ToString(dt.Rows[0]["LastName"])
    };

    return user;
}

एक्जीक्यूटिंग क्वेरी

using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=data.db;Pooling=true;FailIfMissing=false"))
{
    conn.Open();
    using (SQLiteCommand cmd = new SQLiteCommand(conn))
    {
       cmd.CommandText = "query";
       using (SqlDataReader dr = cmd.ExecuteReader())
       {
           while(dr.Read())
           {
               //do stuff
           }
       }
    }
}

नोट : FailIfMissing को सही पर सेट करने से फ़ाइल data.db गायब हो जाती है। हालाँकि, फ़ाइल खाली होगी। तो, किसी भी आवश्यक तालिकाओं को फिर से बनाना होगा।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow