예제 #1
0
 /// <summary>
 /// 执行一条计算查询结果语句,返回查询结果(object)。
 /// </summary>
 /// <param name="SQLString">计算查询结果语句</param>
 /// <returns>查询结果(object)</returns>
 public object ExecuteScalar(string SQLString, params MySqlParameter[] paras)
 {
     using (MySqlConnection connection = new MySqlConnection(ConnStr))
     {
         using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
         {
             try
             {
                 connection.Open();
                 cmd.Parameters.AddRange(paras);
                 object obj = cmd.ExecuteScalar();
                 if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                 {
                     return(null);
                 }
                 else
                 {
                     return(obj);
                 }
             }
             catch (MySql.Data.MySqlClient.MySqlException exp)
             {
                 connection.Close();
                 LogerHelper.CreateLogTxt("数据库出错:" + exp.Message);
                 object obj = new object();
                 return(obj);
             }
         }
     }
 }
예제 #2
0
        /// <summary>
        /// 从配置文件中获取信息
        /// </summary>
        /// <param name="section">根目录</param>
        /// <param name="key">关键字</param>
        /// <param name="def">默认值</param>
        /// <returns></returns>
        public static string GetInfoFromIniFile(string section, string key, string def)
        {
            StringBuilder stringBuilder = new StringBuilder(256);

            if (File.Exists(sFileName))
            {
                //获取软件名字
                GetPrivateProfileString(section, key, def, stringBuilder, 256, sFileName);
                return(stringBuilder.ToString());
            }
            else
            {
                LogerHelper.CreateLogTxt("GetInfoFromIniFile", "配置文件不存在");
                return("");
            }
        }
예제 #3
0
 public int ExecuteNonquery(string sql)
 {
     try
     {
         using (MySqlConnection con = new MySqlConnection(ConnStr))
         {
             MySqlCommand cmd = new MySqlCommand(sql, con);
             con.Open();
             return(cmd.ExecuteNonQuery());
         }
     }
     catch (Exception exp)
     {
         LogerHelper.CreateLogTxt("数据库出错:" + exp.Message);
         return(0);
     }
 }
예제 #4
0
        public DataTable ExcuteDataTable(string sql)
        {
            DataTable dt = new DataTable();

            try
            {
                using (MySqlConnection con = new MySqlConnection(ConnStr))
                {
                    MySqlDataAdapter sqlda = new MySqlDataAdapter(sql, con);
                    sqlda.Fill(dt);
                }
            }
            catch (Exception exp)
            {
                LogerHelper.CreateLogTxt("数据库出错:" + exp.Message);
            }
            return(dt);
        }
예제 #5
0
 public static bool SetInfoToIniFile(string section, string key, string value)
 {
     if (File.Exists(sFileName))
     {
         try {
             WritePrivateProfileString(section, key, value, sFileName);
         }catch (Exception exp)
         {
             LogerHelper.CreateLogTxt("SetInfoToIniFile", "配置文件存在,写参数报错:" + section + "--" + key + "--" + value);
             return(false);
         }
         return(true);
     }
     else
     {
         LogerHelper.CreateLogTxt("SetInfoToIniFile", "配置文件不存在");
         return(false);
     }
 }
예제 #6
0
        /// <summary>
        /// 获得单个结果集时使用该方法,返回DataTable对象。
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>

        public DataTable ExcuteDataTable(string sql, params MySqlParameter[] paras)
        {
            DataTable dt = new DataTable();

            try
            {
                using (MySqlConnection con = new MySqlConnection(ConnStr))
                {
                    MySqlDataAdapter sqlda = new MySqlDataAdapter(sql, con);
                    sqlda.SelectCommand.Parameters.AddRange(paras);
                    sqlda.Fill(dt);
                }
            }
            catch (Exception exp)
            {
                LogerHelper.CreateLogTxt("数据库出错:" + exp.Message);
            }
            return(dt);
        }
예제 #7
0
 public long ExecuteNonqueryAndReturnLastInsertedId(string sql, params MySqlParameter[] paras)
 {
     try
     {
         using (MySqlConnection con = new MySqlConnection(ConnStr))
         {
             MySqlCommand cmd = new MySqlCommand(sql, con);
             cmd.Parameters.AddRange(paras);
             con.Open();
             cmd.ExecuteNonQuery();
             return(cmd.LastInsertedId);
         }
     }
     catch (Exception exp)
     {
         LogerHelper.CreateLogTxt("数据库出错:" + exp.Message);
         return(0);
     }
 }