コード例 #1
0
        /// <summary>
        /// 执行SQL语句,返回影响的记录数,select类型的语句此方法不可行。
        /// </summary>
        public static int ExecuteSql(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                using (SqlTransaction tran = connection.BeginTransaction())
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.Connection  = connection;
                        cmd.Transaction = tran;
                        try
                        {
                            cmd.CommandText = SQLString;
                            int rows = cmd.ExecuteNonQuery(); //-1
                            tran.Commit();
                            return(rows);
                        }
                        catch (Exception ex)
                        {
                            LogHelp.Log(ex.Message);

                            tran.Rollback();
                            connection.Close();
                            return(0);
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: JsonUtils.cs プロジェクト: joyhen/DbTools
        /// <summary>
        /// 反序列化
        /// </summary>
        public static T DeserializeObject <T>(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(default(T));
            }

            try
            {
                return(JsonConvert.DeserializeObject <T>(str));
            }
            catch (System.Exception ex)
            {
                LogHelp.Log(ex.Message);
                return(default(T));
            }
        }