每次操作数据库时, 总要写很多的代码。步骤大概如下图:
步骤大概是:
1, 先建立数据库链接;
2, 接着是设置command的commandText命令语句和设置command的connection链接对象;
虽然上面就2步,但是写的代码还是比较多,上面的步骤后面还要对数据库操作,比如添加,删除,查找。那样写的代码就更多了,
看看下面的代码:
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb"); //数据库连接 OleDbCommand command = new OleDbCommand("insert into studentInfo (id,name,gender,sign) VALUES ('"+id.Text+"','"+name.Text+"','"+gender.Text+"',0)"); //命令对象 command.Connection = con; con.Open(); command.ExecuteNonQuery();//执行添加 con.Close(); MessageBox.Show("成功!"); this.Close();这里,笔者是采用accessDataBase数据库操作的,这里 若安装了office的PC, 都有该数据库。
笔者这里分享个非常便捷操作数据库的文件。(请点击“这里”进行获取)
名为: AccessHelper.cs
下面是AccessHelper.cs的源码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.OleDb; namespace WindowsFormsApplication1 { public class AccessHelper { protected static OleDbConnection conn = new OleDbConnection(); protected static OleDbCommand comm = new OleDbCommand(); public AccessHelper() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 打开数据库 /// </summary> private static void openConnection() { if (conn.State == ConnectionState.Closed) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student133.mdb"; comm.Connection = conn; try { conn.Open(); } catch (Exception e) { throw new Exception(e.Message); } } } /// <summary> /// 关闭数据库 /// </summary> private static void closeConnection() { if (conn.State == ConnectionState.Open) { conn.Close(); conn.Dispose(); comm.Dispose(); } } /// <summary> /// 执行sql语句 /// </summary> /// <param name="sqlstr"></param> public static void excuteSql(string sqlstr) { try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; comm.ExecuteNonQuery(); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } } /// <summary> /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。 /// </summary> /// <param name="sqlstr"></param> /// <returns></returns> public static OleDbDataReader dataReader(string sqlstr) { OleDbDataReader dr = null; try { openConnection(); comm.CommandText = sqlstr; comm.CommandType = CommandType.Text; dr = comm.ExecuteReader(CommandBehavior.CloseConnection); } catch { try { dr.Close(); closeConnection(); } catch { } } return dr; } /// <summary> /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭 /// </summary> /// <param name="sqlstr"></param> /// <param name="dr"></param> public static void dataReader(string sqlstr, ref OleDbDataReader dr) { try { openConnection(); comm.CommandText = sqlstr; comm.CommandType = CommandType.Text; dr = comm.ExecuteReader(CommandBehavior.CloseConnection); } catch { try { if (dr != null && !dr.IsClosed) dr.Close(); } catch { } finally { closeConnection(); } } } /// <summary> /// 返回指定sql语句的dataset /// </summary> /// <param name="sqlstr"></param> /// <returns></returns> public static DataSet dataSet(string sqlstr) { DataSet ds = new DataSet(); OleDbDataAdapter da = new OleDbDataAdapter(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(ds); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } return ds; } /// <summary> /// 返回指定sql语句的dataset /// </summary> /// <param name="sqlstr"></param> /// <param name="ds"></param> public static void dataSet(string sqlstr, ref DataSet ds) { OleDbDataAdapter da = new OleDbDataAdapter(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(ds); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } } /// <summary> /// 返回指定sql语句的datatable /// </summary> /// <param name="sqlstr"></param> /// <returns></returns> public static DataTable dataTable(string sqlstr) { DataTable dt = new DataTable(); OleDbDataAdapter da = new OleDbDataAdapter(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(dt); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } return dt; } /// <summary> /// 返回指定sql语句的datatable /// </summary> /// <param name="sqlstr"></param> /// <param name="dt"></param> public static void dataTable(string sqlstr, ref DataTable dt) { OleDbDataAdapter da = new OleDbDataAdapter(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(dt); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } } /// <summary> /// 返回指定sql语句的dataview /// </summary> /// <param name="sqlstr"></param> /// <returns></returns> public static DataView dataView(string sqlstr) { OleDbDataAdapter da = new OleDbDataAdapter(); DataView dv = new DataView(); DataSet ds = new DataSet(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(ds); dv = ds.Tables[0].DefaultView; } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } return dv; } } }AccessHelper.cs 分装了 我们经常对数据库操作的一些函数,有了它, 对数据的操作就简单的多啦。
下面,我们用AccessHelper.cs读取数据库内容并用DataGridview控件显示数据内容。
string sql = "select * from studentInfo"; DataSet ds = AccessHelper.dataSet(sql); dataGridView1.DataSource = ds.Tables[0];就这么简单的几行代码。完成了对数据库的访问。
dataSet :返回一个DataSet 对象。
特别说明:
若操作的数据库不是Access,是SQL Server,请到AccessHelper.cs的源码里面修改。修改内容是:将所有oledb替换成Sql。