六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

asp.net之C#编程简洁小技巧

[复制链接]

升级  4%

58

主题

58

主题

58

主题

举人

Rank: 3Rank: 3

积分
212
 楼主| 发表于 2013-1-26 13:38:20 | 显示全部楼层 |阅读模式
SqlConnection conn = DBFactory.getSqlConn();            SqlCommand cmd = new SqlCommand();            string querySql = "SELECT [id], [username], [password] FROM [User] where username = @username";            cmd.CommandText = querySql;            cmd.Connection = conn;             cmd.Parameters.AddWithValue("@username", "樊");            conn.Open();            SqlDataReader reader =  cmd.ExecuteReader();            while (reader.Read())            {                Label1.Text = reader["password"].ToString();            }            reader.Close();            conn.Close();SqlConnection conn = DBFactory.getSqlConn();            SqlCommand cmd = new SqlCommand();            string querySql = "SELECT [id], [username], [password] FROM [User] where username = @username";            cmd.CommandText = querySql;            cmd.Connection = conn;             cmd.Parameters.AddWithValue("@username", "樊");            conn.Open();            SqlDataAdapter adapter = new SqlDataAdapter(cmd);            DataSet ds = new DataSet();            adapter.Fill(ds);                                      foreach (DataRow dr in ds.Tables[0].Rows)            {                Response.Write(dr["password"].ToString()+"<br>");            }                 conn.Close(); //Linq的简单使用            DBFactoryDataContext context = new DBFactoryDataContext();                       //增加            Student stud1 = new Student();            stud1.username = "fuck";            stud1.password = "fuck";            context.Student.InsertOnSubmit(stud1);            context.SubmitChanges();                        //删除            var query1 = from student in context.Student where student.username =="小樊" select student;            foreach (var stu in query1)            {                context.Student.DeleteOnSubmit(stu);            }                        //修改            var query2 = from student in context.Student where student.username == "fuck" select student;            foreach (var stu in query2)            {                stu.password = "funkUpdate";            }            context.SubmitChanges();                        //查询            Table<Student> list = context.GetTable<Student>();            var query3 = from student in list select student;            foreach (var stu in query3)            {                Response.Write(stu.username+"====="+stu.password+"<br>");            }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表