我的大学,那可爱的代码

转眼工作也快4个月了,今天又兴趣回头看了以前在学校做的项目,感觉好当时的代码好可爱啊,找了一段来读读,我不想从这里面挑毛病什么什么的,只是回想当时自己的心态,和一些见解,你们就不要喷我了,好了,开始,我想把这段当时的代码应该是ADO.NET技术类别吧,好开始看看吧:

public class Flowers
{
    private SqlConnection Con;//声明SqlConnection 对象,没有初始化
 public Flowers()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
        Con = new SqlConnection();//SqlConnection 对象初始化
        Con.ConnectionString = "user id=sa;password=123456;initial catalog=ST_FlowerPrearrange;data source=.";//给Con对象的ConnectionString属性赋值
 }

//这边可以看出水平了,没有用对象,而是用的基础类型,参数有多个,不好阅读和维护,不过就不管她了,我看了,感觉真的好可爱

    public void addFlowers(string flowerName, int typeFlower, Decimal price , Decimal counts, string cover, int hits, int sales , Boolean status, string desr )
    {
        string sqlStr = "INSERT INTO ST_Flower(ST_Name, ST_Type, ST_Price, ST_Discount, ST_Cover, ST_Hits, ST_Sales, ST_Status, ST_Description)VALUES"+
            "('" + flowerName + "'," + typeFlower + "," + price + "," + counts + ", '" + cover + "'," + hits + "," + sales + ",'" + status + "','" + desr + "')";
        SqlCommand cmd = new SqlCommand(sqlStr, Con);//声明并初始化SqlCommand对象用到了sqlStr, Con两个参数 
        try
        {
            Con.Open();
            cmd.ExecuteNonQuery();//提交数据库并执行sql语句
            Con.Close();
        }
        catch (Exception ex)
        {
            Con.Close();
        }

    }

//判断鲜花的姓名是否已经存在

    public int IsOnlyFloName(string name)
    {
        string sqlStr = "SELECT [ST_FlowerId], [ST_Name] FROM [ST_Flower] where [ST_Name]='" + name + "'";
        // OleDbDataAdapter da=new OleDbDataAdapter("select * from 日常帐目",conn);
        SqlDataAdapter da = new SqlDataAdapter(sqlStr, Con);
        DataSet ds = new DataSet();
        da.Fill(ds, "data");
        //string str =ds.Tables["data"].Rows[0][0].ToString();
        int i = ds.Tables["data"].Rows.Count;
        return i;//要是存在会返回存在个数的,不存在就会返回0
    }

//按照鲜花的编号来获取鲜花的姓名

    public string getFloName(int id)
    {
        string sqlStr = "SELECT [ST_FlowerId], [ST_Name] FROM [ST_Flower]  where [ST_FlowerId]='" + id + "'";
        // OleDbDataAdapter da=new OleDbDataAdapter("select * from 日常帐目",conn);
        SqlDataAdapter da = new SqlDataAdapter(sqlStr, Con);//因为不用改变数据库的值,只是查看就用SqlDataAdapter对象,sqlStr, Con两个参数,和SqlCommand一样
        DataSet ds = new DataSet();//因为是取值所以会有返回值,声明DataSet 来存储返回的结果,DataSet 其实是内存中的数据库,我们这样认为就好了
        da.Fill(ds, "data");//da来为ds进行值得存储规范,表示返回值存放在DataSet 中用“data”来做表名的表
        string str = ds.Tables["data"].Rows[0][1].ToString();//返回值存放在DataSet 中“data”表中,所以用ds.Tables["data"]的形式来查看数据,注意Rows是从[0][0]开始的。
        return str;

    }
}

原文地址:https://www.cnblogs.com/lmfeng/p/2132101.html