Asp.Net(c#)データベース操作クラス
5475 ワード
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Mysqlserver
{
/// <summary>
/// SqlServerDataBase
/// </summary>
public class SqlServerDataBase
{
private string strError = null;
private int intCount = 0;
public SqlServerDataBase()
{
//
// TODO:
//
}
/// <summary>
/// DBConn,
/// </summary>
/// <returns></returns>
public SqlConnection DBconn()
{
string strConn = "Server=(local);Database=GlobalMeetings;Uid=sa;pwd=";
try
{
return new SqlConnection(strConn);
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// ErrorMessage,
/// </summary>
public string ErrorMessage
{
get
{
return strError;
}
}
/// <summary>
///
/// </summary>
/// <param name="strSelect"> </param>
/// <param name="SqlConn"> </param>
/// <returns> DataSet , null</returns>
public DataSet Select(string SelectString, SqlConnection sqlConn)
{
strError = "";
SqlConnection conn;
if (sqlConn == null)
{
conn = DBconn();
}
else
{
conn = sqlConn;
}
try
{
// ,
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
SqlCommand selectCommand = new SqlCommand(SelectString, conn);
selectCommand.CommandType = CommandType.Text;
mySqlDataAdapter.SelectCommand = selectCommand;
DataSet myDS = new DataSet();
mySqlDataAdapter.Fill(myDS);
return myDS;
}
catch (Exception e)
{
strError = " :" + e.Message;
return null;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="UpdateString">Update Sql </param>
/// <param name="SqlConn"> </param>
/// <returns> true</returns>
public bool Update(string UpdateString, SqlConnection SqlConn)
{
return udiDataBase(UpdateString, SqlConn);
}
/// <summary>
///
/// </summary>
/// <param name="DeleteString">Delete Sql </param>
/// <param name="SqlConn"> </param>
/// <returns> true</returns>
public bool Delete(string DeleteString, SqlConnection SqlConn)
{
return udiDataBase(DeleteString, SqlConn);
}
/// <summary>
///
/// </summary>
/// <param name="InsertString">Insert Sql </param>
/// <param name="SqlConn"> </param>
/// <returns> true</returns>
public bool Insert(string InsertString, SqlConnection SqlConn)
{
return udiDataBase(InsertString, SqlConn);
}
/// <summary>
/// Sql
/// </summary>
/// <param name="UDIString"> </param>
/// <param name="SqlConn"> </param>
/// <returns> true</returns>
public bool udiDataBase(string UDIString, SqlConnection SqlConn)
{
strError = "";
SqlConnection conn;
if (SqlConn == null)
{
conn = DBconn();
}
else
{
conn = SqlConn;
}
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand(UDIString, conn);
cmd.CommandType = CommandType.Text;
intCount = cmd.ExecuteNonQuery();
return !(intCount < 1);
}
catch (Exception e)
{
strError = " :" + e.Message;
return false;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
}
}