Using mydbase to connect to SQL Server

Source: Internet
Author: User
Tags bool commit

To connect to a SQL Server database using the Ado.net custom class MyDBase

Using System;

Using System.Data;

Using System.Data.SqlClient;

public class MyDBase

{

BOOL Ecode=false;

String ES;

SqlConnection cn=new System.Data.SqlClient.SqlConnection ();

DataSet Rs;

Public MyDBase (String mydbservername,string mydatabasename)

{

Ecode = false;

cn. connectionstring= "Workstation id=" +mydbservername+ ";p acket size=4096;integrated security=sspi;data source=" + mydbservername+ ";p ersist security info=false;initial catalog=" +mydatabasename;

Try

{

cn. Open ();

}

catch (Exception e)

{

ES = E.message;

Ecode = true;

}

}

Public MyDBase (String mydbservername, String mydatabasename, String suername, String spasswd)

{

Ecode = false;

String sconn = "Workstation id=" + mydbservername + ";p acket size=4096;user id=" + suername + ";p wd=" + spasswd + ";d ata s Ource= "+ mydbservername +";p ersist security info=false;initial catalog= "+ mydatabasename;

cn. ConnectionString = sconn;

Try

{

cn. Open ();

}

catch (Exception e)

{

ES = E.message;

Ecode = true;

}

}

Public DataSet GetRecordSet (string sqls)

{

SqlCommand sqlcmd= new SqlCommand ();

Sqlcmd.connection = CN;

Sqlcmd.commandtext = Sqls;

Try

{

SqlDataAdapter ADP = new SqlDataAdapter (SQLCMD);

Rs = new DataSet ();

Adp. Fill (Rs);

}

catch (Exception e)

{

ES = E.message;

Ecode = true;

return null;

}

return (Rs);

}

public int Executesqlscalar (string sqls)

{

string S;

SqlCommand sqlcmd= new SqlCommand ();

Sqlcmd.connection = CN;

Sqlcmd.commandtext = Sqls;

Sqlcmd.commandtype = CommandType.Text;

Try

{

s = Sqlcmd.executescalar (). ToString ();

}

catch (Exception e)

{

ES = E.message;

Ecode = true;

return-1;

}

return (int. Parse (s));

}

public string executesqlscalartostring (string sqls)

{

string S;

SqlCommand SQLCMD = new SqlCommand ();

Sqlcmd.connection = CN;

Sqlcmd.commandtext = Sqls;

Sqlcmd.commandtype = CommandType.Text;

Try

{

s = Sqlcmd.executescalar (). ToString ();

}

catch (Exception e)

{

ES = E.message;

Ecode = true;

Return "-1";

}

return s;

}

public string Executesqlwithtrans (string sqls)

{

string S;

SqlTransaction Mytrans;

MYTRANS=CN. BeginTransaction ();

SqlCommand sqlcmd= new SqlCommand ();

Sqlcmd.connection = CN;

Sqlcmd.commandtext = Sqls;

Sqlcmd.commandtype = CommandType.Text;

Sqlcmd.transaction =mytrans;

Sqlcmd.executenonquery ();

sqls= "SELECT @ @IDENTITY as ID";

Sqlcmd.commandtext =sqls;

Try

{

s = Sqlcmd.executescalar (). ToString ();

}

catch (Exception e)

{

ES = E.message;

Ecode = true;

Mytrans.commit ();

Return "";

}

Mytrans.commit ();

return (s);

}

public void ExecuteSQL (string sqls)

{

SqlCommand sqlcmd= new SqlCommand ();

Sqlcmd.connection = CN;

Sqlcmd.commandtext = Sqls;

Sqlcmd.commandtype = CommandType.Text;

Try

{

Sqlcmd.executenonquery ();

}

catch (Exception e)

{

ES = E.message;

Ecode = true;

}

}

Public SqlDataReader DbDataReader (string sqls)

{

SqlCommand sqlcmd= new SqlCommand ();

Sqlcmd.connection = CN;

Sqlcmd.commandtext = Sqls;

Sqlcmd.commandtype = CommandType.Text;

Try

{

Return Sqlcmd.executereader (commandbehavior.closeconnection);

}

catch (Exception e)

{

ES = E.message;

Ecode = true;

return null;

}

}

public void Dbclose ()

{

Try

{

cn. Close ();

}

catch (Exception e)

{

ES = E.message;

Ecode = true;

}

}

public bool ErrorCode ()

{

return ecode;

}

public string Errmessage ()

{

return ES;

}

~mydbase ()

{

if (CN. State==connectionstate.open) cn. Close ();

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.