1:连接oracle
A:安装oracle服务端(11G/10G)
B:添加引用:
C:引入命名空间:using System.Data.OracleClient;
D:连接字符:
/// <summary> /// 获取连接对象 /// </summary> /// <param name="user">用户名</param> /// <param name="password">密码</param> /// <param name="url">连接url,如(localhost:1521/orcl)</param> /// <returns>OracleConnection</returns> public OracleConnection getOracleConnection(String user,String password,String url) { String source = "Data source="+url+";Integrated Security=no;User ID="+user+";Password="+password; OracleConnection oraConn = new OracleConnection(source); oraConn.Open(); return oraConn; }
2:连接sqlCE
A:安装SQLCE
B:添加引用:
C:引入命名空间:using System.Data.SQLite;
D:测试连接
/// <summary> /// 获取连接对象 /// </summary> /// <param name="source">数据源(数据库存放路径),如:E:\dataSource\PostingSys.sdf</param> /// <param name="password">密码</param> /// <returns>SqlCeConnection</returns> public SqlCeConnection getSqlceConnection(String source,String password) { source = @"Data Source="+source+";Password="+password; SqlCeConnection sqlce = new SqlCeConnection(source); sqlce.Open(); return sqlce; }
3:连接SQLite
A:安装SQLite
B:添加引用:
C:命名空间:using System.Data.SQLite;
D:测试连接:
/// <summary> /// 获取连接对象 /// </summary> /// <param name="source">数据源(数据库存放路径),如E:\dataSource\openises.db</param> /// <returns>SQLiteConnection连接对象</returns> public SQLiteConnection getSQLiteConnection(String db) { SQLiteConnection slc = new SQLiteConnection("DateTimeKind = Utc;Data Source = " + db); slc.Open(); return slc; }
4:由于本人发现c#对数据库的操作重复性很高,下面只贴出对oracle封装操作。因为其他两个思想也基本一致,只是对象及方法不同。下载
- Oracle
更新
/// <summary> /// 操作:update\add\delete /// 如果是采用更新的话,则需注意以下 /// 1:更新指定其字段,保存占位符的参数不能为null /// </summary> /// <param name="oc">连接对象</param> /// <param name="sqlText">执行的sql语句</param> /// <param name="arrayPara">占位符参数</param> /// <returns>int 返回操作后改变的行数</returns> public int ExecuteUpdate(OracleConnection oc,String sqlText,List<OracleParameter> arrayPara) { OracleCommand oraCom = null; int edit = 0; try{ oraCom = new OracleCommand(); oraCom.Connection = oc; oraCom.CommandText = sqlText; foreach(OracleParameter op in arrayPara){ oraCom.Parameters.Add(op); } oraCom.Transaction = oc.BeginTransaction();//开启事务 edit = oraCom.ExecuteNonQuery(); oraCom.Transaction.Commit();//事务提交后,就把事务对象为null,但会话没有结束! }catch(Exception ex){ if(oraCom!=null){ oraCom.Transaction.Rollback();//回滚 } Console.WriteLine("\n操作失败,异常:{0}",ex.Message); throw; } return edit; }查询
/// <summary> /// 操作:查询 /// 默认是查询所有。sqlText为条件后语句 /// </summary> /// <param name="oc">Oracle连接对象</param> /// <param name="sqlText">条件语句(and xx=:xx)</param> /// <param name="arrayPara">占位符参数(如:id_x,:name_x)</param> /// <param name="clazz">查询表对应的实体类</param> /// <returns>list符合条件数据</returns> public List<Object> ExecuteSelect(OracleConnection oc,String sqlText,List<OracleParameter> arrayPara,Type clazz) { List<Object> clazzArray = new List<Object>(); //执行对象 OracleCommand comm = new OracleCommand(); comm.CommandText = "select * from " + clazz.Name + " where 1=1 " +sqlText; comm.Connection = oc; //参数 foreach(OracleParameter op in arrayPara){ comm.Parameters.Add(op); } //执行sql OracleDataReader dataReader = comm.ExecuteReader(); int fieLen = dataReader.FieldCount; while(dataReader.Read()) { Object objTemp = Activator.CreateInstance(clazz); //获取每一行的每一个列。 for(int x = 0; x < fieLen;x++) { String fieName = dataReader.GetName(x); Object fieValue= dataReader.GetValue(x); FieldInfo fieInfo = clazz.GetField(fieName,BindingFlags.IgnoreCase|BindingFlags.NonPublic|BindingFlags.Instance); /*类型转换:*/ String typeName = fieInfo.FieldType.Name; if(fieValue.ToString()==String.Empty || fieValue.ToString().Length < 1) continue; fieValue = TransformationUtils.typeTransform(typeName,fieValue); fieInfo.SetValue(objTemp,fieValue); } clazzArray.Add(objTemp); } return clazzArray; }
工具类下载