Beispiel #1
0
 public static string Dt2Json(string jsonName, DataTable dt)
 {
     try
     {
         StringBuilder Json = new StringBuilder();
         if (jsonName != "")
         {
             Json.Append("{\"" + jsonName + "\":[");
         }
         else
         {
             Json.Append("{[");
         }
         if (dt.Rows.Count > 0)
         {
             for (int i = 0; i < dt.Rows.Count; i++)
             {
                 Json.Append("{");
                 for (int j = 0; j < dt.Columns.Count; j++)
                 {
                     Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                     if (j < dt.Columns.Count - 1)
                     {
                         Json.Append(",");
                     }
                 }
                 Json.Append("}");
                 if (i < dt.Rows.Count - 1)
                 {
                     Json.Append(",");
                 }
             }
         }
         Json.Append("]}");
         return(Json.ToString());
     }
     catch (Exception ex)
     {
         GLog.LogError(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  方法名:GFun:Dt2Json  异常:" + ex.Message);
         return("");
     }
 }
Beispiel #2
0
        /// <summary>
        /// 执行 非查询 sql语句(插入,删除,修改等)


        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <returns>返回命令影响行数 返回-1时命令出错</returns>
        public static int ExecuteNonQuery(String strSql, SqlConnection cnn)
        {
            SqlCommand cmd     = new SqlCommand(strSql, cnn);
            int        nReturn = -1;

            try
            {
                cnn.Open();
                nReturn = cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                GLog.LogError("ExecuteNonQuery", e);
            }
            finally
            {
                cnn.Close();
                cnn = null;
            }

            return(nReturn);
        }
Beispiel #3
0
        /// <summary>
        /// 执行 非查询 sql语句(插入,删除,修改等)


        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <param name="cmdType">命令字符串类型,如text,StoredProcedure</param>
        /// <param name="cmdParms">命令执行参数</param>
        /// <returns>返回命令影响行数 返回-1时命令出错</returns>
        public static int ExecuteNonQuery(String strSql, CommandType cmdType, SqlParameter[] cmdParms, SqlConnection cnn)
        {
            SqlCommand cmd     = PrepareCommand(cnn, null, cmdType, strSql, cmdParms);
            int        nReturn = -1;

            try
            {
                cnn.Open();
                nReturn = cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                GLog.LogError("ExecuteNonQuery", e);
            }
            finally
            {
                cnn.Close();
                cnn = null;
            }

            return(nReturn);
        }
Beispiel #4
0
        /// <summary>
        /// 执行返回查询第一行第一列内容命令


        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <param name="cmdType">命令字符串类型,如text,StoredProcedure</param>
        /// <param name="cmdParms">命令执行参数</param>
        /// <returns>返回查询结果中的第一行第一列</returns>
        public static object ExecuteScalar(String strSql, CommandType cmdType, SqlParameter[] cmdParms, SqlConnection cnn)
        {
            SqlCommand cmd     = PrepareCommand(cnn, null, cmdType, strSql, cmdParms);
            object     oReturn = null;

            try
            {
                cnn.Open();
                oReturn = cmd.ExecuteScalar();
            }
            catch (SqlException e)
            {
                GLog.LogError("ExecuteScalar", e);
            }
            finally
            {
                cnn.Close();
                cnn = null;
            }

            return(oReturn);
        }
Beispiel #5
0
        /// <summary>
        /// 执行返回查询第一行第一列内容命令


        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <returns>返回查询结果中的第一行第一列</returns>
        public static object ExecuteScalar(String strSql, SqlConnection cnn)
        {
            SqlCommand cmd     = new SqlCommand(strSql, cnn);
            object     oReturn = null;

            try
            {
                cnn.Open();
                oReturn = cmd.ExecuteScalar();
            }
            catch (SqlException e)
            {
                GLog.LogError("ExecuteScalar", e);
            }
            finally
            {
                cnn.Close();
                cnn = null;
            }

            return(oReturn);
        }
Beispiel #6
0
        /// <summary>
        ///  执行查询命令,尽量不要使用此函数
        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <returns>返回SqlDataReader</returns>
        public static SqlDataReader ExecuteReader(String strSql, SqlConnection cnn)
        {
            SqlCommand    cmd     = new SqlCommand(strSql, cnn);
            SqlDataReader oReader = null;

            try
            {
                cnn.Open();
                oReader = cmd.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess);
            }
            catch (SqlException e)
            {
                GLog.LogError("ExecuteReader", e);
            }
            catch (Exception e)
            {
                GLog.LogError("ExecuteReader", e);
            }
            finally
            {
            }

            return(oReader);
        }