- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- namespace HelloWinForm.DBUtility
- {
- class SQLHelper
- {
- #region 通用方法
-
- private SqlConnection con;
-
-
-
-
- public static String GetSqlConnection()
- {
- String conn = ConfigurationManager.AppSettings["connectionString"].ToString();
- return conn;
- }
- #endregion
- #region 执行sql字符串
-
-
-
-
-
- public static int ExecuteSql(String Sqlstr)
- {
- String ConnStr = GetSqlConnection();
- using (SqlConnection conn = new SqlConnection(ConnStr))
- {
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- cmd.CommandText = Sqlstr;
- conn.Open();
- cmd.ExecuteNonQuery();
- conn.Close();
- return 1;
- }
- }
-
-
-
-
-
-
- public static int ExecuteSql(String Sqlstr, SqlParameter[] param)
- {
- String ConnStr = GetSqlConnection();
- using (SqlConnection conn = new SqlConnection(ConnStr))
- {
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- cmd.CommandText = Sqlstr;
- cmd.Parameters.AddRange(param);
- conn.Open();
- cmd.ExecuteNonQuery();
- conn.Close();
- return 1;
- }
- }
-
-
-
-
-
- public static SqlDataReader ExecuteReader(String Sqlstr)
- {
- String ConnStr = GetSqlConnection();
- SqlConnection conn = new SqlConnection(ConnStr);
- try
- {
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- cmd.CommandText = Sqlstr;
- conn.Open();
- return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
- }
- catch
- {
- return null;
- }
- }
-
-
-
-
-
- public static DataTable ExecuteDt(String Sqlstr)
- {
- String ConnStr = GetSqlConnection();
- using (SqlConnection conn = new SqlConnection(ConnStr))
- {
- SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
- DataTable dt = new DataTable();
- conn.Open();
- da.Fill(dt);
- conn.Close();
- return dt;
- }
- }
-
-
-
-
-
- public static DataSet ExecuteDs(String Sqlstr)
- {
- String ConnStr = GetSqlConnection();
- using (SqlConnection conn = new SqlConnection(ConnStr))
- {
- SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
- DataSet ds = new DataSet();
- conn.Open();
- da.Fill(ds);
- conn.Close();
- return ds;
- }
- }
- #endregion
- #region 操作存储过程
-
-
-
-
-
- public int RunProc(string procName)
- {
- SqlCommand cmd = CreateCommand(procName, null);
- cmd.ExecuteNonQuery();
- this.Close();
- return (int)cmd.Parameters["ReturnValue"].Value;
- }
-
-
-
-
-
-
- public int RunProc(string procName, SqlParameter[] prams)
- {
- SqlCommand cmd = CreateCommand(procName, prams);
- cmd.ExecuteNonQuery();
- this.Close();
- return (int)cmd.Parameters[0].Value;
- }
-
-
-
-
-
- public void RunProc(string procName, out SqlDataReader dataReader)
- {
- SqlCommand cmd = CreateCommand(procName, null);
- dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
- }
-
-
-
-
-
-
- public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
- {
- SqlCommand cmd = CreateCommand(procName, prams);
- dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
- }
-
-
-
-
-
-
- private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
- {
-
- Open();
-
- SqlCommand cmd = new SqlCommand(procName, con);
- cmd.CommandType = CommandType.StoredProcedure;
-
- if (prams != null)
- {
- foreach (SqlParameter parameter in prams)
- cmd.Parameters.Add(parameter);
- }
-
- return cmd;
- }
-
-
-
-
-
-
-
-
- public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
- {
- return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
- }
-
-
-
-
-
-
-
- public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
- {
- return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
- }
-
-
-
-
-
-
-
-
-
- public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
- {
- SqlParameter param;
- if (Size > 0)
- {
- param = new SqlParameter(ParamName, DbType, Size);
- }
- else
- {
- param = new SqlParameter(ParamName, DbType);
- }
- param.Direction = Direction;
- if (!(Direction == ParameterDirection.Output && Value == null))
- {
- param.Value = Value;
- }
- return param;
- }
- #endregion
- #region 数据库连接和关闭
-
-
-
- private void Open()
- {
-
- if (con == null)
- {
-
- con = new SqlConnection(GetSqlConnection());
- con.Open();
- }
- }
-
-
-
- public void Close()
- {
- if (con != null)
- con.Close();
- }
-
-
-
- public void Dispose()
- {
-
- if (con != null)
- {
- con.Dispose();
- con = null;
- }
- }
- #endregion
- }
- }
简单用一下:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- using System.Collections;
- using HelloWinForm.DBUtility;
- namespace HelloWinForm.DAL
- {
- class Student
- {
- public string test()
- {
- string str = "";
- SqlDataReader dr = SQLHelper.ExecuteReader("select * from Student");
- while (dr.Read())
- {
- str += dr["StudentNO"].ToString();
- }
- dr.Close();
- return str;
- }
- }
- }