[csharp] view plaincopyprint?
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
-
- namespace DbHelper
- {
-
-
-
- public sealed class DbUtility
- {
- public string ConnectionString { get; set; }
- private DbProviderFactory providerFactory;
-
-
-
-
-
- public DbUtility(string connectionString, DbProviderType providerType)
- {
- ConnectionString = connectionString;
- providerFactory = ProviderFactory.GetDbProviderFactory(providerType);
- if (providerFactory == null)
- {
- throw new ArgumentException("Can't load DbProviderFactory for given value of providerType");
- }
- }
-
-
-
-
-
-
- public int ExecuteNonQuery(string sql, IList<DbParameter> parameters)
- {
- return ExecuteNonQuery(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
- public int ExecuteNonQuery(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- using (DbCommand command = CreateDbCommand(sql, parameters, commandType))
- {
- command.Connection.Open();
- int affectedRows = command.ExecuteNonQuery();
- command.Connection.Close();
- return affectedRows;
- }
- }
-
-
-
-
-
-
-
- public DbDataReader ExecuteReader(string sql, IList<DbParameter> parameters)
- {
- return ExecuteReader(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
-
- public DbDataReader ExecuteReader(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- DbCommand command = CreateDbCommand(sql, parameters, commandType);
- command.Connection.Open();
- return command.ExecuteReader(CommandBehavior.CloseConnection);
- }
-
-
-
-
-
-
-
- public DataTable ExecuteDataTable(string sql, IList<DbParameter> parameters)
- {
- return ExecuteDataTable(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
- public DataTable ExecuteDataTable(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- using (DbCommand command = CreateDbCommand(sql, parameters, commandType))
- {
- using (DbDataAdapter adapter = providerFactory.CreateDataAdapter())
- {
- adapter.SelectCommand = command;
- DataTable data = new DataTable();
- adapter.Fill(data);
- return data;
- }
- }
- }
-
-
-
-
-
-
-
- public Object ExecuteScalar(string sql, IList<DbParameter> parameters)
- {
- return ExecuteScalar(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
-
- public Object ExecuteScalar(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- using (DbCommand command = CreateDbCommand(sql, parameters, commandType))
- {
- command.Connection.Open();
- object result = command.ExecuteScalar();
- command.Connection.Close();
- return result;
- }
- }
-
- public DbParameter CreateDbParameter(string name, object value)
- {
- return CreateDbParameter(name, ParameterDirection.Input, value);
- }
-
- public DbParameter CreateDbParameter(string name, ParameterDirection parameterDirection, object value)
- {
- DbParameter parameter = providerFactory.CreateParameter();
- parameter.ParameterName = name;
- parameter.Value = value;
- parameter.Direction = parameterDirection;
- return parameter;
- }
-
-
-
-
-
-
-
-
- private DbCommand CreateDbCommand(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- DbConnection connection = providerFactory.CreateConnection();
- DbCommand command = providerFactory.CreateCommand();
- connection.ConnectionString = ConnectionString;
- command.CommandText = sql;
- command.CommandType = commandType;
- command.Connection = connection;
- if (!(parameters == null || parameters.Count == 0))
- {
- foreach (DbParameter parameter in parameters)
- {
- command.Parameters.Add(parameter);
- }
- }
- return command;
- }
- }
-
-
-
- public enum DbProviderType : byte
- {
- SqlServer,
- MySql,
- SQLite,
- Oracle,
- ODBC,
- OleDb,
- Firebird,
- PostgreSql,
- DB2,
- Informix,
- SqlServerCe
- }
-
-
-
- public class ProviderFactory
- {
- private static Dictionary<DbProviderType, string> providerInvariantNames = new Dictionary<DbProviderType, string>();
- private static Dictionary<DbProviderType, DbProviderFactory> providerFactoies = new Dictionary<DbProviderType, DbProviderFactory>(20);
- static ProviderFactory()
- {
-
- providerInvariantNames.Add(DbProviderType.SqlServer, "System.Data.SqlClient");
- providerInvariantNames.Add(DbProviderType.OleDb, "System.Data.OleDb");
- providerInvariantNames.Add(DbProviderType.ODBC, "System.Data.ODBC");
- providerInvariantNames.Add(DbProviderType.Oracle, "Oracle.DataAccess.Client");
- providerInvariantNames.Add(DbProviderType.MySql, "MySql.Data.MySqlClient");
- providerInvariantNames.Add(DbProviderType.SQLite, "System.Data.SQLite");
- providerInvariantNames.Add(DbProviderType.Firebird, "FirebirdSql.Data.Firebird");
- providerInvariantNames.Add(DbProviderType.PostgreSql, "Npgsql");
- providerInvariantNames.Add(DbProviderType.DB2, "IBM.Data.DB2.iSeries");
- providerInvariantNames.Add(DbProviderType.Informix, "IBM.Data.Informix");
- providerInvariantNames.Add(DbProviderType.SqlServerCe, "System.Data.SqlServerCe");
- }
-
-
-
-
-
- public static string GetProviderInvariantName(DbProviderType providerType)
- {
- return providerInvariantNames[providerType];
- }
-
-
-
-
-
- public static DbProviderFactory GetDbProviderFactory(DbProviderType providerType)
- {
-
- if (!providerFactoies.ContainsKey(providerType))
- {
- providerFactoies.Add(providerType, ImportDbProviderFactory(providerType));
- }
- return providerFactoies[providerType];
- }
-
-
-
-
-
- private static DbProviderFactory ImportDbProviderFactory(DbProviderType providerType)
- {
- string providerName = providerInvariantNames[providerType];
- DbProviderFactory factory = null;
- try
- {
-
- factory = DbProviderFactories.GetFactory(providerName);
- }
- catch (ArgumentException e)
- {
- factory = null;
- }
- return factory;
- }
- }
- }
[csharp] view plaincopyprint?
-
- string connectionString = @"Data Source=D:\VS2008\NetworkTime\CrawlApplication\CrawlApplication.db3";
- string sql = "SELECT * FROM Weibo_Media order by Id desc limit 0,20000";
- DbUtility db = new DbUtility(connectionString, DbProviderType.SQLite);
- DataTable data = db.ExecuteDataTable(sql, null);
- DbDataReader reader = db.ExecuteReader(sql, null);
- reader.Close();
-
- string connectionString = @"Server=localhost;Database=crawldb;Uid=root;Pwd=root;Port=3306;";
- string sql = "SELECT * FROM Weibo_Media order by Id desc limit 0,20000";
- DbUtility db = new DbUtility(connectionString, DbProviderType.MySql);
- DataTable data = db.ExecuteDataTable(sql, null);
- DbDataReader reader = db.ExecuteReader(sql, null);
- reader.Close();
-
- string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/XLS/车型.xls") + ";Extended Properties=Excel 8.0;";
- string sql = "SELECT * FROM [Sheet1$]";
- DbUtility db = new DbUtility(connectionString, DbProviderType.OleDb);
- DataTable data = db.ExecuteDataTable(sql, null);