{"id":18329865,"url":"https://github.com/somdipdey/easysql","last_synced_at":"2025-04-13T04:12:29.317Z","repository":{"id":83917523,"uuid":"110380958","full_name":"somdipdey/EasySQL","owner":"somdipdey","description":"Use this class library to execute your MS SQL queries easily without hassle. ","archived":false,"fork":false,"pushed_at":"2017-11-28T00:14:05.000Z","size":34,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T19:09:38.804Z","etag":null,"topics":["csharp","csharp-code","csharp-library","database","dataset","dictionary","easysql","ms-sql-queries","mssql","nuget","nuget-gallery","nuget-package","sql","sql-statement","sqlhelper","tsql"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/somdipdey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-11T21:16:20.000Z","updated_at":"2024-12-09T18:08:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"35e2b163-8841-4f79-bb65-ca1966c3d835","html_url":"https://github.com/somdipdey/EasySQL","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/somdipdey%2FEasySQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/somdipdey%2FEasySQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/somdipdey%2FEasySQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/somdipdey%2FEasySQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/somdipdey","download_url":"https://codeload.github.com/somdipdey/EasySQL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248661722,"owners_count":21141451,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","csharp-code","csharp-library","database","dataset","dictionary","easysql","ms-sql-queries","mssql","nuget","nuget-gallery","nuget-package","sql","sql-statement","sqlhelper","tsql"],"created_at":"2024-11-05T19:18:58.834Z","updated_at":"2025-04-13T04:12:29.286Z","avatar_url":"https://github.com/somdipdey.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EasySQL\nUse this class library to execute your MS SQL queries in your C# programs easily without hassle. \n\n# Background\n\nDo you find yourself writing a handful of codes to execute a single or number of SQL statement(s) in your C# program using System.Data.SqlClient?\nDo your code somewhat looks like this as follows:\n\n        SqlConnection conn = new SqlConnection(SQLConnectionString);\n        SqlCommand comm = null;\n        int returnResult;\n\n        try\n        {\n            comm = conn.CreateCommand();\n            comm.CommandType = CommandType.Text;\n            comm.CommandText = Query;\n            comm.CommandTimeout = SQLTimeout;\n                try\n                {\n                    conn.Open();\n                    returnResult = comm.ExecuteNonQuery();\n                }\n                catch\n                {\n                    conn.Close();\n                    throw new SqlException();\n                }\n        }\n        catch (Exception ex)\n        {\n            throw new Exception(ex.Message.ToString());\n        }\n        finally\n        {\n            conn.Close(); // Always remember to close connection to database or else it would lead to memory leakage\n        }\n\n\nAnd don't even mention that what headache this code can cause to you in case you mess up or forget a single line of code from the above.\n\nBut do not worry, there is an easy solution. \n\n# Enters The EasySQL Class\nUse EasySQL class library to execute any (or list of) SQL statement(s) without hassle.\n\nLet me show you how you can write the aforementioned code using EasySQL Class as follows:\n\n        EasySQL thisSQL = new EasySQL(SQLConnectionString);\n        thisSQL.Execute(Query);\n\nThat's it. Nothing else. You don't have to worry about opening or closing database connections or worry about other small stuffs, so that you can devote more in developing your solution effectively rather than fighting to execute database queries.\n\n# Methods In EasySQL Class\n#### Name:\t  Returns:\t Description\n\nIsConnected():\t bool:\t Returns true or false whether connection to database is successful or not\n\nGetConnectionString():\t String:\t Returns the connection string which is set/used\n\nGetTimeout():\t Int:\tReturns the connection time out\n\nExecute(String):\tInt:\tExecutes the query and returns the number of rows affected\n\nExecute(String, out DataSet):\tInt:\tExecutes the query to fetch the data in dataset and returns the number of rows affected\n\nExecute(String, out DataTable):\t        Int:\tExecutes the query to fetch the data in datatable and returns the number of rows affected\n\nExecute(String, Dictionary\u003cString, String\u003e):\tInt:\tExecutes the query with parameters and returns the number of rows affected\n\nExecute(String, Dictionary\u003cString, String\u003e, out DataSet):\tInt:\tExecutes the query with parameters to fetch the data in dataset and returns the number of rows affected\n\nExecute(String, Dictionary\u003cString, String\u003e, out DataTable):\t        Int:\tExecutes the query with parameters to fetch the data in datatable and returns the number of rows affected\n\n\nExecuteProcedure(String):\tInt:\tExcutes the stored procedure and returns the number of rows affected\n\nExecuteProcedure(String, out DataSet):\t        Int:\tExcutes the stored procedure to fetch data in dataset and returns the number of rows affected\n\nExecuteProcedure(String, out DataTable):\tInt:\tExcutes the stored procedure to fetch data in datatable and returns the number of rows affected\n\nExecute(String, SqlCredential):\t        Int:\tExcutes the query using SqlCredential for elevated security and returns the number of rows affected\n\nExecute(String, SqlCredential, out DataSet):\t        Int:\tExecutes the query using SqlCredential for elevated security to fetch the data in dataset and returns the number of rows affected\n\nExecute(String, SqlCredential, out DataTable):\t        Int:\tExecutes the query using SqlCredential for elevated security to fetch the data in datatable and returns the number of rows affected\n\nExecute(String, Dictionary\u003cString, String\u003e, SqlCredential):\t        Int:\tExcutes the query with parameters using SqlCredential for elevated security and returns the number of rows affected\n\nExecute(String, Dictionary\u003cString, String\u003e, SqlCredential, out DataSet):\t        Int:\tExecutes the query with parameters using SqlCredential for elevated security to fetch the data in dataset and returns the number of rows affected\n\nExecute(String, Dictionary\u003cString, String\u003e, SqlCredential, out DataTable):\t        Int:\tExecutes the query with parameters using SqlCredential for elevated security to fetch the data in datatable and returns the number of rows affected\n\n\nExecuteProcedure(String, SqlCredential):\tInt:\tExecutes the stored procedure using SqlCredential for elevated security and returns the number of rows affected\n\nExecuteProcedure(String, SqlCredential, out DataSet):   \tInt:\tExecutes the stored procedure using SqlCredential for elevated security to fetch data in dataset and returns the number of rows affected\n\nExecuteProcedure(String, SqlCredential, out DataTable): \tInt:\tExecutes the stored procedure using SqlCredential for elevated security to fetch data in datatable and returns the number of rows affected\n\n\n# Some Examples\nLet's insert a new record of city in our CITIES table as follows:\n\n        private void InsertCityRecord(string connectionString)\n        {\n          string query = \"Insert Into [CITIES] (CityName, Country) VALUES('Kolkata', 'India')\";\n\n          EasySQL thisSQL = new EasySQL(connectionString);\n          thisSQL.Execute(query);\n        }\n\nOr we can fetch user credentials from a form and use that to query our database as follows:\n\n        private void runUserQuery()\n        {\n          // Fetch User input\n          System.Windows.Controls.TextBox txtUserId = new System.Windows.Controls.TextBox();\n          System.Windows.Controls.PasswordBox txtPwd = new System.Windows.Controls.PasswordBox();\n\n          // Read config for connection string\n          Configuration config = Configuration.WebConfigurationManager.OpenWebConfiguration(Null);\n          ConnectionStringSettings connString = config.ConnectionStrings.ConnectionString[“MyConnString”];\n\n          // Create sql credential\n          SecureString pwd = txtPwd.SecurePassword;\n          pwd.MakeReadOnly();\n          SqlCredential cred = new SqlCredential(txtUserId.Text, pwd);\n\n          // Execute using EasySQL\n          EasySQL thisSQL = new EasySQL(connString);\n          thisSQL.ExecuteProcedure(storedProcedureName, cred);\n        }\n\nOr we can also execute a list of queries as follows:\n\n        private void runListOfQueries(List\u003cstring\u003e queries, string connectionString)\n        {\n          // Initiate EasySQL\n          EasySQL thisSQL = new EasySQL(connectionString);\n\n          // Iterate over list of queries and execute one by one\n          foreach(string query in queries)\n          {\n            thisSQL.Execute(query);\n          }\n        }\n\n# Some More Real World Examples\n\nLet's assume we have a table in the database named, Customers, with the following fields: Name: varchar, Email: varchar, Age: int, Adult: varchar, IsBritish: boolean. You would now like to query the table using EasySQL class in the following ways:\n\t\t\t\n\t    // Example 1::\n            // Connection string to your database\n            string connectionString = \"Data Source=ServerName; Initial Catalog = DatabaseName; User ID = UserName; Password=Password\";\n            // initiate the EasySQL object\n            EasySQL.EasySQL thisSQL = new EasySQL.EasySQL(connectionString, 300);\n\n            // Your required query to return the names and associated emails of customers\n            string query = \"Select Name, Email from Customers Where Age = @age \";\n            // Required parameters\n            Dictionary\u003cstring, string\u003e param = new Dictionary\u003cstring, string\u003e();\n            param.Add(\"@age\", \"26\");\n            // Data table which will hold returned result of the query\n            DataTable dt = new DataTable();\n            try\n            {\n                // Execute the query inside a try/catch to catch exceptions as they happen\n                thisSQL.Execute(query, param, out dt);\n            }\n            catch (Exception ex) { throw new Exception(ex.Message); }\n\n            // Read the names and associated emails from the data table\n            if (dt.Rows.Count \u003e 0)\n            {\n                foreach (DataRow dr in dt.Rows)\n                {\n                    var name = dr[\"Name\"].ToString();\n                    var email = dr[\"Email\"].ToString();\n                }\n            }\n            \n            \n\t    // Example 2::\n            // The same aforementioned query without any parameter--\u003e\n            string connectionString = \"Data Source=ServerName; Initial Catalog = DatabaseName; User ID = UserName; Password=Password\";\n            EasySQL.EasySQL thisSQL = new EasySQL.EasySQL(connectionString, 300);\n\n            string query = \"Select Name, Email from Customers\";\n            DataTable dt = new DataTable();\n            try\n            {\n                thisSQL.Execute(query, out dt);\n            }\n            catch (Exception ex) { throw new Exception(ex.Message); }\n\n            if (dt.Rows.Count \u003e 0)\n            {\n                foreach (DataRow dr in dt.Rows)\n                {\n                    var name = dr[\"Name\"].ToString();\n                    var email = dr[\"Email\"].ToString();\n                }\n            }\n            \n\t    // Example 3::\n            // And an update query on your customer database as follows --\u003e\n            string connectionString = \"Data Source=ServerName; Initial Catalog = DatabaseName; User ID = UserName; Password=Password\";\n            EasySQL.EasySQL thisSQL = new EasySQL.EasySQL(connectionString, 300);\n\n            string query = \"Update Customers Set Adult = @adult Where Age = @age \";\n            Dictionary\u003cstring, string\u003e param = new Dictionary\u003cstring, string\u003e();\n            param.Add(\"@adult\", \"true\");\n            param.Add(\"@age\", \"26\");\n            try\n            {\n                int rowsAffected = thisSQL.Execute(query, param);\n            }\n            catch (Exception ex) { throw new Exception(ex.Message); }\n\t\n\t\n\t    // Example 4::\n            // And another query to fetch all customers' names who are British --\u003e\n            string connectionString = \"Data Source=ServerName; Initial Catalog = DatabaseName; User ID = UserName; Password=Password\";\n            EasySQL.EasySQL thisSQL = new EasySQL.EasySQL(connectionString, 300);\n\n            string query = \"Select Name from Customers where IsBritish = @british \";\n            Dictionary\u003cstring, string\u003e param = new Dictionary\u003cstring, string\u003e();\n            param.Add(\"@british\", \"1\"); // For boolean the value to be passed is 1 (true) or 0 (false).\n\t\t\tDataTable dt = new DataTable();\n            try\n            {\n                int rowsAffected = thisSQL.Execute(query, param, out dt);\n            }\n            catch (Exception ex) { throw new Exception(ex.Message); }\n\t\t\t\n            if (dt.Rows.Count \u003e 0)\n            {\n                foreach (DataRow dr in dt.Rows)\n                {\n                    var name = dr[\"Name\"].ToString();\n                }\n            }\n\n\n\n# Extras\n\nThe source code of the EasySQL Class library is available to download from TechNet Gallery and GitHub.\n\nThis article is also available from TechNet Wiki here: https://social.technet.microsoft.com/wiki/contents/articles/48290.using-easysql-class-to-execute-ms-sql-and-stored-procedures-easily-in-c.aspx\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsomdipdey%2Feasysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsomdipdey%2Feasysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsomdipdey%2Feasysql/lists"}