C#数据库类的使用方法
来源:网络整理 责任编辑:栏目编辑 发表时间:2013-07-01 22:46 点击:次
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;
}
}
}
}
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;
}
}
}
}
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>