SQL数据库基本操作、生成日志文件的方法

//数据库所在服务器IP
string servername = "192.168.2.137";
//数据库名字
string sqlname = "myTest";
//数据库账号
string usname= "myTest";
//数据库密码
string pwd = "123456";
//表名
string tablename="user";
//连接数据库语句
string strSql = "Server=" + servername + ";database=" + dbname + ";uid=" + usname + ";pwd=" + pwd + ";";
//建表方法
private static void creatTable(){
    using (SqlConnection conn = new SqlConnection(mConnString)){
    try{
        if(conn.State== ConnectionState.Closed){
         conn.Open();
        }
        StringBuilder sqlBuider = new StringBuilder("CREATE TABLE "+tablename+"(")
        .Append("[id] [int] IDENTITY(1,1) NOT NULL,")
        .Append("[UserName] [nvarchar](50) NULL,")
        .Append("[UserPwd] [nvarchar](200) NULL")
        SqlCommand cmd = new SqlCommand(sqlBuider.ToString(),conn);
        cmd.ExecuteNonQuery();
    }catch (Exception ex){
        conn.Close();
    }
  }
}
// 执行 修改、删除、添加的sql语句
private static Boolean update(string sql){
    using(SqlConnection conn = new SqlConnection(mConnString)){
    try{
        if(conn.State== ConnectionState.Closed){
         conn.Open();
        }
        SqlCommand com = new SqlCommand(sql,conn);
        int result = com.ExecuteNonQuery();
        conn.Close();
        if (result > 0) return true;
       }
     catch(Exception ex){
        conn.Close();
     }
     return false;
    }
}

// 事务执行 修改、删除、添加的sql语句
private static Boolean updateTran(List<string> sqlList,IsolationLevel leave){
    if (sqlList.Count == 0) return false;
    using(SqlConnection conn = new SqlConnection(mConnString)){
    SqlTransaction tran = null;
    try{
     
        if(conn.State== ConnectionState.Closed){
         conn.Open();
        }
        tran = conn.BeginTransaction(leave);
        for (int index = 0; index < sqlList.Count; index++){
             SqlCommand com = new SqlCommand(sqlList[index],conn);
             com.Transaction = tran;
             int result = com.ExecuteNonQuery();
             conn.Close();
             if (result > 0) {
               tran.Rollback();
               return true;
             }    
        }
       tran.Commit();
       return true; 
       }
     catch(Exception ex){
      if (tran != null){
        tran.Rollback();
         }
        conn.Close();
     }
     return false;
    }
}
 //获取查询的记录集
public static  DataTable getTable(string sql){
  using (SqlConnection conn = new SqlConnection(mConnString)){
    try{
        if(conn.State== ConnectionState.Closed){
           conn.Open();
        }
       SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);
       DataTable dt = new DataTable();
       adapter.Fill(dt);
       conn.Close();
       return dt;
    }catch (Exception ex){
        conn.Close();
    }
  }

//添加日志文件
public static bool NoteLog(string line){
try{
   string Fpath = System.Web.HttpContext.Current.Server.MapPath("~/bin/log");
       if (!Directory.Exists(Fpath))
      {
       Directory.CreateDirectory(Fpath);
       }
       StreamWriter Note = new StreamWriter(Fpath + "/" + DateTime.Now.ToString("yyyyMMdd") + ".log", true, Encoding.Default);
       Note.WriteLine(DateTime.Now.ToString() + "##" + line + "\r\n");
       Note.Close();  
    }
 catch (Exception ex){
       
    }
    return true;
}

}

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值