数据库连接字符串放置在配置文件中,配置文件写法: <?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="ConStr1" connectionString="Initial Catalog='StudentsManagement';Server='127.0.0.1';User ID='sa';Password='sa';Persist Security Info=True" providerName="System.Data.SqlClient"/> </connectionStrings> </configuration> 类的实现方法: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using System.Data; namespace DBHelper { public class DBConnection { /// <summary> /// 数据库交互类 /// Created by Author /// Date 2013-11-8 /// /// 创建数据库连接 /// 返回数据连接对象 /// </summary> /// <param name="ConStr">连接字符串</param> public static SqlConnection DataBaseConnection(string ConStr) { //从配置文件中获取数据库连接字符串 string DBConStr = System.Configuration.ConfigurationManager.AppSettings[ConStr]; SqlConnection DBConect = new SqlConnection(DBConStr); //如果连接状态关闭则打开数据库 if (DBConect.State == ConnectionState.Closed) { DBConect.Open();//打开数据库 } return DBConect; } /// <summary> /// ExecuteNonQuery /// 返回影响记录的行数 /// </summary> /// <param name="ConStr">连接字符串</param> /// <param name="CmdType">Sql类型</param> /// <param name="StrSql">Sql语句</param> /// <param name="Parameter">Sql语句参数</param> /// <returns>返回影响记录的行数</returns> public static int ExecuteNonQuery(string ConStr,CommandType CmdType,string StrSql,SqlParameter[] Parameter){ int result=-1; using(SqlConnection con=DataBaseConnection(ConStr)){//创建数据库连接,执行完后关闭释放 SqlCommand cmd = new SqlCommand(StrSql,con);//第一个参数Sql语句字符串,第二个参数数据库连对象 cmd.CommandType = CmdType; //类型 Sql语句 存储过程 查询表名称 cmd.Parameters.AddRange(Parameter);//Sql语句添加参数 可以是参数组,Add则只能添加一个参数
result = cmd.ExecuteNonQuery();//执行Sql语句返回影响的记录行数,失败则返回-1 } return result; } /// <summary> /// ExecuteScalar /// 返回第一行第一列,或者未实例化的对象(空引用) /// </summary> /// <param name="ConStr"></param> /// <param name="CmdType"></param> /// <param name="StrSql"></param> /// <param name="Parameter"></param> /// <returns>返回第一行第一列,或者未实例化的对象(空引用)</returns> public static object ExecuteScalar(string ConStr, CommandType CmdType, string StrSql, SqlParameter[] Parameter) { object result= null; using (SqlConnection con=DataBaseConnection(ConStr)){ SqlCommand cmd = new SqlCommand(StrSql,con); cmd.CommandType = CmdType; cmd.Parameters.AddRange(Parameter); result = cmd.ExecuteScalar();//执行SQL,如果执行Select返回查询结果的第一行第一列 //如果SQL不是查询Select那么返回未实例化的对象 } return result; } /// <summary> /// ExecuteReader /// 返回一个SqlDateReader对象,通过该对象的Read方法可以实现对查询结果中的多条记录进行逐一访问。 /// 比如while(reader.Read()){"{0} {1} ...",reader["..."],reader["..."],...} /// </summary> /// <param name="ConStr"></param> /// <param name="CmdType"></param> /// <param name="StrSql"></param> /// <param name="Parameter"></param> /// <returns>SqlDateReader reader</returns> public static SqlDataReader ExecuteReader(string ConStr, CommandType CmdType, string StrSql, SqlParameter[] Parameter){ SqlDataReader reader = null; SqlConnection con=DataBaseConnection(ConStr); SqlCommand cmd = new SqlCommand(StrSql,con); cmd.Parameters.AddRange(Parameter); cmd.CommandType = CmdType; reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);//在关闭DataReader的同时,也将与其关联的连接关闭 //读完查询数据集中的数据断开连接 return reader; } /// <summary> /// GetDataTable /// </summary> /// <param name="ConStr"></param> /// <param name="CmdType"></param> /// <param name="StrSql"></param> /// <param name="Parameter"></param> /// <returns>DataTable dt</returns> public static DataTable GetDataTable(string ConStr, CommandType CmdType, string StrSql, SqlParameter[] Parameter){ DataTable dt = new DataTable();//DataSet ds=new DataSet(); using (SqlConnection con=DataBaseConnection(ConStr)){ SqlCommand cmd = new SqlCommand(StrSql,con); cmd.CommandType = CmdType; cmd.Parameters.AddRange(Parameter); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dt);//填充数据表//adapter.Fill(ds);//填充数据集 //逐行输出查询结果 //foreach(DataRow row in ds.Table[0].Rows) //{ // {"{0},{1}...",row[0],row[1]...} //} } return dt; } } }