📄 databaseoperation.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Hr.component
{
public abstract class DatabaseOperation
{
#region "Fields"
protected static string strConn = ConfigurationSettings.AppSettings["strConnection"];
protected static string strSQL;
#endregion
#region "Properties"
private int m_ID;
private string m_Name;
public int ID
{
get
{
return m_ID;
}
set
{
m_ID = value;
}
}
public string Name
{
get
{
return m_Name;
}
set
{
m_Name = value;
}
}
#endregion
#region "Functions"
public DatabaseOperation()
{
}
protected static int ExecuteNonQuery(string strSQL)
{
SqlConnection myConn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myConn);
try
{
myConn.Open();
myCmd.ExecuteNonQuery();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myConn.Close();
}
}
protected static DataSet ExecuteReturnDS(string strSQL)
{
SqlConnection myConn = new SqlConnection(strConn);
try
{
myConn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSQL,myConn);
DataSet ds = new DataSet("ds");
sda.Fill(ds);
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myConn.Close();
}
}
protected static int ExecuteReturnValue(string strSQL)
{
SqlConnection myConn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myConn);
try
{
myConn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
{
throw new Exception("返回值发生错误!");
}
else
{
return (int)r;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myConn.Close();
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -