ylbtech-LearningNotes:LNotes-ADO.NET-ADO.NET访问数据库(SqlServer,Oracle)增删改查操作示例 |
ADO.NET访问数据库(SqlServer,Oracle)增删改查操作示例。
C# --sql --Oracle |
|
|
|
空间名 |
|
|
|
Com.Myt.DAL |
|
|
数据库/sql |
|
|
|
增 |
//增
public static void Insert(ProductInfo product)
{
SqlConnection con = new DBConnection().Con;
SqlCommand com = new SqlCommand();
string sql1 = "insert into product(productname,unitprice) values(@productname,@unitprice)";
com.Connection = con;
com.CommandText = sql1;
com.Parameters.Add(new SqlParameter("@productname", product.Productname));
com.Parameters.Add(new SqlParameter("@unitprice", product.Unitprice));
con.Open();
com.ExecuteNonQuery();
con.Close();
}
|
|
|
|
|
|
|
删 |
//删
public static void Delete(int productid)
{
SqlConnection con = new DBConnection().Con;
SqlCommand com = new SqlCommand();
string sql1 = "delete product where productid=@productid";
com.Connection = con;
com.CommandText = sql1;
com.Parameters.Add(new SqlParameter("@productid", productid));
con.Open();
com.ExecuteNonQuery();
con.Close();
}
|
|
|
|
|
|
|
改 |
//改
public static void Update(ProductInfo product)
{
SqlConnection con = new DBConnection().Con;
SqlCommand com = new SqlCommand();
string sql1 = "update product set productname=@productname,unitprice=@unitprice,intime=@intime where productid=@productid";
com.Connection = con;
com.CommandText = sql1;
com.Parameters.Add(new SqlParameter("@productname", product.Productname));
com.Parameters.Add(new SqlParameter("@unitprice", product.Unitprice));
com.Parameters.Add(new SqlParameter("@intime", product.Intime));
com.Parameters.Add(new SqlParameter("@productid", product.Productid));
con.Open();
com.ExecuteNonQuery();
con.Close();
}
|
|
|
|
|
|
|
查全部 |
//查全部
public static IList SelectAll()
{
IList ar1 = new ArrayList();
SqlConnection con = new DBConnection().Con;
SqlCommand com = new SqlCommand();
string sql1 = "select productid,productname,unitprice,intime from product";
com.Connection = con;
com.CommandText = sql1;
con.Open();
SqlDataReader sdr = com.ExecuteReader();
while (sdr.Read())
{
ProductInfo product = new ProductInfo(sdr.GetInt32(0), sdr.GetString(1), sdr.GetDecimal(2), sdr.GetDateTime(3));
ar1.Add(product);
}
con.Close();
return ar1;
|
|
|
|
|
|
|
查单一 |
//查单一
public static ProductInfo Select(int productid)
{
ProductInfo pro = null;
SqlConnection con = new DBConnection().Con;
SqlCommand com = new SqlCommand();
string sql1 = "select productid,productname,unitprice,intime from product where productid=@productid";
com.Connection = con;
com.CommandText = sql1;
con.Open();
com.Parameters.Add(new SqlParameter("@productid",productid));
SqlDataReader sdr = com.ExecuteReader();
while (sdr.Read())
{
pro = new ProductInfo(sdr.GetInt32(0), sdr.GetString(1), sdr.GetDecimal(2), sdr.GetDateTime(3));
}
con.Close();
return pro;
}
|
|
|
|
|
|
|
模糊查 |
//模糊查
public static IList SelectByName(string productname)
{
IList ar1 = new ArrayList();
SqlConnection con = new DBConnection().Con;
SqlCommand com = new SqlCommand();
string sql1 = "select productid,productname,unitprice,intime from product where productname like @productname";
com.Connection = con;
com.CommandText = sql1;
com.Parameters.Add(new SqlParameter("@productname", "%"+productname+"%"));
con.Open();
SqlDataReader sdr = com.ExecuteReader();
while (sdr.Read())
{
ProductInfo product = new ProductInfo(sdr.GetInt32(0), sdr.GetString(1), sdr.GetDecimal(2), sdr.GetDateTime(3));
ar1.Add(product);
}
con.Close();
return ar1;
}
|
|
|
|
|
|
|
空间名 |
|
|
|
Com.Myt.BLL |
数据库/Oracle |
|
|
|
增 |
//增
public static void Insert()
{
Console.WriteLine("请输入商品名称");
string productname = IOOperate.GetString();
Console.WriteLine("请输入商品价格");
decimal unitprice = IOOperate.GetDecimal();
Console.WriteLine("请输入商品名称");
string productname = IOOperate.GetString();
//Console.WriteLine("请输入数量");
//int amount = IOOperate.GetInt();
//Console.WriteLine("请输入时间");
//DateTime intime = IOOperate.GetDateTime();
ProductInfo product1 = new ProductInfo(0, productname, unitprice, System.DateTime.Now);
ProductOper.Insert(product1);
Console.WriteLine("添加商品成功!!");
}
|
|
|
|
|
删 |
//删
public static void Delete()
{
Console.WriteLine("请输入商品编号");
int productid = IOOperate.GetInt();
ProductInfo pro = ProductOper.Select(productid);
if (pro == null)
{
Console.WriteLine("您要销货的商品不存在!!");
}
else
{
ProductOper.Delete(productid);
Console.WriteLine("注销商品成功!!");
}
}
|
|
|
|
|
改 |
//改
public static void Update()
{
Console.WriteLine("请输入商品编号");
int productid = IOOperate.GetInt();
ProductInfo pro = ProductOper.Select(productid);
if(pro==null)
{
Console.WriteLine("您要盘货的商品不存在!!");
}
else
{
Console.WriteLine("请输入商品新进货名称");
string productname = IOOperate.GetString();
Console.WriteLine("请输入商品新进货价格");
decimal unitprice = IOOperate.GetDecimal();
Console.WriteLine("请输入商品新进货日期(yyyy-MM-dd HH:mi:ss)");
System.DateTime intime = Convert.ToDateTime(Console.ReadLine());
pro = new ProductInfo(productid, productname, unitprice, intime);
ProductOper.Update(pro);
Console.WriteLine("查理此商品成功!!");
}
}
|
|
|
|
|
查全部 |
//查全部
public static void SelectAll()
{
IList ar1= ProductOper.SelectAll();
Console.WriteLine("编号\t名称\t价格\t日期");
for (int i = 0; i < ar1.Count; i++)
{
ProductInfo pro = (ProductInfo)ar1[i];
pro.Show();
}
}
|
|
|
|
|
查单一 |
//查单一
public static void SelectById()
{
Console.WriteLine("请输入商品编号");
int productid = IOOperate.GetInt();
ProductInfo pro = ProductOper.Select(productid);
if (pro==null)
{
Console.WriteLine("此商品信息不存在!!");
}
else
{
Console.WriteLine("编号\t名称\t价格\t日期");
pro.Show();
}
}
|
|
|
|
|
模糊查 |
//模糊查
public static void SelectByName()
{
Console.WriteLine("请输入商品名称");
string productname = IOOperate.GetString();
IList ar1 = ProductOper.SelectByName(productname);
if (ar1.Count <= 0)
{
Console.WriteLine("没找到您所需要的商品信息");
}
else
{
Console.WriteLine("编号\t名称\t价格\t日期");
for (int i = 0; i < ar1.Count; i++)
{
ProductInfo pro = (ProductInfo)ar1[i];
pro.Show();
}
}
}
|
|
|
|
|
dreamoflan&wanghuikai |
Mar 24,1010 你说咱们是兄弟,就一辈子不分开,在一块好好的玩,一起喝酒,一起打篮球,谁敢得罪咱,就让他好看 |
|
|
|
|
|
|
无。