数据库连接进行增、删、查、改和聚会函数的DBHepler类分享
public static string newsrt = "";
public static SqlConnection coon = null;
public static bool Connection()
{
coon = new SqlConnection(newsrt);
coon.Open();
if (coon.State==ConnectionState.Open)
{
return true;
}
else
{
return false;
}
}
public static SqlDataReader GetDataReader(string sql)
{
Connection();
SqlCommand com = new SqlCommand(sql, coon);
SqlDataReader reader = com.ExecuteReader();
return reader;
}
public static int ExecuteNonQuery(string sql)
{
Connection();
SqlCommand com = new SqlCommand(sql, coon);
int result = com.ExecuteNonQuery();
return result;
}
public static object ExecuteScalar(string sql)
{
Connection();
SqlCommand com = new SqlCommand(sql, coon);
object result = com.ExecuteScalar();
return result;
}
public static void Connshut()
{ coon.Close();
}
字段名 | 类型 | 是否公开 | 说明 |
---|
ConnString | String | 是( Public ) | 数据库连接 |
Conn | SqlConnection | 否( Private ) | 数据库连接对象 |
InitConnection | 无 | 无(void) | 否( Private ) |
GetDataReader | 查询的SQL字符串 | SqlDataReader | 是( Public ) |
GetDataTable | 查询的SQL字符串 | DataTable | 是( Public ) |
ExecuteNonQuery | 增删改的SQL字符串 | 执行是否成功 | 是( Public ) |
ExecuteScalar | 聚合函数SQL字符串 | 结果( Object ) | 是( Public |