Asp.net(c#)数据库操作类
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();
}
}
}
}
}