c\#SQLHelper(for winForm)구현 코드

9540 단어 c#SQLHelperwinForm
SQLHelper.cs

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;
/// <summary>
///
/// </summary>
/// <returns></returns>
public static String GetSqlConnection()
{
String conn = ConfigurationManager.AppSettings["connectionString"].ToString();
return conn;
}
#endregion
#region sql
/// <summary>
/// SQL
/// </summary>
/// <param name="Sqlstr"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// SQL
/// </summary>
/// <param name="Sqlstr">SQL </param>
/// <param name="param"> </param>
/// <returns></returns>
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;
}
}
/// <summary>
/// DataReader
/// </summary>
/// <param name="Sqlstr"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(String Sqlstr)
{
String ConnStr = GetSqlConnection();
SqlConnection conn = new SqlConnection(ConnStr);// DataReader , using()
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);// Connection
}
catch //(Exception ex)
{
return null;
}
}
/// <summary>
/// SQL
/// </summary>
/// <param name="Sqlstr">SQL </param>
/// <returns></returns>
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;
}
}
/// <summary>
/// SQL DataSet
/// </summary>
/// <param name="Sqlstr">SQL </param>
/// <returns></returns>
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
/// <summary>
/// ( )
/// </summary>
/// <param name="procName"> </param>
/// <returns> </returns>
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// ( )
/// </summary>
/// <param name="procName"> </param>
/// <param name="prams"> </param>
/// <returns> </returns>
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters[0].Value;
}
/// <summary>
/// ( )
/// </summary>
/// <param name="procName"> </param>
/// <param name="dataReader"> </param>
public void RunProc(string procName, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, null);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// <summary>
/// ( )
/// </summary>
/// <param name="procName"> </param>
/// <param name="prams"> </param>
/// <param name="dataReader"> </param>
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// <summary>
/// Command
/// </summary>
/// <param name="procName"> </param>
/// <param name="prams"> </param>
/// <returns>Command </returns>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
//
Open();
//command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) );
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
//
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// Command
return cmd;
}
/// <summary>
///
/// </summary>
/// <param name="ParamName"> </param>
/// <param name="DbType"> </param>
/// <param name="Size"> </param>
/// <param name="Value"> </param>
/// <returns> </returns>
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
///
/// </summary>
/// <param name="ParamName"> </param>
/// <param name="DbType"> </param>
/// <param name="Size"> </param>
/// <returns> </returns>
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// <summary>
///
/// </summary>
/// <param name="ParamName"> </param>
/// <param name="DbType"> </param>
/// <param name="Size"> </param>
/// <param name="Direction"> ( / )</param>
/// <param name="Value"> </param>
/// <returns> </returns>
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
/// <summary>
///
/// </summary>
private void Open()
{
//
if (con == null)
{
// using System.Configuration;
con = new SqlConnection(GetSqlConnection());
con.Open();
}
}
/// <summary>
///
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}
/// <summary>
///
/// </summary>
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;
}
}
}

좋은 웹페이지 즐겨찾기