博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
培训教程_数据库访问
阅读量:6608 次
发布时间:2019-06-24

本文共 5607 字,大约阅读时间需要 18 分钟。

oracle数据库访问

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Configuration;using System.Data.OracleClient;using System.Data;/// ///sqlHelper 的摘要说明/// public class OracleSqlHelper{    string conStr = "";    public OracleSqlHelper()    {        conStr = ConfigurationManager.ConnectionStrings["OraConStr"].ConnectionString;    }    public object ExecuteScalar(string sql)    {        object obj = null;        OracleConnection conn = new OracleConnection(conStr);        conn.Open();        OracleCommand cmd = new OracleCommand(sql, conn);        obj = cmd.ExecuteScalar();        conn.Close();        conn.Dispose();        return obj;    }    public int ExecuteNonQuery(string sql)    {        int result = 0;        OracleConnection conn = new OracleConnection(conStr);        conn.Open();        OracleCommand cmd = new OracleCommand(sql, conn);        result = cmd.ExecuteNonQuery();        conn.Close();        conn.Dispose();        return result;    }    public DataTable ExecuteDataTable(string sql)    {        DataTable result = new DataTable();        OracleConnection conn = new OracleConnection(conStr);        conn.Open();        OracleCommand cmd = new OracleCommand(sql, conn);        OracleDataAdapter oda = new OracleDataAdapter(cmd);        oda.Fill(result);        conn.Close();        conn.Dispose();        return result;    }    public DataSet ExecuteDataSet(string sql)    {        DataSet result = new DataSet();        OracleConnection conn = new OracleConnection(conStr);        conn.Open();        OracleCommand cmd = new OracleCommand(sql, conn);        OracleDataAdapter oda = new OracleDataAdapter(cmd);        oda.Fill(result);        conn.Close();        conn.Dispose();        return result;    }    public List
GetUsers(string sql) { List
users = new List
(); OracleDataReader result = null; OracleConnection conn = new OracleConnection(conStr); conn.Open(); OracleCommand cmd = new OracleCommand(sql, conn); result = cmd.ExecuteReader(); while (result.Read()) { User user = new User(); var objName = result.GetValue(result.GetOrdinal("Name")); if (objName != DBNull.Value) user.Name = objName.ToString(); var objAge = result.GetValue(result.GetOrdinal("Age")); if (objAge != DBNull.Value) user.Age = objAge.ToString(); users.Add(user); } conn.Close(); conn.Dispose(); return users; }}public class User{ public string Name { get; set; } public string Age { get; set; }}

  

 

sqlserver教程

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Configuration;using System.Data.SqlClient;using System.Data;/// ///sqlHelper 的摘要说明/// public class SqlHelper{    string conStr = "";    public SqlHelper()    {        conStr = ConfigurationManager.ConnectionStrings["SqlConStr"].ConnectionString;    }    public object ExecuteScalar(string sql)    {        object obj = null;        SqlConnection conn = new SqlConnection(conStr);        conn.Open();        SqlCommand cmd = new SqlCommand(sql, conn);        obj = cmd.ExecuteScalar();        conn.Close();        conn.Dispose();        return obj;    }    public int ExecuteNonQuery(string sql)    {        int result = 0;        SqlConnection conn = new SqlConnection(conStr);        conn.Open();        SqlCommand cmd = new SqlCommand(sql, conn);        result = cmd.ExecuteNonQuery();        conn.Close();        conn.Dispose();        return result;    }    public DataTable ExecuteDataTable(string sql)    {        DataTable result = new DataTable();        SqlConnection conn = new SqlConnection(conStr);        conn.Open();        SqlCommand cmd = new SqlCommand(sql, conn);        SqlDataAdapter oda = new SqlDataAdapter(cmd);        oda.Fill(result);        conn.Close();        conn.Dispose();        return result;    }    public DataSet ExecuteDataSet(string sql)    {        DataSet result = new DataSet();        SqlConnection conn = new SqlConnection(conStr);        conn.Open();        SqlCommand cmd = new SqlCommand(sql, conn);        SqlDataAdapter oda = new SqlDataAdapter(cmd);        oda.Fill(result);        conn.Close();        conn.Dispose();        return result;    }    public SqlDataReader ExecuteReader(string sql)    {        SqlDataReader result = null;        SqlConnection conn = new SqlConnection(conStr);        conn.Open();        SqlCommand cmd = new SqlCommand(sql, conn);        result = cmd.ExecuteReader();        return result;    }    public List
GetUsers(string sql) { List
users = new List
(); SqlDataReader result = null; SqlConnection conn = new SqlConnection(conStr); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); result = cmd.ExecuteReader(); while (result.Read()) { User user = new User(); var objName = result.GetValue(result.GetOrdinal("Name")); if (objName != DBNull.Value) user.Name = objName.ToString(); var objAge = result.GetValue(result.GetOrdinal("Age")); if (objAge != DBNull.Value) user.Age = objAge.ToString(); users.Add(user); } conn.Close(); conn.Dispose(); return users; }}public class User{ public string Name { get; set; } public string Age { get; set; }}

  程序员的基础教程:

转载地址:http://lhiso.baihongyu.com/

你可能感兴趣的文章
jenkins gitlab整合注意事项
查看>>
linux下常见的文件夹含义
查看>>
Windows下,MySQL root用户忘记密码解决方案
查看>>
Python基础之 正则表达式指南
查看>>
hdu 5735 Born Slippy 暴力
查看>>
MySQ中Lmax_connections的合理设置
查看>>
浅谈网站流量劫持防范措施 (转)
查看>>
Docker 修改默认存储位置
查看>>
Fatal error: Class ‘mysqli’ not found in解决办法
查看>>
用imagemagick和tesseract-ocr破解简单验证码
查看>>
JavaScript-求时间差
查看>>
网络干货,无论是运维还是开发都要知道的网络知识系列之(六)
查看>>
分分钟带你玩转 Web Services【1】JAX-WS
查看>>
秋招笔试碰到的疑难题目2
查看>>
ubuntu内部错误的解决办法
查看>>
dictionaryWithContentsOfFile:方法
查看>>
Leetcode: Find Right Interval
查看>>
ERP程序开发中遇到的六种错误
查看>>
web项目中解决post乱码和get乱码的方法
查看>>
运维堡垒机开发
查看>>