1.日期毫秒转换为标准的C#日期格式
//使用时,先将秒Convert.ToInt64,返回格式2015-2-10 14:03:33 public DateTime JavaTimeToC(long ltime) { long time_JAVA_Long = ltime;//java长整型日期,毫秒为单位 DateTime dt_1970 = new DateTime(1970, 1, 1); long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度 long time_tricks = tricks_1970 + time_JAVA_Long * 10000;//日志日期刻度 DateTime dt = new DateTime(time_tricks, DateTimeKind.Unspecified).AddHours(8);//转化为DateTime return dt; }
2.带事务的数据库插入更新删除方法
public void DbOp(List<string> sqlNum, ref string error) { if (sqlNum.Count <= 0) { error = "sql语句为空,没有订单数据!"; return; } try { if (!string.IsNullOrEmpty(strDbConnection))//缺少数据库连接字符串 { using (OracleConnection con = new OracleConnection(strDbConnection)) { using (OracleCommand cmd = con.CreateCommand()) { con.Open(); OracleTransaction trans = con.BeginTransaction();//开始事务 cmd.Transaction = trans;//设置事务 string[] sql = sqlNum.ToArray(); try { for (int i = 0; i < sql.Length; i++) { cmd.CommandText = sql[i]; cmd.ExecuteNonQuery(); } trans.Commit();//提交事务 } catch (Exception ex) { error += ex; trans.Rollback();//回滚 } finally { con.Close();//始终关闭数据库连接 } } } } else { //缺少数据库连接字符串 error += "缺少数据库连接字符串"; } } catch (Exception ex) { error += ex; } }
3.数据库查询方法
public DataSet Query(string SQLString) { using (OracleConnection connection = new OracleConnection(strDbConnection)) { DataSet ds = new DataSet(); try { connection.Open(); OracleDataAdapter command = new OracleDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.OracleClient.OracleException ex) { throw new Exception(ex.Message); } finally { connection.Close(); } return ds; } }
4.DataTime,转换为string类型
public string DateTimeChange(DateTime dt) { string strDT = dt.Year.ToString(); if (dt.Month.ToString().Length == 1) { strDT += "-0" + dt.Month.ToString(); } else { strDT += "-" + dt.Month.ToString(); } if (dt.Day.ToString().Length == 1) { strDT += "-0" + dt.Day.ToString(); } else { strDT += "-" + dt.Day.ToString(); } if (dt.Hour.ToString().Length == 1) { strDT += " 0" + dt.Hour.ToString(); } else { strDT += " " + dt.Hour.ToString(); } if (dt.Minute.ToString().Length == 1) { strDT += ":0" + dt.Minute.ToString(); } else { strDT += ":" + dt.Minute.ToString(); } if (dt.Second.ToString().Length == 1) { strDT += ":0" + dt.Second.ToString(); } else { strDT += ":" + dt.Second.ToString(); } return strDT; }
时间: 2024-10-07 21:00:06