using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
public class DataBase
{
private string ConnStr = null;
public DataBase()
{
ConnStr = ConfigurationSettings.AppSettings["ConnStr"];
}
public void RunProc(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd ;
Cmd = CreateCmd(SQL, Conn);
try
{
Cmd.ExecuteNonQuery();
}
catch
{
throw new Exception(SQL);
}
Dispose(Conn);
return;
}
}
在另外调用的时候 写
DataBase db = new DataBase ();
string sql = ""//这里是你要执行的SQL语句
db.RunProc(sql);//ok了。
如果是 ASP.NET 把 ConnStr 连接字符串配置在WEB CONFIG 中就可以了。或者直接将
ConnStr = ConfigurationSettings.AppSettings["ConnStr"]改为 ConnStr= "Data Source=数据库名;User
ID=用户名;Password=密码;" ;
ORCALE 牵涉到事务的问题。给你一个底层类。你自己看着调用吧。
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
namespace gds.DBUtility
{
///
/// 数据访问抽象基础类
/// Copyright (C) 2004-2008 LiTianPing
/// All rights reserved
///
public abstract class DbHelperSQL
{
//数据库连接字符串(web.config来配置)
public static string connectionString = ConfigurationManager.AppSettings
["gdsconstring"];
public DbHelperSQL()
{
}
#region 公用方法
public static int GetMaxID(string FieldName, string TableName)
{
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
object obj = DbHelperSQL.GetSingle(strsql);
if (obj == null)
{
return 1;
}
else
{
return int.Parse(obj.ToString());
}
}
public static bool Exists(string strSql)
{
object obj = DbHelperSQL.GetSingle(strSql);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
public static bool Exists(string strSql, params OracleParameter[] cmdParms)
{
object obj = DbHelperSQL.GetSingle(strSql, cmdParms);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
#endregion
#region 执行简单SQL语句
///
/// 执行SQL语句,返回影响的记录数
///
/// SQL语句
///
public static int ExecuteSql(string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.OracleClient.OracleException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
///
/// 执行SQL语句,设置命令的执行等待时间
///
///
///
///
public static int ExecuteSqlByTime(string SQLString, int Times)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand(SQLString, connection))
{
try
{
connection.Open();
cmd.CommandTimeout = Times;
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.OracleClient.OracleException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
///
/// 执行多条SQL语句,实现数据库事务。
///
/// 多条SQL语句
public static void ExecuteSqlTran(ArrayList SQLStringList)
{
using (OracleConnection conn = new OracleConnection(connectionString))
{
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
OracleTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (System.Data.OracleClient.OracleException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
///
/// 执行一条计算查询结果语句,返回查询结果(object)。
///
/// 计算查询结果语句
///
public static object GetSingle(string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj,
System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.OracleClient.OracleException e)
{
connection.Close();
throw new Exception(e.Message);
}
}
}
}
///
/// 执行查询语句,返回SqlDataReader(使用该方法切记要手工关闭SqlDataReader和连接)
///
/// 查询语句
///
public static OracleDataReader ExecuteReader(string strSQL)
{
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand cmd = new OracleCommand(strSQL, connection);
try
{
connection.Open();
OracleDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.OracleClient.OracleException e)
{
throw new Exception(e.Message);
}
//finally //不能在此关闭,否则,返回的对象将无法使用
//{
// cmd.Dispose();
// connection.Close();
//}
}
///
/// 执行查询语句,返回DataSet
///
/// 查询语句
///
public static DataSet Query(string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.OracleClient.OracleException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
///
/// 执行查询语句,返回DataSet,设置命令的执行等待时间
///
///
///
///
public static DataSet Query(string SQLString, int Times)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
command.SelectCommand.CommandTimeout = Times;
command.Fill(ds, "ds");
}
catch (System.Data.OracleClient.OracleException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
#endregion
#region 执行带参数的SQL语句
///
/// 执行SQL语句,返回影响的记录数
///
/// SQL语句
///
public static int ExecuteSql(string SQLString, params OracleParameter[] cmdParms)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.OracleClient.OracleException E)
{
throw new Exception(E.Message);
}
}
}
}
///
/// 执行多条SQL语句,实现数据库事务。
///
/// SQL语句的哈希表(key为sql语句,value是该语句的
SqlParameter[])
public static void ExecuteSqlTran(Hashtable SQLStringList)
{
using (OracleConnection conn = new OracleConnection(connectionString))
{
conn.Open();
using (OracleTransaction trans = conn.BeginTransaction())
{
OracleCommand cmd = new OracleCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Key.ToString();
OracleParameter[] cmdParms = (OracleParameter[])myDE.Value;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
trans.Commit();
}
}
catch
{
trans.Rollback();
throw;
}
}
}
}
///
/// 执行一条计算查询结果语句,返回查询结果(object)。
///
/// 计算查询结果语句
///
public static object GetSingle(string SQLString, params OracleParameter[] cmdParms)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj,
System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.OracleClient.OracleException e)
{
throw new Exception(e.Message);
}
}
}
}
///
/// 执行查询语句,返回SqlDataReader (使用该方法切记要手工关闭SqlDataReader和连接)
///
/// 查询语句
///
public static OracleDataReader ExecuteReader(string SQLString, params OracleParameter[]
cmdParms)
{
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand cmd = new OracleCommand();
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
OracleDataReader myReader = cmd.ExecuteReader();
cmd.Parameters.Clear();
return myReader;
}
catch (System.Data.OracleClient.OracleException e)
{
throw new Exception(e.Message);
}
//finally //不能在此关闭,否则,返回的对象将无法使用
//{
// cmd.Dispose();
// connection.Close();
//}
}
///
/// 执行查询语句,返回DataSet
///
/// 查询语句
///
public static DataSet Query(string SQLString, params OracleParameter[] cmdParms)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
OracleCommand cmd = new OracleCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (OracleDataAdapter da = new OracleDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
}
catch (System.Data.OracleClient.OracleException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
}
private static void PrepareCommand(OracleCommand cmd, OracleConnection conn,
OracleTransaction trans, string cmdText, OracleParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (OracleParameter parameter in cmdParms)
{
if ((parameter.Direction == ParameterDirection.InputOutput ||
parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
cmd.Parameters.Add(parameter);
}
}
}
#endregion
}
}
一些名称空间我就不引用了啊……
sqlcon = "server=服务器,database=数据库名,uid = 用户,pwd=密码";
sqlstr1 = "insert ……)";
sqlstr2 = "update ……";
sqlstr3 = "delete ……";
sqlconnection con = new sqlconnection(sqlcon);
sqlcommand com = new (sqlstr1、2、3,con);
con.open();
com.excuteQuery();
con.close();
这样就OK了……
说清楚点,是有什么数据库。不同的数据库差别还是比较大的。
那你是用的什么数据库呢?
SQLServer、Access或者Orcal?
留下邮箱