標籤:
1.造DBConnection
做一個連結字串的常量
2.造實體類
把資料庫的表封裝類,把表的欄位都封狀成成員變數和屬性
3.造資料訪問類
a.聲明三個成員:SqlConnection,SqlCommand,SqlDataReader
b.在建構函式中,把SqlConnection和SqlCommand執行個體化。
c.做一系列的增、刪、改、查的方法。
4.Main函數
(1)。
a.調用資料訪問類,獲得資料。
b.在介面上使用for顯示出來。
(2)。
a.從介面上獲得輸入的資料,存在變數中。
b.調用資料訪問類,把變數傳進相應的增刪改的方法中去,實現資料庫相應操作。
資料庫:
create table stock
(
ids varchar(10) primary key, --股票看板代號
name varchar(50) ,--股票名稱
start decimal(18,2),--開盤價
stop decimal(18,2),--收盤價
trans bigint--成交量
)
go
insert into stock values(‘s001‘,‘濟南輕騎‘,8.5,8.7,59874)
insert into stock values(‘s002‘,‘長江股份‘,7.2,7.0,35659)
insert into stock values(‘s003‘,‘浦發銀行‘,6.5,7.1,32120)
insert into stock values(‘s004‘,‘山東鋁業‘,3.5,3.1,36520)
insert into stock values(‘s005‘,‘鐵龍股份‘,5.6,5.9,55460)
insert into stock values(‘s006‘,‘金牛股份‘,6.3,5.4,98630)
insert into stock values(‘s007‘,‘遼漁集團‘,12.1,11.4,36541)
insert into stock values(‘s008‘,‘中華電力‘,9.6,8.7,25698)
select * from stock
主函數:
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.顯示表中所有資料");
Console.WriteLine("2.添加資料");
Console.WriteLine("3.刪除相應編號資料");
Console.WriteLine("4.修改相應編號資料");
Console.WriteLine("5.退出");
Console.Write("請輸入您的選擇:");
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("請認真查看,重新輸入!");
Showmain();
}
}
}//迴圈顯示要求
}
}
主函數類下的調用:
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("請輸入要插入的資料:");
Console.Write("股票看板代號:");
string ids = Console.ReadLine();
string ke = Czduancun(ids);
if (ke == "no")
{
Console.Write("股票名稱:");
string name = Console.ReadLine();
Console.Write("開盤價:");
decimal start = Convert.ToDecimal(Console.ReadLine());
Console.Write("收盤價:");
decimal stop = Convert.ToDecimal(Console.ReadLine());
Console.Write("成交量:");
int trans = Convert.ToInt32(Console.ReadLine());
new StockDA().Insert(ids, name, start, stop, trans);
}
else { Console.WriteLine("您輸入的股票代號已存在!"); }
}//插入資料操作
public void CzDelete()
{
Console.WriteLine("請輸入要刪除資料的編號:");
Console.Write("股票看板代號:");
string ids = Console.ReadLine();
string ke = Czduancun(ids);
if (ke == "you")
{
new StockDA().Delete(ids);
}
else { Console.WriteLine("您輸入的股票看板代號不存在!"); }
}//刪除資料操作
public void CzUpdate()
{
Console.WriteLine("請輸入要修改的資料:");
Console.Write("股票看板代號:");
string ids = Console.ReadLine();
string ke = Czduancun(ids);
if (ke == "you")
{
Console.Write("股票名稱:");
string name = Console.ReadLine();
Console.Write("開盤價:");
decimal start = Convert.ToDecimal(Console.ReadLine());
Console.Write("收盤價:");
decimal stop = Convert.ToDecimal(Console.ReadLine());
Console.Write("成交量:");
int trans = Convert.ToInt32(Console.ReadLine());
new StockDA().Update(ids, name, start, stop, trans);
}
else { Console.WriteLine("您輸入的股票看板代號不存在!"); }
}//修改資料操作
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");
}
}//顯示資料操作
public string Czduancun(string ids)
{
string ok = "";
Stock st=new StockDA().Select(ids);
if (st == null)
{
ok = "no";
}
else { ok = "you"; }
return ok;
}//判斷是否存在相應資料
}
}
執行個體化:
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();
}//建構函式
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();
}
}//根據相應的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();
}
}
//插入資料
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();
}
}
//根據相應的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;
}//查詢表中所有資料
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();
}
}//根據ids查詢相應資訊
}
}
工具類:
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; }
}
}
}
連結字串:
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#封裝---小練