@manhng

Welcome to my blog!

UI Controls

December 26, 2018 12:36

UI Controls (edit)

UI-Bootstrap Datepicker

http://embed.plnkr.co/OVh5h7GJYkthVwCUkZwJ/

Using AngularJS

https://dalelotts.github.io/angularjs-bootstrap-datetimepicker/

https://codepen.io/jagathish/pen/EZwqPM

Using HTML, Support Input

https://www.sitepoint.com/finding-date-picker-input-solution-bootstrap/

https://codepen.io/SitePoint/pen/RNryzy

SQL Server + CSharp (C#)

http://csharp.net-informations.com/

https://www.c-sharpcorner.com/

https://www.c-sharpcorner.com/technologies/windows-forms

https://csharp.hotexamples.com/

https://github.com/Fast-Forward-llc/FFLib/

AngularJS

https://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single

SQL Server

https://www.codeproject.com/Articles/6936/Paging-of-Large-Resultsets-in-ASP-NET

https://blog.sqlauthority.com/sql-server-interview-questions-and-answers/

DBProviderFactory

https://blog.codeinside.eu/2016/12/31/dbproviderfactory-write-database-agnostic-adonet-code/

Data Access Layer - Rick Strahl

A general purpose utility and helper library for .NET development

https://github.com/RickStrahl/Westwind.Utilities

Data Access Layer - Manh Nguyen

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StringBuilder stringBuilder = new StringBuilder();
            DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

            using (IDbConnection dbConnection = dbProviderFactory.CreateConnection())
            {
                dbConnection.ConnectionString = "Server=MANHNV\\SQL;database=master;Integrated Security=SSPI;";

                dbConnection.Open();

                try
                {
                    using (IDbCommand dbCommand = dbConnection.CreateCommand())
                    {
                        dbCommand.CommandType = CommandType.Text;
                        dbCommand.CommandText = "SELECT servicename, service_account FROM sys.dm_server_services";

                        using (IDataReader dataReader = dbCommand.ExecuteReader())
                        {
                            while (dataReader.Read())
                            {
                                stringBuilder.Append(System.Environment.NewLine);
                                stringBuilder.Append("Service Name".PadRight(45));
                                stringBuilder.Append(dataReader[0]);
                                stringBuilder.Append(System.Environment.NewLine);
                                stringBuilder.Append("Service Account".PadRight(45));
                                stringBuilder.Append(dataReader[1]);
                                stringBuilder.Append(System.Environment.NewLine);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    stringBuilder.Length = 0;
                    stringBuilder.Append(ex.ToString());
                }
            }

            MessageBox.Show(stringBuilder.ToString());
        }

        public static bool TestTransaction()
        {
            //http://www.zentut.com/sql-tutorial/sql-sample-database/
            //https://dotnetstories.wordpress.com/2011/11/09/a-thorough-look-into-transactions-in-sql-server/
            /*
             *  USE AdventureWorks;
             *  GO
             *  BEGIN TRANSACTION;
             *
             *  BEGIN TRY
             *      -- Generate a constraint violation error.
             *      DELETE FROM Production.Product
             *      WHERE ProductID = 980;
             *  END TRY
             *  BEGIN CATCH
             *      SELECT
             *          ERROR_NUMBER() AS ErrorNumber
             *          ,ERROR_SEVERITY() AS ErrorSeverity
             *          ,ERROR_STATE() AS ErrorState
             *          ,ERROR_PROCEDURE() AS ErrorProcedure
             *          ,ERROR_LINE() AS ErrorLine
             *          ,ERROR_MESSAGE() AS ErrorMessage;
             *
             *      IF @@TRANCOUNT > 0
             *          ROLLBACK TRANSACTION;
             *  END CATCH;
             *
             *  IF @@TRANCOUNT > 0
             *      COMMIT TRANSACTION;
             *  GO
             */
            DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

            using (IDbConnection dbConnection = dbProviderFactory.CreateConnection())
            {
                dbConnection.ConnectionString = "Server=MANHNV\\SQL;database=master;Integrated Security=SSPI;";

                dbConnection.Open();

                IDbTransaction dbTransaction = dbConnection.BeginTransaction();

                try
                {
                    using (IDbCommand dbCommand = dbConnection.CreateCommand())
                    {
                        dbCommand.Transaction = dbTransaction;
                        dbCommand.CommandType = CommandType.Text;
                        dbCommand.CommandText = "INSERT INTO Table1 (Id, Data) VALUES (@ID, @DT)";
                        dbCommand.CreateParameter().AddWithValue("@ID", 1);
                        dbCommand.CreateParameter().AddWithValue("@DT", 1);
                        dbCommand.ExecuteNonQuery();
                    }

                    using (IDbCommand dbCommand = dbConnection.CreateCommand())
                    {
                        dbCommand.Transaction = dbTransaction;
                        dbCommand.CommandType = CommandType.Text;
                        dbCommand.CommandText = "INSERT INTO Table2 (Id, Data) VALUES (@ID, @DT)";
                        dbCommand.CreateParameter().AddWithValue("@ID", 1);
                        dbCommand.CreateParameter().AddWithValue("@DT", 1);
                        dbCommand.ExecuteNonQuery();
                    }

                    dbTransaction.Commit();
                }
                catch (Exception)
                {
                    dbTransaction.Rollback();
                }
            }
            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder stringBuilder = new StringBuilder();

            try
            {
                //SQL Server connection strings
                //https://www.connectionstrings.com/sql-server/
                //https://www.connectionstrings.com/all-sql-server-connection-string-keywords/
                //https://stackoverflow.com/questions/20558043/the-right-connection-string-for-remote-sql-server-for-c-sharp
                //https://www.dofactory.com/reference/connection-strings
                string usr = "sa";
                string pwd = "123456a@";
                string srv = $"MANHNV\\SQL";
                int port = 1433;
                int connectTimeout = 30;
                string connStr = $"Data Source={srv};Uid={usr};Pwd={pwd};Connect Timeout={connectTimeout};";
                string myConnStr = $"Data Source=10.255.3.156,{port};Uid={usr};Pwd={pwd};Connect Timeout={connectTimeout};";
                string serviceName = string.Empty;
                string serviceAccount = string.Empty;
                using (var conn = new SqlConnection(connStr))
                {
                    string cmdText = $"SELECT servicename, service_account FROM sys.dm_server_services";
                    using (var cmd = new SqlCommand(cmdText, conn))
                    {
                        conn.Open();
                        var sqlDataReader = cmd.ExecuteReader();
                        while (sqlDataReader.Read())
                        {
                            serviceName = sqlDataReader.GetString(0);
                            serviceAccount = sqlDataReader.GetString(1);

                            stringBuilder.Append(System.Environment.NewLine);
                            stringBuilder.Append("Service Name".PadRight(45));
                            stringBuilder.Append(serviceName);
                            stringBuilder.Append(System.Environment.NewLine);
                            stringBuilder.Append("Service Account".PadRight(45));
                            stringBuilder.Append(serviceAccount);
                            stringBuilder.Append(System.Environment.NewLine);
                        }
                    }
                }
                MessageBox.Show(stringBuilder.ToString());
            }
            catch (Exception ex)
            {
                stringBuilder.Length = 0;
                stringBuilder.Append(ex.ToString());
                MessageBox.Show(ex.ToString());
            }
        }
    }

    public static class MyExtensions
    {
        public static void AddWithValue(this IDbDataParameter command, string parameterName, object parameterValue)
        {
            command.ParameterName = parameterName;
            command.Value = parameterValue;
        }
    }
}

Categories

Recent posts