Example #1
0
        //4.SqlSelect - Thực thi câu lệnh Select trả về một Dataset
        public static DataSet SqlSelect(string sQuery, string strConn)
        {
            DataSet oDs = new DataSet();

            try
            {
                SqlDataAdapter oSqlDa = new SqlDataAdapter();
                oSqlDa.SelectCommand                = new SqlCommand("SET DATEFORMAT DMY;\r\n" + sQuery);
                oSqlDa.SelectCommand.Connection     = objConn;
                oSqlDa.SelectCommand.CommandTimeout = 60;
                if (ClsSQL.Connect(strConn) == true)//Ket noi
                {
                    oSqlDa.Fill(oDs);
                }
                ClsSQL.Disconnect();
            }
            catch
            {
                ClsSQL.Disconnect();
                Clipboard.SetText("SET DATEFORMAT DMY;\r\n" + sQuery);
                MessageBox.Show("SET DATEFORMAT DMY;\r\n" + sQuery, "SQL Server select fail", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }
            return(oDs);
        }
Example #2
0
        //3.Viết phương thức SqlExecute - Thực thi câu lệnh SQL Insert, Update, Delete
        public static bool SqlExecute(string sQuery, string strConn)
        {
            using (SqlCommand oSqlComm = new SqlCommand())
            {
                oSqlComm.Connection     = ClsSQL.objConn;
                oSqlComm.CommandText    = "SET DATEFORMAT DMY;\r\n" + sQuery;
                oSqlComm.CommandTimeout = 60;
                //Kết nối
                ClsSQL.Connect(strConn);
                SqlTransaction oTran = ClsSQL.objConn.BeginTransaction();//Transaction
                oSqlComm.Transaction = oTran;
                try
                {
                    oSqlComm.ExecuteNonQuery();//Thực thi câu lệnh SQL
                    oTran.Commit();
                    ClsSQL.Disconnect();
                }
                catch
                {
                    oTran.Rollback();
                    ClsSQL.Disconnect();
                    Clipboard.SetText("SET DATEFORMAT DMY;\r\n" + sQuery);
                    MessageBox.Show("SET DATEFORMAT DMY;\r\n" + sQuery, "SQL Server execute fail", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
            }

            return(true);
        }