xiaofancn 发表于 2013-1-26 13:38:20

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

SqlConnection conn = DBFactory.getSqlConn();            SqlCommand cmd = new SqlCommand();            string querySql = "SELECT , , FROM 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 , , FROM 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.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>");            }
页: [1]
查看完整版本: asp.net之C#编程简洁小技巧