.net连接数据库的基本类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.OleDb;//引入oledb

using System.Data;

using System.Collections;

using System.Windows.Forms;

using System.IO;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Text;



/// <summary>

/// 描述:数据访问类,业务处理类

/// 功能:提供数据访问,业务处理的方法

/// 作者:R3-徐镇

/// 时间:2010-9-20 14:47:28
///using System.Data.SqlClient; SqlServer 命名空间
///using CoreLab.MySql;mySql 程序集
///using System.Data.Odbc; 引入odbc连接
///using MySql.Data.MySqlClient;Mysql命名空间 /// </summary> public class SaleInfoManager { HttpResponse Response = null; public SaleInfoManager(HttpResponse response) { Response = response; // // TODO: Add constructor logic here // } //数据库连接 如果您使用的是其他类型的数据库,就把OleDb 这个前缀改为您所要连接的 数据库引擎 就可以了! 例如SQLConnection,SQLCommand,SQLDataAdapter;MySqlConnection.等等…… private static OleDbConnection con; public static OleDbConnection Con { get { if (con == null) { con = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mdb;"+ "Persist Security Info=True"); } else if (con.State == ConnectionState.Broken) { con.Close(); con.Open(); } else if (con.State == ConnectionState.Closed ) { con.Open(); } return con; } } #region GetBySQL 查询-返回datatable public static DataTable GetBySQL(string sql) { OleDbCommand cmd = new OleDbCommand(sql, Con);//执行命令 OleDbDataAdapter da = new OleDbDataAdapter(cmd);//装载结果 DataTable dt = new DataTable();//创建datata da.Fill(dt);//填充datatable return dt;//返回结果 } #endregion #region ExcuteCmd 执行修改,删除,添加 public static int ExcuteCmd(string sql) { OleDbCommand cmd = new OleDbCommand(sql, Con);//执行命令 int rs = cmd.ExecuteNonQuery();//返回执行结果 return rs; } #endregion

你可能感兴趣的:(.net)