ASP源码
PHP源码
.NET源码
JSP源码
using System;
using Sy***m.Data;
using Sy***m.Configuration;
using Sy***m.Web;
using Sy***m.Web.Security;
using Sy***m.Web.UI;
using Sy***m.Web.UI.WebControls;
using Sy***m.Web.UI.WebControls.WebParts;
using Sy***m.Web.UI.HtmlControls;
using Sy***m.Data.SqlClient;
namespace Mysqlserver
{
///
/// SqlServerDataBase 的摘要说明
///
public class SqlServerDataBase
{
private string strError = null;
private int intCount = 0;
public SqlServerDataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///
/// 公开方法DBConn,返回数据库连接
///
///
public SqlConnection DBconn()
{
string strConn = "Server=(local);Database=GlobalMeetings;Uid=sa;pwd=";
try
{
return new SqlConnection(strConn);
}
catch (Exception)
{
return null;
}
}
///
/// 公开属性ErrorMessage,返回错误信息
///
public string ErrorMessage
{
get
{
return strError;
}
}
///
/// 根据查询语句从数据库检索数据
///
/// 查询语句
/// 数据库连接
///
public DataSet Select(string SelectString, SqlConnection sqlConn)
{
strError = "";
SqlConnection conn;
if (sqlConn == null)
{
conn = DBconn();
}
else
{
conn = sqlConn;
}
try
{
//若数据库连接的当前状态是关闭的,则打开连接
if (co***State == Co***ctionState.Closed)
{
co***Open();
}
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
SqlCommand selectCommand = new SqlCommand(SelectString, conn);
se***tCommand.CommandType = Co***ndType.Text;
my***DataAdapter.SelectCommand = selectCommand;
DataSet myDS = new DataSet();
my***DataAdapter.Fill(myDS);
return myDS;
}
catch (Exception e)
{
strError = "数据检索失败:" + e.***sage;
return null;
}
finally
{
if (co***State != Co***ctionState.Closed)
{
co***Close();
}
}
}
///
/// 更新数据库
///
/// Update Sql语句
/// 数据库连接
///
public bool Update(string UpdateString, SqlConnection SqlConn)
{
return udiDataBase(UpdateString, SqlConn);
}
///
/// 从数据库中删除数据
///
/// Delete Sql语句
/// 数据库连接
///
public bool Delete(string DeleteString, SqlConnection SqlConn)
{
return udiDataBase(DeleteString, SqlConn);
}
///
/// 把数据插入数据库
///
/// Insert Sql语句
/// 数据库连接
///
public bool Insert(string InsertString, SqlConnection SqlConn)
{
return udiDataBase(InsertString, SqlConn);
}
///
/// 根据Sql语句更新数据库
///
/// 更新语句
/// 数据库连接
///
public bool udiDataBase(string UDIString, SqlConnection SqlConn)
{
strError = "";
SqlConnection conn;
if (SqlConn == null)
{
conn = DBconn();
}
else
{
conn = SqlConn;
}
try
{
if (co***State == Co***ctionState.Closed)
{
co***Open();
}
SqlCommand cmd = new SqlCommand(UDIString, conn);
cm***ommandType = Co***ndType.Text;
intCount = cm***xecuteNonQuery();
return !(intCount < 1);
}
catch (Exception e)
{
strError = "更新数据库失败:" + e.***sage;
return false;
}
finally
{
if (co***State != Co***ctionState.Closed)
{
co***Close();
}
}
}
}
}
-----------------------------
两种调用方法
1、 string strUserPsw = Us***sw.Text.Trim();
string UserPassword = Sy***m.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserPsw, "MD5");//md5加密
SqlServerDataBase obj = new SqlServerDataBase();
ob***nsert("insert into asUserInfo (UserName,UserPassword,Question,Answer,CreateTime) values('" + Us***ame.Text.Trim() + "','" + UserPassword + "','" + Qu***ion.Text.Trim() + "','" + An***r.Text.Trim() + "','" + Da***ime.Now.ToString() + "' )", null);
2、 private bool IsUsernameExist(string strUsername)
{
bool bRet = true;
SqlServerDataBase db = new SqlServerDataBase();
DataSet ds = db***lect("select * from asUserInfo where UserName = '" + strUsername + "'", null);
if (ds == null || ds***bles.Count == 0 || ds***bles[0].R***.Count == 0)
{
bRet = false;
}
else
{
bRet = true;
}
return bRet;
}
http://blog.csdn.net/zdyguilong/archive/2007/01/22/1490250.aspx