麻烦下。这个是我操作SQL时用的。现在数据库改为ACCESS。这两个类不知道怎么改。麻烦下帮我改下。小弟不是专业程序员,只是爱好而已。在此谢过
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;namespace Farming.DbOperate
{
public class SqlValidate
{ #region 变量
protected SqlConnection conn;
#endregion
public SqlValidate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected void CreateCon()
{
try
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["strConnection"];
conn = new SqlConnection(strconn);
}
catch
{
throw new Exception("数据库创建出错");
}
} protected void CloseCon()
{
try
{
conn.Close();
}
catch
{
throw new Exception("数据库关闭出错");
}
}
}
}using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace Farming.DbOperate.Operation
{
public class T_db_operation : SqlValidate
{
public T_db_operation()
{
//默认
CreateCon();
} /// <summary>
/// 执行SQL,返回影响数据库记录的条数(插入、更新)
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public int ExcuteNonQuery(string strSql)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
int count = command.ExecuteNonQuery();
this.conn.Close();
return count;
}
catch(Exception ex)
{
if (conn.State != ConnectionState.Closed)
{
this.conn.Close();
}
throw ex;
}
}
/// <summary>
/// 执行SQL,返回object(登陆验证)
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public object ExecuteScalar(string strSql)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
object obj = command.ExecuteScalar();
this.conn.Close();
return obj;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 执行SQL,返回SqlDataReader
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public SqlDataReader ExecuteReader(string strSql)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
SqlDataReader reader = command.ExecuteReader();
this.conn.Close();
return reader;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 执行SQL,返回DataTable
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public DataTable ExecuteDataTable(string strSql)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable dt = new DataTable();
adapter.Fill(dt);
this.conn.Close();
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 执行SQL语句,返回Dataset
/// </summary>
/// <param name="strSql"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public DataSet ExecuteDataSet(string strSql,string tableName)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataSet ds = new DataSet();
adapter.Fill(ds,tableName);
this.conn.Close();
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;namespace Farming.DbOperate
{
public class SqlValidate
{ #region 变量
protected SqlConnection conn;
#endregion
public SqlValidate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected void CreateCon()
{
try
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["strConnection"];
conn = new SqlConnection(strconn);
}
catch
{
throw new Exception("数据库创建出错");
}
} protected void CloseCon()
{
try
{
conn.Close();
}
catch
{
throw new Exception("数据库关闭出错");
}
}
}
}using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace Farming.DbOperate.Operation
{
public class T_db_operation : SqlValidate
{
public T_db_operation()
{
//默认
CreateCon();
} /// <summary>
/// 执行SQL,返回影响数据库记录的条数(插入、更新)
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public int ExcuteNonQuery(string strSql)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
int count = command.ExecuteNonQuery();
this.conn.Close();
return count;
}
catch(Exception ex)
{
if (conn.State != ConnectionState.Closed)
{
this.conn.Close();
}
throw ex;
}
}
/// <summary>
/// 执行SQL,返回object(登陆验证)
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public object ExecuteScalar(string strSql)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
object obj = command.ExecuteScalar();
this.conn.Close();
return obj;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 执行SQL,返回SqlDataReader
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public SqlDataReader ExecuteReader(string strSql)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
SqlDataReader reader = command.ExecuteReader();
this.conn.Close();
return reader;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 执行SQL,返回DataTable
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public DataTable ExecuteDataTable(string strSql)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable dt = new DataTable();
adapter.Fill(dt);
this.conn.Close();
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 执行SQL语句,返回Dataset
/// </summary>
/// <param name="strSql"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public DataSet ExecuteDataSet(string strSql,string tableName)
{
try
{
this.conn.Open();
SqlCommand command = new SqlCommand(strSql);
command.CommandType = CommandType.Text;
command.Connection = this.conn;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataSet ds = new DataSet();
adapter.Fill(ds,tableName);
this.conn.Close();
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
OleDbCommand
看看sqlhelper
1 config配置文件里的DB连接
2 用OleDB通配替换Sql
然后查错。嘿嘿
当数据库更改时,只须改配置文件就样了,如果你觉得麻烦,就像楼上
说的