C # Packaging---little practice

Source: Internet
Author: User


1. Build DbConnection
Constant to make a link string
2. Create entity class
Encapsulates a database's table wrapper class, enclosing the table's fields as member variables and attributes
3. Creating data Access Classes
A. Declaration of three members: Sqlconnection,sqlcommand,sqldatareader
B. In the constructor, instantiate SqlConnection and SqlCommand.
C. Do a series of methods of adding, deleting, changing and checking.
4.Main function
(1).
A. Call the data access class to get the data.
B. Use for display on the interface.
(2).
A. The input data is obtained from the interface, and there are variables.
B. Call the data access class, the variable into the corresponding method of adding and deleting, to achieve the corresponding operation of the database.

Database:

CREATE TABLE Stock
(
IDS varchar (primary key)--ticker symbol
Name varchar,--stock names
Start Decimal (18,2),--Open Price
Stop decimal (18,2),--closing price
Trans bigint--Volume
)
Go
INSERT into stock values (' s001 ', ' Jinan scooter ', 8.5,8.7,59874)
INSERT into stock values (' s002 ', ' Changjiang shares ', 7.2,7.0,35659)
INSERT into stock values (' s003 ', ' Pune Bank ', 6.5,7.1,32120)
INSERT into stock values (' s004 ', ' Shandong Aluminum ', 3.5,3.1,36520)
INSERT into stock values (' s005 ', ' Iron Dragon Stake ', 5.6,5.9,55460)
INSERT into stock values (' s006 ', ' Taurus shares ', 6.3,5.4,98630)
INSERT into stock values (' s007 ', ' Liao Yu Group ', 12.1,11.4,36541)
INSERT into stock values (' s008 ', ' CLP Power ', 9.6,8.7,25698)
SELECT * FROM Stock

Main function:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace Lian_fengzhuang
{
Class Program
{
static void Main (string[] args)
{
Showmain ();
Console.ReadLine ();
}
public static void Showmain ()
{
while (true)
{
Console.WriteLine ("1. Show all data in the table");
Console.WriteLine ("2. Add data");
Console.WriteLine ("3. Delete the corresponding number data");
Console.WriteLine ("4. Modify the corresponding number data");
Console.WriteLine ("5. Exit");
Console.Write ("Please enter your choice:");
String shu = Console.ReadLine ();
if (Shu = = "1")
{
New Caozuo (). Czshow ();
}
else if (Shu = = "2")
{
New Caozuo (). Czinsert ();
}
else if (Shu = = "3")
{
New Caozuo (). Czdelete ();
}
else if (Shu = = "4")
{
New Caozuo (). Czupdate ();
}
else if (Shu = = "5")
{
Break
}
Else
{
Console.WriteLine ("Please check carefully, re-enter!") ");
Showmain ();
}
}

}//Cycle Display Requirements
}
}

Calls under the main function class:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace Lian_fengzhuang
{
Class Caozuo
{
public void Czinsert ()
{
Console.WriteLine ("Please enter data to insert:");
Console.Write ("Stock Code:");
string ids = Console.ReadLine ();
String ke = czduancun (IDS);
if (ke = = "no")
{
Console.Write ("Stock name:");
String name = Console.ReadLine ();
Console.Write ("Open Price:");
Decimal start = Convert.todecimal (Console.ReadLine ());
Console.Write ("Closing price:");
Decimal stop = Convert.todecimal (Console.ReadLine ());
Console.Write ("Volume:");
int trans = Convert.ToInt32 (Console.ReadLine ());
New Stockda (). Insert (IDs, name, start, stop, trans);
}
else {Console.WriteLine ("The stock code you entered already exists!") "); }

}//Inserting Data operations
public void Czdelete ()
{
Console.WriteLine ("Please enter the number of data to be deleted:");
Console.Write ("Stock Code:");
string ids = Console.ReadLine ();
String ke = czduancun (IDS);
if (Ke = = "You")
{
New Stockda (). Delete (IDS);
}
else {Console.WriteLine ("The ticker symbol you entered does not exist!) "); }
}//Deleting Data operations
public void Czupdate ()
{
Console.WriteLine ("Please enter the data to be modified:");
Console.Write ("Stock Code:");
string ids = Console.ReadLine ();
String ke = czduancun (IDS);
if (Ke = = "You")
{
Console.Write ("Stock name:");
String name = Console.ReadLine ();
Console.Write ("Open Price:");
Decimal start = Convert.todecimal (Console.ReadLine ());
Console.Write ("Closing price:");
Decimal stop = Convert.todecimal (Console.ReadLine ());
Console.Write ("Volume:");
int trans = Convert.ToInt32 (Console.ReadLine ());
New Stockda (). Update (IDs, name, start, stop, trans);
}
else {Console.WriteLine ("The ticker symbol you entered does not exist!) "); }
}//Modifying Data operations
public void Czshow ()
{
List<stock> show = new Stockda (). Select ();
for (int i = 0; i < show. Count; i++)
{
Stock st = Show[i];
Console.Write (St. Ids + "\ t");
Console.Write (St. Name + "\ t");
Console.Write (St. Start + "\ t");
Console.Write (St. Stop + "\ t");
Console.Write (St. Trans + "\ n");
}
}//Displaying Data operations
public string Czduancun (string IDs)
{
string ok = "";
Stock st=new stockda (). Select (IDS);
if (st = = null)
{
OK = "no";
}
else {OK = "you";}
return OK;
}//determine if there is data
}
}

Instantiation:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Data;
Using System.Data.SqlClient;

Namespace Lian_fengzhuang
{
Class Stockda
{
Private SqlConnection _con;
Private SqlCommand _cmd;
Private SqlDataReader _dr;
Public stockda ()
{
_con=new SqlConnection (Dbconnection.liansql);
_cmd = _con.createcommand ();
}//constructor function
public void Delete (string IDs)
{
_cmd.commandtext = "Delete from stock where [email protected]";
_cmd.parameters.clear ();
_cmd.parameters.addwithvalue ("@ids", IDS);
Try
{
_con.open ();
_cmd.executenonquery ();

}
Finally
{
_con.close ();
}
}//Delete data based on the corresponding ID
public void Insert (string IDs, string name, decimal start, decimal stop, int trans)
{
_cmd.commandtext = "INSERT into stock values (@ids, @name, @start, @stop, @trans)";
_cmd.parameters.clear ();
_cmd.parameters.addwithvalue ("@ids", IDS);
_cmd.parameters.addwithvalue ("@name", name);
_cmd.parameters.addwithvalue ("@start", start);
_cmd.parameters.addwithvalue ("@stop", stop);
_cmd.parameters.addwithvalue ("@trans", trans);
Try
{
_con.open ();
_cmd.executenonquery ();
}
Finally
{
_con.close ();
}

}
Inserting data
public void Update (string IDs, string name, decimal start, decimal stop, int trans)
{
_cmd.commandtext = "Update stock set [email protected],[email protected],[email protected],[email protected] where [email Protected] ";
_cmd.parameters.clear ();
_cmd.parameters.addwithvalue ("@ids", IDS);
_cmd.parameters.addwithvalue ("@name", name);
_cmd.parameters.addwithvalue ("@start", start);
_cmd.parameters.addwithvalue ("@stop", stop);
_cmd.parameters.addwithvalue ("@trans", trans);
Try
{
_con.open ();
_cmd.executenonquery ();
}
Finally
{
_con.close ();
}
}
Modify the corresponding data according to the corresponding ID
Public list<stock> Select ()
{
List<stock> list=new list<stock> ();
_cmd.commandtext = "SELECT * from stock";
Try
{
_con.open ();
_DR = _cmd.executereader ();
while (_dr.read ())
{
Stock St = new Stock ();
St. ids = _dr["IDs"]. ToString ();
St. Name = _dr["Name"]. ToString ();
St. Start = (decimal) _dr["Start"];
St. stop= (decimal) _dr["Stop";
St. Trans=convert.toint32 (_dr["Trans"]);
List. ADD (ST);
}
}
Finally
{
_con.close ();
}
return list;

}//querying all data in a table
Public Stock Select (string IDs)
{
_cmd.commandtext = "SELECT * from stock where [email protected]";
_cmd.parameters.clear ();
_cmd.parameters.addwithvalue ("@ids", IDS);
Try
{
_con.open ();
_dr=_cmd.executereader ();
if (_dr.read ())
{
Stock St = new Stock ();
St. ids = _dr["IDs"]. ToString ();
St. Name = _dr["Name"]. ToString ();
St. Start = (decimal) _dr["Start"];
St. stop = (decimal) _dr["Stop"];
St. trans = (int) _dr["Trans"];
Return St;
}
Else
{return null;}
}
Finally
{
_con.close ();
}
}//Query the appropriate information according to IDs
}
}

Tool class:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace Lian_fengzhuang
{
Class Stock
{
private string _ids;

public string Ids
{
get {return _ids;}
set {_ids = value;}
}
private string _name;

public string Name
{
get {return _name;}
set {_name = value;}
}
Private decimal _start;

Public decimal Start
{
get {return _start;}
set {_start = value;}
}
Private decimal _stop;

Public decimal Stop
{
get {return _stop;}
set {_stop = value;}
}
private int _trans;

public int Trans
{
get {return _trans;}
set {_trans = value;}
}
}
}

Link string:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace Lian_fengzhuang
{
Class DBConnection
{
Public Const string liansql = "server=.; Database=lian;uid=sa;pwd=123 ";
}
}

C # Packaging---little practice

Related Article

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.