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 ListGetUsers(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 ListGetUsers(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; }}
程序员的基础教程: