C#:数据库操作(待补充)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms; namespace MyCommanHelper
{
public class DataBaseHelper
{ #region 字段变量 public static volatile string Connstring = "";
private static volatile OleDbConnection connection = null; #endregion #region 方法 #region Access // Methods
/// <summary>
/// 获取Access2003连接字符串
/// </summary>
/// <param name="sFilePath">Access文件路径</param>
/// <returns></returns>
public static string GetAccess2003ConnectionString(string sFilePath)
{
return ("Provider=Microsoft.JET.OLEDB.4.0;data source=" + sFilePath);
} /// <summary>
/// 获取Access2007连接字符串
/// </summary>
/// <param name="sFilePath">Access文件路径</param>
/// <returns></returns>
public static string GetAccess2007ConnectionString(string sFilePath)
{
return ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath);
} #endregion #region SqlServer #endregion #region Oracle
public static string GetOracleBlobConnectionString(string sUser, string sPassword, string sServiceName)
{
return ("server=oratest;Data Source=" + sServiceName + ";User ID=" + sUser + ";Password=" + sPassword);
} public static string GetOracleConnectionString(string sUser, string sPassword, string sServiceName)
{
return ("Provider=OraOLEDB.Oracle.1;Data Source=" + sServiceName + ";User ID=" + sUser + ";Password=" + sPassword);
} #endregion #region 公共 /// <summary>
/// 初始化新实例
/// </summary>
/// <returns></returns>
public static bool Init()
{
try
{
connection = new OleDbConnection(Connstring);
return true;
}
catch (Exception)
{
return false;
}
} /// <summary>
/// 执行非查询操作
/// </summary>
/// <param name="sqlStr">执行SQL语句</param>
/// <returns>返回影响行数,值为-1 表示未创建连接实例或执行语句错误</returns>
public static int ExecNonQuery(string sqlStr)
{
int rslt = -1; if (null == connection)
{
MessageBox.Show("无连接实例!");
return rslt;
} try
{
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
OleDbCommand command = new OleDbCommand(sqlStr, connection);
rslt = command.ExecuteNonQuery();
command.Dispose();
//connection.Close();
//connection.Dispose();
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
return rslt;
} /// <summary>
/// 获取只包含指定的数据的数据集
/// </summary>
/// <param name="sqlStr">Sql指令</param>
/// <param name="sTable">指定数据</param>
/// <returns>数据集 值为null 表示未创建连接实例</returns>
public static DataSet ExecQuery(string sqlStr, string sTable)
{
DataSet set2 = null;
if (null == connection)
{
MessageBox.Show("无连接实例!");
return set2;
} try
{
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlStr, connection);
DataSet dataSet = new DataSet();
if (dataSet.Tables.Contains(sTable))
{
dataSet.Tables[sTable].Clear();
}
adapter.Fill(dataSet, sTable);
set2 = dataSet;
}
catch (Exception exception)
{
throw exception;
}
finally
{
connection.Close();
//connection.Dispose();
//connection = null;
}
return set2;
} /// <summary>
/// 获得第一行数据
/// </summary>
/// <param name="sql"></param>
/// <param name="sTable"></param>
/// <returns></returns>
public static DataRow GetFirstRow(string sql, string sTable)
{
DataRow row = null;
DataSet set = ExecQuery(sql, sTable);
if (set.Tables[sTable].Rows.Count > 0)
{
row = set.Tables[sTable].Rows[0];
}
return row;
} /// <summary>
/// 获得第一行第一个字段的值
/// </summary>
/// <param name="sql"></param>
/// <param name="sTable"></param>
/// <returns></returns>
public static string GetFirstFieldValue(string sql, string sTable)
{
string str = "";
DataSet set = ExecQuery(sql, sTable);
if (set.Tables[sTable].Rows.Count > 0)
{
DataRow row = set.Tables[sTable].Rows[0];
str = row[0].ToString();
}
return str;
} /// <summary>
/// 释放连接资源
/// </summary>
public static void Dispose()
{
if (null != connection)
{
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
connection.Dispose();
connection = null;
}
} #endregion #endregion }
}
C#:数据库操作(待补充)的更多相关文章
- 如何在高并发环境下设计出无锁的数据库操作(Java版本)
一个在线2k的游戏,每秒钟并发都吓死人.传统的hibernate直接插库基本上是不可行的.我就一步步推导出一个无锁的数据库操作. 1. 并发中如何无锁. 一个很简单的思路,把并发转化成为单线程.Jav ...
- 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~
最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...
- MySQL 系列(二) 你不知道的数据库操作
第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 本章内容: 查看\创建\使用\删除 数据库 用户管理及授权实战 局域网 ...
- ABP创建数据库操作步骤
1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...
- 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- django数据库操作和中间件
数据库配置 django的数据库相关表配置在models.py文件中,数据库的连接相关信息配置在settings.py中 models.py相关相关参数配置 from django.db import ...
- [Android Pro] 完美Android Cursor使用例子(Android数据库操作)
reference to : http://www.ablanxue.com/prone_10575_1.html 完美 Android Cursor使用例子(Android数据库操作),Androi ...
- phpcms v9 中的数据库操作函数
1.查询 $this->select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='') 返回 ...
- Android打造属于自己的数据库操作类。
1.概述 开发Android的同学都知道sdk已经为我们提供了一个SQLiteOpenHelper类来创建和管理SQLite数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...
- python之数据库操作
数据库操作 Python 操作 Mysql 模块的安装 1 2 3 4 5 linux: yum install MySQL-python window: http://files ...
随机推荐
- apache&mod_wsgi&django部署多个项目
今天做好了第二个django项目,但在部署时出了一点小问题,在此记录一下. 1.mod_wsgi 3.4已经支持了部署多个项目,只需在httpd.conf中进行如下配置: WSGIScriptAlia ...
- iOS开发读取plist文件、iphone中plist文件的
在Xcode中建立一个iOS项目后,会自己产生一个.plist文件,点击时会看见它显示的是类似于excel表格: 但是,如果打开方式选择Source Code,你会看见它其实是一个xml文件. 我们会 ...
- weblogic打补丁,bsu方法
刚装了10.3.6版本的weblogic,想把版本补丁到10.3.6.0.12 我用的系统是windows 8.1 ,呵呵 查看版本 执行java weblogic.version WebLogic ...
- Model工具
Sybase PowerDesigner - 一个高端数据建模工具.你可以下载一个45天试用版. ERWin - 一个高端数据建模工具.可下载试用版. Rational Rose Enterprise ...
- 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)
设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...
- 触摸事件【MotionEvent】简介
MotionEvent简介 当用户触摸屏幕时,将创建一个MontionEvent对象,MotionEvent包含了关于发生触摸的位置.时间信息,以及触摸事件的其他很多细节. Android 将所有的输 ...
- C++对C的改进(2)
本文地址:http://www.cnblogs.com/archimedes/p/cpp-change2.html,转载请注明源地址 区别一:原型声明的区别 原型声明的概念: 函数要先定义再使用,如果 ...
- js 构造函数(construction)与原型(prototype)
1.面向对象:js原型 java有class和instance,js仅仅有构造函数(function Cat(name,age){this.name=name;this.age=age}),为了实现数 ...
- [React] Use the useReducer Hook and Dispatch Actions to Update State (useReducer, useMemo, useEffect)
As an alternate to useState, you could also use the useReducer hook that provides state and a dispat ...
- es5 - array - reverse
/** * 描述:也就是数组元素反转 * 使用:arr.reverse() */ var a = [1,2,3]; console.log(a.reverse()); /** * 一个经典的问题:如何 ...