asp.net| Data | database
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>
Summary description of Sqlserverdatabase
</summary>
public class Sqlserverdatabase
{
private string strerror = null;
private int intcount = 0;
Public Sqlserverdatabase ()
{
//
TODO: Add constructor logic here
//
}
<summary>
Exposing method Dbconn, returning a database connection
</summary>
<returns></returns>
Public SqlConnection Dbconn ()
{
String strconn = "server= (local);D atabase=globalmeetings; Uid=sa;pwd= ";
Try
{
return new SqlConnection (strconn);
}
catch (Exception)
{
return null;
}
}
<summary>
Exposing property errormessage, returning an error message
</summary>
public string errormessage
{
Get
{
return strerror;
}
}
<summary>
Retrieving data from a database based on a query statement
</summary>
<param name= "Strselect" > Query Statements </param>
<param name= "sqlconn" > Database connection </param>
<returns> returns the DataSet object if it has data, otherwise returns null</returns>
Public DataSet Select (string selectstring, SqlConnection sqlconn)
{
Strerror = "";
SqlConnection Conn;
if (sqlconn = null)
{
conn = Dbconn ();
}
Else
{
conn = sqlconn;
}
Try
{
Open the connection if the current state of the database connection is closed
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 = "Data retrieval failed:" + e.message;
return null;
}
Finally
{
IF (Conn. State!= connectionstate.closed)
{
Conn. Close ();
}
}
}
<summary>
Update Database
</summary>
<param name= "updatestring" >update SQL statement </param>
<param name= "sqlconn" > Database connection </param>
<returns> update successfully returned to True</returns>
public bool Update (string updatestring, SqlConnection sqlconn)
{
Return Udidatabase (updatestring, sqlconn);
}
<summary>
Delete data from the database
</summary>
<param name= "deletestring" >delete SQL statement </param>
<param name= "sqlconn" > Database connection </param>
<returns> Delete successful return true</returns>
public bool Delete (string deletestring, SqlConnection sqlconn)
{
Return Udidatabase (deletestring, sqlconn);
}
<summary>
Inserting data into the database
</summary>
<param name= "insertstring" >insert SQL statement </param>
<param name= "sqlconn" > Database connection </param>
<returns> Insert Successful return true</returns>
public bool Insert (string insertstring, SqlConnection sqlconn)
{
Return Udidatabase (insertstring, sqlconn);
}
<summary>
Update database based on SQL statement
</summary>
<param name= "udistring" > UPDATE statement </param>
<param name= "sqlconn" > Database connection </param>
<returns> return to true</returns> if successful update
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 = "Update database failed:" + e.message;
return false;
}
Finally
{
IF (Conn. State!= connectionstate.closed)
{
Conn. Close ();
}
}
}
}
}
-----------------------------
Two methods of calling
1, String struserpsw = UserPsw.Text.Trim ();
String userpassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (STRUSERPSW, "MD5" );//MD5 Encryption
Sqlserverdatabase obj = new Sqlserverdatabase ();
Obj. Insert ("INSERT into Asuserinfo (username,userpassword,question,answer,createtime) VALUES (' + UserName.Text.Trim () +" ', ' "+ UserPassword +" ', ' "+ Question.Text.Trim () +" ', ' "+ Answer.Text.Trim () +" ', ' "+ DateTime.Now.ToString () +" ') ", NULL);
2. Private bool Isusernameexist (string strusername)
{
bool BRet = true;
Sqlserverdatabase db = new Sqlserverdatabase ();
DataSet ds = db. Select ("SELECT * from asuserinfo where UserName = '" + strUserName + "", null);
if (ds = null | | ds. Tables.count = 0 | | Ds. Tables[0]. Rows.Count = 0)
{
BRet = false;
}
Else
{
BRet = true;
}
return bRet;
}