c#

位置:IT落伍者 >> c# >> 浏览文章

C#如何使用数据库


发布日期:2023年01月14日
 
C#如何使用数据库

数据库在我们开发项目中那是最常见的了无论复杂的项目还是简单的项目只要涉及数据的操作(增删改查)都会用到数据库当然使用的数据库的种类根据项目的需求不同也是不同的在我所做的项目中就是用过ACCESS SQL Server Oracle g因此简单总结下c#如何使用数据库的程序执行sql语句(包括单条sql多条sql存储过程函数等等)大体有三个步骤连接数据库执行sql语句返回结果那么我们来看看c#是如何完成这三个步骤的无论哪种数据库都是这三个步骤那么就不按数据库分了按照返回结果的类型来分类吧

执行sql语句返回受影响的行数

一条sql语句

view plainprint?

public static int ExecuteNonQuery(string connStr string sql)

{ OleDbConnection conn = nulltry { conn = new OleDbConnection(connStr)connOpen()OleDbCommand command = new OleDbCommand(sql conn)return commandExecuteNonQuery()} catch { return } finally { if (conn != null)

connClose()}(多条sql语句返回受影响行数由于多条sql为了保证数据库的正确性最好使用事务方式运行

view plainprint?

public static int ExecuteNonQuery(string connStr List<string> sqlList)

{ int executeCount =

OleDbTransaction transaction = null

OleDbConnection conn = nulltry { conn = new OleDbConnection(connStr)connOpen()

transaction = connBeginTransaction()

OleDbCommand command = new OleDbCommand()

commandConnection = conn

commandTransaction = transaction

for (int i = i < sqlListCount i++)

{ commandCommandText = sqlList[i]

executeCount += commandExecuteNonQuery()} transactionCommit()} catch { if (transaction != null)

transactionRollback()return } finally { if (conn != null)

connClose()} return executeCount} 返回DataSet

view plainprint?

public static DataSet ExecuteQueryReturnDataSet(string connStr string sql)

{ OleDbConnection conn = nullDataSet ds = nulltry { conn = new OleDbConnection(connStr)ds = new DataSet()connOpen()

OleDbDataAdapter command = new OleDbDataAdapter(sql conn)commandFill(ds datasetName} catch { } finally { if (conn != null)

connClose()} return ds} 返回OleDbDataReader

view plainprint?

public static OleDbDataReader ExecuteQueryReturnReader(string sql string connStr)

{ OleDbConnection conn = new OleDbConnection()OleDbDataReader reader = null

try { conn = new OleDbConnection(connStr)connOpen()

OleDbCommand command = connCreateCommand()commandCommandText = sqlreader = commandExecuteReader()} catch { } finally { if (conn != null)

connClose()} return reader} 执行存储过程并返回受影响行数(以Oracle为例)

view plainprint?

public static int RunProcedure(string storedProcName IDataParameter[] parameters)

{ int result = try { using (OracleConnection connection = new OracleConnection(GetConnectionString()))

{ connectionOpen()

OracleCommand command = BuildQueryCommand(connection storedProcName parameters)

result = commandExecuteNonQuery()} catch (Exception e)

{

}

return result} private static OracleCommand BuildQueryCommand(OracleConnection connection string storedProcName

IDataParameter[] parameters)

{ OracleCommand command = new OracleCommand(storedProcName connection)

commandCommandType = CommandTypeStoredProcedure

foreach (OracleParameter parameter in parameters)

{ commandParametersAdd(parameter)}

return command} 带参数的sql语句

view plainprint?

public static int ExecuteNonQuery(string sqlstring connStr OleDbParameter[] commandParameters)

{ int count = OleDbCommand cmd = new OleDbCommand()

OleDbConnection conn = nulltry { conn = new OleDbConnection(connStr)PrepareCommand(cmd connsql commandParameters)count = cmdExecuteNonQuery()cmdParametersClear()} catch { } finally {

if (conn != null)

{ connClose()} return count}

private static void PrepareCommand(OleDbCommand cmd OleDbConnection conn string sql OleDbParameter[] cmdParms)

{ if (connState != ConnectionStateOpen)

connOpen()

cmdConnection = conncmdCommandText = sqlcmdCommandType = CommandTypeText

if (cmdParms != null)

{ foreach (OleDbParameter parm in cmdParms)

cmdParametersAdd(parm)}一般常用的就这么几种数据库的几个重要对象分别是OleDbCommandOleDbConnectionOleDbDataReaderOleDbDataAdapterDataSet对于不同数据库有几个对象需要对应相应数据库的对象OleDbCommandOleDbConnectionOleDbDataReaderOleDbDataAdapterOracle下OleDb换成OracleSql Server下 则换成SQL

               

上一篇:ado.net快速上手实践篇(二)

下一篇:基于.Net Framework的N层分布式应用开发(2)