C # Common classes for underlying data operations

Source: Internet
Author: User
Tags config tostring
Data using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Windows.Forms;

Namespace Datalibrary
{
<summary>
Data connection Settings
</summary>
public class SqlConnect
{
static string Sqlconnstr;

<summary>
Connection string
</summary>
public string connstring
{
Set
{
Sqlconnstr=value;
}
Get
{
return sqlconnstr;
}
}

<summary>
Get a connection
</summary>
<returns></returns>
Public SqlConnection getsqlconnection ()
{
SqlConnection _connection=null;
if (Sqlconnstr==null | | sqlconnstr== "")
{
String systemdir=system.environment.systemdirectory;
Disposalconductservice disposalconductservice1= New Disposalconductservice (systemdir+ "\\CBERP_Dis.DLL");
Sqlconnstr = "server=" +disposalconductservice1.getvalue ("ServerName", "(local)")
+ "; user id=" +disposalconductservice1.getvalue ("Sqlaccount", "sa")
+ ";p assword=" +disposalconductservice1.getvalue ("password", "jiayu520")
+ ";D atabase=" +disposalconductservice1.getvalue ("Database", "Cberp");
}
Try
{
_connection = new SqlConnection (SQLCONNSTR);
_connection. Open ();
}
catch (SqlException SE)
{
if (SE. number==17 | | SE. number==18456)
{
if (MessageBox.Show ("database access parameter is set incorrectly, do you want to set it?", "hint", messageboxbuttons.yesno,messageboxicon.question) = = Dialogresult.yes)
{
Sqlconnstr= "";
disposalconduct config = new disposalconduct ();
Config. ShowDialog ();
_connection=getsqlconnection ();
}
Else
{
MessageBox.Show ("Failed to establish database connection, program cannot continue running!", "error", messageboxbuttons.ok,messageboxicon.stop);
Application.exitthread ();
Application.exit ();
}
}
Else
{
MessageBox.Show (SE. message, "Hint information", messageboxbuttons.ok,messageboxicon.information);
_connection. Close ();
}
}
return _connection;
}
}

<summary>
Data configuration
</summary>
public class Datacatena:idisposable
{
Private SqlConnection _connection;
<summary>
Data configuration
</summary>
Public Datacatena ()
{
SqlConnect SqlConnect = new SqlConnect ();
_connection = SQLConnect. Getsqlconnection ();
_connection. Close ();
}

<summary>
Data configuration
</summary>
<param name= "ConnStr" > Connection string </param>
Public Datacatena (String connstr)
{
_connection = new SqlConnection (CONNSTR);
}

<summary>
Get DataView
</summary>
<param name= "Sqlstr" > Connection string </param>
<returns> Data View </returns>
Public DataView Getdataview (String sqlstr)
{
DataView Dv=null;
SqlDataAdapter mycommand = new SqlDataAdapter (Sqlstr, _connection);
DataSet ds = new DataSet ();
Try
{
Mycommand.fill (ds, "table");
Dv=ds. tables["Table"]. DefaultView;
}
catch (SqlException exc)
{
Debugbox db = new Debugbox (sqlstr + "\ r \ n" + exc.) message);
Db. Show ();
}
return DV;
}

<summary>
Clean up resources
</summary>
public void Dispose ()
{
Dispose (TRUE);
Gc. SuppressFinalize (TRUE);
}

<summary>
Clean up all resources that are in use.
</summary>
<param name= "disposing" ></param>
protected virtual void Dispose (bool disposing)
{
if (! disposing)
Return

if (this._connection!= null)
{
This._connection. Dispose ();
}
}

<summary>
Get Data adapter
</summary>
<param name= "sqlstr" >sql statement </param>
<returns> Data Adapters </returns>
Public SqlDataAdapter Getsqlda (string sqlstr)
{
SqlDataAdapter mycommand = new SqlDataAdapter (Sqlstr, _connection);
return mycommand;
}

<summary>
Get data table
</summary>
<param name= "sqlstr" >sql statement </param>
<returns> Data Sheet </returns>
Public DataTable Getdt (String sqlstr)
{
SqlDataAdapter mycommand = new SqlDataAdapter (Sqlstr, _connection);
DataSet ds = new DataSet ();
Try
{
Mycommand.fill (ds, "table");
}
catch (SqlException exc)
{
Debugbox db = new Debugbox (sqlstr + "\ r \ n" + exc.) ToString ());
Db. Show ();
}
Return DS. tables["Table"];
}

<summary>
EXECUTE statement
</summary>
<param name= "sqlstr" >sql statement </param>
<returns></returns>
public Object Execwithvalue (String sqlstr)
{
Object Result=null;
_connection. Open ();
SqlCommand mycommand = new SqlCommand (sqlstr, _connection);
Try
{
Result=mycommand.executescalar ();
}
catch (SqlException exc)
{
Debugbox db = new Debugbox (sqlstr + "\ r \ n" + exc.) ToString ());
Db. Show ();
MessageBox.Show (exc. message);
}
_connection. Close ();
return result;
}

<summary>
Returns the corresponding number of rows
</summary>
<param name= "sqlstr" >sql statement </param>
<returns> Number of lines </returns>
public int Exec (String sqlstr)
{
int sucess=0;
SqlCommand mycommand = new SqlCommand (sqlstr, _connection);
_connection. Open ();
Try
{
Mycommand.executenonquery ();
}
catch (SqlException exc)
{
Debugbox db = new Debugbox (sqlstr + "\ r \ n" + exc.) ToString ());
Db. Show ();
MessageBox.Show (exc. message);
Sucess=exc. number;
}
_connection. Close ();
return sucess;
}
}

<summary>
Single-table operations
</summary>
public class Singletable
{
Private Datacatena Datacatena;
Private SqlDataAdapter MyDataAdapter;
Private DataSet DS;
<summary>
Single-table operations
</summary>
<param name= "Sqlstr" ></param>
Public singletable (String sqlstr)
{
Datacatena = new Datacatena ();
MyDataAdapter = Datacatena.getsqlda (SQLSTR);
ds = new DataSet ();
Mydataadapter.fill (ds, "Table1");
}

<summary>
Update a table
</summary>
<param name= "Sqlstr" ></param>
public void Setupdatecommand (string sqlstr)
{
Mydataadapter.updatecommand = new SqlCommand (sqlstr,mydataadapter.selectcommand.connection);
}

<summary>
Return data table
</summary>
<returns></returns>
Public DataTable getdatatable ()
{
Return DS. tables["Table1"];
}

<summary>
Return Data adapter
</summary>
<returns></returns>
Public SqlDataAdapter Getdataadapter ()
{
return mydataadapter;
}

<summary>
Update table
</summary>
public void Update ()
{
Mydataadapter.update (ds, "Table1");
}
}

<summary>
Automatic Updates
</summary>
public class Autoupdateobj
{
Private Datacatena Datacatena;
Private SqlDataAdapter MyDataAdapter;
Private SqlCommandBuilder CB;
Private DataSet DS;
<summary>
Automatic Updates
</summary>
<param name= "Sqlstr" ></param>
Public Autoupdateobj (String sqlstr)
{
Datacatena = new Datacatena ();
MyDataAdapter = Datacatena.getsqlda (SQLSTR);
cb = New SqlCommandBuilder (MyDataAdapter);
ds = new DataSet ();
Mydataadapter.fill (ds, "Table1");
}

<summary>
Update SQL statement
</summary>
<param name= "Sqlstr" ></param>
public void Setupdatecommand (string sqlstr)
{
Mydataadapter.updatecommand = new SqlCommand (sqlstr,mydataadapter.selectcommand.connection);
}

<summary>
Set SELECT statement
</summary>
<param name= "Sqlstr" ></param>
public void Setselectcommand (string sqlstr)
{
Mydataadapter.selectcommand = new SqlCommand (sqlstr,mydataadapter.selectcommand.connection);
Cb. DataAdapter = MyDataAdapter;
Ds. Tables.clear ();
}

<summary>
Get data table
</summary>
<returns></returns>
Public DataTable getdatatable ()
{
Ds. Clear ();
Mydataadapter.fill (ds, "Table1");
Return DS. tables["Table1"];
}

<summary>
Get Data adapter
</summary>
<returns></returns>
Public SqlDataAdapter Getdataadapter ()
{
return mydataadapter;
}

<summary>
Update
</summary>
public void Update ()
{
Try
{
Mydataadapter.update (ds, "Table1");
}
catch (SqlException Sqlex)
{
MessageBox.Show ("Cause of Error:" +sqlex.) Message, "Error", Messageboxbuttons.ok,messageboxicon.error);
}
}
}
}




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.