程序人生

致力于MicroSoft Borland Sybase Oracle技术的研究

导航

【转贴】 C#数据库类的使用方法

C#连接数据库虽然可以使用控件绑定数据集的方法,但是这样的方法不是很灵活,我想用类实现,下面是网上照到的:(VS2005下调试成功)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using System.IO;

namespace ConnectionDb
{

public partial class Form1 : Form
{
public SqlConnection con;
string strCon = "server=Eagle;database=hisbook";
string ErrLogPath = "错误存放的路径";
//public SqlConnection getConnection(string strCon)
//{
// SqlConnection con = new SqlConnection(strCon);
// con.Open();
// return con;
// //SqlCommand com = new SqlCommand("select * from 药品分类",con);
// //SqlDataReader dr =com.ExecuteReader();
// //this.dataGrid1.DataSource =dr;
// ////this.dataGrid1.();
// ////Console.WriteLine(dr);

//}
public SqlConnection getConnection(string strCon)
{
try
{
SqlConnection con 
= new SqlConnection(strCon);
con.Open();
return con;
}
catch (Exception ee)
{
MessageBox.Show(
"连接错误");

return null;
}
}
public int ExecSql(string sql)
{
try
{
con 
= this.getConnection(this.strCon);
SqlCommand cmd 
= con.CreateCommand();
cmd.CommandText 
= sql;
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
catch (Exception ee)
{
MessageBox.Show(
"错误3");
return 0;
}
finally
{
}
}

public Form1()
{
InitializeComponent();
}
public DataSet GetDataSet(string sql)
{
try
{
SqlDataAdapter SDA 
= new SqlDataAdapter(sql, this.strCon);
DataSet ds 
= new DataSet();
SDA.Fill(ds);
return ds;
}
catch
{
MessageBox.Show(
"处理出错");
return null;
}
}
}
} 


/// Db_Class 的摘要说明。

/// </summary>

public class Db_Class

{

public OleDbConnection Conn;

//构造函数

public Db_Class()

{

Conn
= new OleDbConnection("Provider=SQLOLEDB;Server=(local);Pwd=123456;UID=sa;Database=test");

}

//打开数据源链接

public OleDbConnection Db_Conn()

{

Conn.Open();

return Conn;

}

//返回DataReader数据集,下面的SQL可以动态生成

public OleDbDataReader Db_CreateReader(string SQL)

{

Db_Conn();

OleDbCommand cmd 
= new OleDbCommand(SQL,Conn);

OleDbDataReader Rs 
= cmd.ExecuteReader();

return Rs;

this.close();

}

//返回DataReader数据集,下面的SQL是存储过程

public OleDbDataReader Db_CommandReader(string SQL)

{

Db_Conn();

OleDbCommand cmd 
= new OleDbCommand(SQL,Conn);

cmd.CommandType 
= CommandType.StoredProcedure;

OleDbDataReader Rs 
= cmd.ExecuteReader();

return Rs;

this.close();

}

//返回数据DataSet数据集

public OleDbDataSet Db_CreateDataSet(string SQL)

{

Db_Conn();

OleDbCommand cmd 
= new OleDbCommand(SQL,Conn);

OleDbDataAdapter Adpt
= new OleDbDataAdapter(cmd,Conn);

DataSet Ds 
= new DataSet();

Adpt.Fill(Ds,
"NewTable");

return Ds;

this.close();

}

//返回数据DataReader数据集,不需要返回数据的修改,删除可以使用本函数

public bool Db_ExecuteNonquery(string SQL)

{

Db_Conn();

OleDbCommand cmd 
= new OleDbCommand(SQL,Conn);

try

{

cmd.ExecuteNonQuery();

return true;

}

catch

{

return false;

}

this.close();

}

//关闭数据链接

public void close()

{

Conn.Close();

}

}

}

使用方法如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

namespace DbClass

{

/// <summary>

/// WebForm1 的摘要说明。 
 
/// </summary>

public class WebForm1 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.DataGrid DataGrid1;

protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

//string SQL="select * from sysfiles";

Db_Class Db_class 
= new Db_Class();

DataGrid1.DataSource
=Db_class.Db_CommandReader("sp_tables");//使用SQLSERVER的存储过程。

DataGrid1.DataBind();

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是 asp.net Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.Button1.Click += new System.EventHandler(this.Button1_Click);

this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}

}

posted on 2007-05-07 16:35  yoyo918  阅读(1794)  评论(0)    收藏  举报