string connstring = "server=.;database=pubs;uid=sa;pwd=;pooling=true"
SqlConnection cn = new SqlConnection("Data Source=你数据库的地址;DataBase=数据库名称;Uid=数据库用户名;Pwd=数据库密码;");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace AccessC
{
public class AccessC
{
/// <summary>
/// 连接常量
/// </summary>
private OleDbConnection Con;
/// <summary>
/// SQL命令
/// </summary>
private OleDbCommand Com;
/// <summary>
/// 初始化一个连接实例
/// </summary>
/// <param name="FilePath">文件名</param>
public AccessC(string FilePath)
{
Con = new OleDbConnection(@"Provider =Microsoft.Jet.OLEDB.4.0;Data Source =" + @FilePath);
Com = new OleDbCommand();
}
/// <summary>
/// 返回执行所受影响的函数
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public int ExecuteGetLines(string com)
{
try
{
Com.CommandText = com;
Com.Connection = Con;
Con.Open();
int i = 0;
i = Com.ExecuteNonQuery();
return i;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 统计特定条件的数量
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public int ExecteCount(string com)
{
try
{
Com.CommandText = com;
Com.Connection = Con;
Con.Open();
int i = 0;
i = (int)Com.ExecuteScalar();
return i;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 返回结果的第一行第一列
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public object ExecuteGetCol(string com)
{
try
{
Com.CommandText = com;
Com.Connection = Con;
Con.Open();
object o = null;
o = (object)Com.ExecuteScalar();
return o;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 返回一个数据集合DataTable
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public DataTable ExecuteGetTable(string com)
{
try
{
DataTable dt = new DataTable();
Com.CommandText = com;
Com.Connection = Con;
OleDbDataAdapter dad = new OleDbDataAdapter(Com);
dad.Fill(dt);
return dt;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 关闭连接
/// </summary>
public void CloseCon()
{
Com.Dispose();
Con.Close();
}
}
}