Exemplo n.º 1
0
        /// <summary>
        /// 操作修改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public bool UpdateData(string sql)
        {
            SqlConnection sqlConn = null;
            SqlCommand    cmd     = null;

            try
            {
                sqlConn = new SqlConnection(m_ConnectionString);
                sqlConn.Open();
                cmd = new SqlCommand(sql, sqlConn);
                cmd.ExecuteNonQuery();
                return(true);
            }
            catch (Exception e)
            {
                //记录错误日志
                Sys_ErrorLog errorlog = new Sys_ErrorLog();
                errorlog.ErrorMessage = e.Message;
                errorlog.Memo         = sql;
                errorlog.UpdateTime   = DateTime.Now;
                errorlog.Save();
                return(false);
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (sqlConn != null)
                {
                    sqlConn.Close();
                }
            }
        }
Exemplo n.º 2
0
        public int InsertData(string sql)
        {
            sql += SqlHelper.SCOPE_IDENTITY;
            SqlConnection sqlConn = null;
            SqlCommand    cmd     = null;

            try
            {
                sqlConn = new SqlConnection(m_ConnectionString);
                sqlConn.Open();
                cmd = new SqlCommand(sql, sqlConn);
                return(Convert.ToInt32(cmd.ExecuteScalar()));
            }
            catch (Exception e)
            {
                Sys_ErrorLog errorlog = new Sys_ErrorLog();
                errorlog.Memo         = sql;
                errorlog.ErrorMessage = e.Message;
                errorlog.UpdateTime   = DateTime.Now;
                errorlog.Save();
                return(0);
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (sqlConn != null)
                {
                    sqlConn.Close();
                }
            }
        }
Exemplo n.º 3
0
        public int InsertData(string tableName, SqlParameter[] paramArray)
        {
            SqlConnection sqlConn   = new SqlConnection(m_ConnectionString);
            string        insertsql = GetInsertSql(tableName, paramArray);
            SqlCommand    cmd       = new SqlCommand(insertsql, sqlConn);

            try
            {
                for (int j = 0; j < paramArray.Length; j++)
                {
                    cmd.Parameters.Add(paramArray[j]);
                }
                sqlConn.Open();
                return(Convert.ToInt32(cmd.ExecuteScalar()));
            }
            catch (Exception e)
            {
                Sys_ErrorLog errorlog = new Sys_ErrorLog();
                errorlog.TableName    = tableName;
                errorlog.ErrorMessage = e.Message;
                errorlog.UpdateTime   = DateTime.Now;
                errorlog.Save();
                return(0);
            }
            finally
            {
                cmd.Dispose();
                sqlConn.Dispose();
            }
        }
Exemplo n.º 4
0
        public object ExecuteScalar(string sql)
        {
            SqlConnection sqlConn = new SqlConnection(m_ConnectionString);
            SqlCommand    cmd     = new SqlCommand(sql, sqlConn);

            try
            {
                sqlConn.Open();
                return(cmd.ExecuteScalar());
            }
            catch (Exception e)
            {
                Sys_ErrorLog errorlog = new Sys_ErrorLog();
                errorlog.TableName    = e.Message;
                errorlog.ErrorMessage = e.Message;
                errorlog.UpdateTime   = DateTime.Now;
                errorlog.Save();
                return(0);
            }
            finally
            {
                cmd.Dispose();
                sqlConn.Dispose();
            }
        }
Exemplo n.º 5
0
        public bool ExecuteNonQuery(string sql)
        {
            SqlConnection sqlConn = new SqlConnection(m_ConnectionString);
            SqlCommand    cmd     = new SqlCommand(sql, sqlConn);

            try
            {
                sqlConn.Open();
                cmd.ExecuteNonQuery();
                return(true);
            }
            catch (Exception e)
            {
                Sys_ErrorLog errorlog = new Sys_ErrorLog();
                string       m        = sql + " " + e.Message;
                errorlog.ErrorMessage = m.Substring(0, 1999);
                errorlog.UpdateTime   = DateTime.Now;
                errorlog.Save();
                return(false);
            }
            finally
            {
                cmd.Dispose();
                sqlConn.Dispose();
            }
        }
Exemplo n.º 6
0
        public bool UpdateData(string tableName, SqlParameter[] paramArray)
        {
            SqlConnection sqlConn = new SqlConnection(m_ConnectionString);

            sqlConn.Open();
            string     updatesql = GetUpdateSql(tableName, paramArray);
            SqlCommand cmd       = new SqlCommand(updatesql, sqlConn);

            try
            {
                for (int j = 0; j < paramArray.Length; j++)
                {
                    cmd.Parameters.Add(paramArray[j]);
                }

                cmd.ExecuteNonQuery();
                return(true);
            }
            catch (Exception e)
            {
                string msg = e.Message;
                //记录错误日志
                Sys_ErrorLog errorlog = new Sys_ErrorLog();
                errorlog.TableName = tableName;
                string m = updatesql + " " + e.Message;
                if (m.Length > 2000)
                {
                    m = m.Substring(0, 1999);
                }
                errorlog.ErrorMessage = m;
                errorlog.RelationId   = paramArray[0].ToString();
                errorlog.UpdateTime   = DateTime.Now;
                errorlog.Save();
                return(false);
            }
            finally
            {
                cmd.Dispose();
                sqlConn.Dispose();
            }
        }
Exemplo n.º 7
0
        public bool Send()
        {
            //使用指定的邮件地址初始化MailAddress实例
            MailAddress maddr = new MailAddress(mailFrom);
            //初始化MailMessage实例
            MailMessage myMail = new MailMessage();

            myMail.IsBodyHtml = true;
            //向收件人地址集合添加邮件地址
            string strto = "";

            if (mailToArray != null)
            {
                for (int i = 0; i < mailToArray.Length; i++)
                {
                    myMail.To.Add(mailToArray[i].ToString());
                    strto += mailToArray[i] + ";";
                }
            }

            //向抄送收件人地址集合添加邮件地址
            if (mailCcArray != null)
            {
                for (int i = 0; i < mailCcArray.Length; i++)
                {
                    myMail.CC.Add(mailCcArray[i].ToString());
                }
            }
            //发件人地址
            myMail.From = maddr;

            //电子邮件的标题
            myMail.Subject = mailSubject;

            //电子邮件的主题内容使用的编码
            myMail.SubjectEncoding = Encoding.UTF8;

            //电子邮件正文
            myMail.Body = mailBody;

            //电子邮件正文的编码
            myMail.BodyEncoding = Encoding.Default;

            myMail.Priority = MailPriority.High;

            myMail.IsBodyHtml = isbodyHtml;

            //在有附件的情况下添加附件
            try
            {
                if (attachmentsPath != null && attachmentsPath.Length > 0)
                {
                    Attachment attachFile = null;
                    foreach (string path in attachmentsPath)
                    {
                        attachFile = new Attachment(path);
                        myMail.Attachments.Add(attachFile);
                    }
                }
            }
            catch (Exception err)
            {
                throw new Exception("在添加附件时有错误:" + err);
            }

            SmtpClient smtp = new SmtpClient();

            //指定发件人的邮件地址和密码以验证发件人身份
            smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);

            //设置SMTP邮件服务器
            smtp.Host = host;

            Email email = new Email();

            email.Body     = this.mailBody;
            email.MailFrom = this.mailFrom;
            email.MailTo   = strto;
            email.Subject  = this.mailSubject;

            try
            {
                //将邮件发送到SMTP邮件服务器
                smtp.Send(myMail);
                email.IsOK = 1;
                email.Save();//记录邮件发送日志

                return(true);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                email.IsOK = 0;
                int id = email.Save();

                Sys_ErrorLog errorlog = new Sys_ErrorLog();
                errorlog.TableName  = "SendEmail";
                errorlog.Memo       = ex.Message;
                errorlog.RelationId = id.ToString();
                errorlog.Save();
                return(false);
            }
        }