c#事务、using释放资源、实体类的高级应用

来源:互联网 发布:淘宝带图评价 淘气值 编辑:程序博客网 时间:2024/05/18 03:03
namespace Day04_001事务{    class Program    {        static void Main(string[] args)        {            string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";            string sql="insert into grade (gradename) values ('S2226')";            using (SqlConnection conn=new SqlConnection(str))            {                SqlCommand cmd=new SqlCommand(sql,conn);                conn.Open();                SqlTransaction tran = conn.BeginTransaction();                cmd.Transaction = tran;                try                {                    int count = cmd.ExecuteNonQuery();                    if (count > 0)                    {                        Console.WriteLine("add ok");                    }                    tran.Commit();                }                catch (Exception)                {                                       tran.Rollback();                }                }            Console.ReadKey();        }    }}

namespace using释放资源{    class Program    {        static void Main(string[] args)        {            string sql="select * from student";            using (SqlConnection conn = new SqlConnection(SqlHelper.constr))            {                using (SqlCommand cmd = new SqlCommand(sql, conn))                {                    conn.Open();                    Console.WriteLine("高傲的分割线");                    Console.WriteLine("数据库");                                    }            }            Console.ReadKey();        }    }}

namespace Day05_001实体类的高级应用{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";            SqlConnection con=new SqlConnection(str);            string sql="select * from grade";            SqlDataAdapter da=new SqlDataAdapter(sql,con);            DataSet ds=new DataSet();            da.Fill(ds, "gradeinfo");            comboBox1.DataSource = ds.Tables[0];            comboBox1.ValueMember = "gradeid";            comboBox1.DisplayMember = "gradename";        }        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)        {            string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";            string gradename = comboBox1.Text;            int gradeid = GetIdByName(gradename);            SqlConnection con=new SqlConnection(str);            string sql = "select * from subject where gradeid=@gradeid";            SqlParameter para=new SqlParameter("@gradeid",gradeid);            SqlCommand cmd=new SqlCommand(sql,con);            cmd.Parameters.Add(para);            SqlDataAdapter da=new SqlDataAdapter();            da.SelectCommand = cmd;            DataSet ds=new DataSet();            da.Fill(ds, "subjectinfo");            comboBox2.DataSource = ds.Tables["subjectinfo"];            comboBox2.ValueMember = "subjectid";            comboBox2.DisplayMember = "subjectname";            if (ds.Tables["subjectinfo"].Rows.Count == 0)            {                ds.Tables["subjectinfo"].Clear();            }        }        //根据id获取年级名称        private int GetIdByName(string gradename)        {            string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";            SqlConnection con = new SqlConnection(str);            string sql = "select gradeid from grade where gradename='" + gradename + "'";            SqlCommand cmd=new SqlCommand(sql,con);            con.Open();            int gradeid = Convert.ToInt32(cmd.ExecuteScalar());            con.Close();            return gradeid;        }        //根据id获取科目名称        public int GetidByName(string subjectname)        {            string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";            SqlConnection con = new SqlConnection(str);            string sql = "select subjectid from subject where subjectname='" + subjectname + "'";            SqlCommand cmd = new SqlCommand(sql, con);            con.Open();            int gradeid = Convert.ToInt32(cmd.ExecuteScalar());            con.Close();            return gradeid;        }        public DataTable StudentInfo(int gradeid, int subjectid )        {            string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";            string sql = @"select  student.StudentNo, LoginPwd, StudentName, Gender, student.gradeid, Phone, Address, Birthday, Email from student, result,subject                          where subject.subjectid=result.subjectid and student.gradeid=@gradeid and result.subjectid=@subjectid ";            using (SqlConnection con = new SqlConnection(str))            {                SqlParameter[] paras =                   {                      new SqlParameter("@gradeid",gradeid),                                           new SqlParameter("@subjectid",subjectid)                                    };                SqlCommand cmd = new SqlCommand(sql, con);                cmd.Parameters.AddRange(paras);                con.Open();                SqlDataAdapter Adapter = new SqlDataAdapter();                Adapter.SelectCommand = cmd;                DataSet da = new DataSet();                Adapter.Fill(da, "pp");                return da.Tables["pp"];            }        }        private void button1_Click(object sender, EventArgs e)        {            string gradename = comboBox1.Text;            int gradeid = GetIdByName(gradename);            string subjectname = comboBox2.Text;            int subjectid = GetidByName(gradename);            dataGridView1.DataSource = StudentInfo(gradeid, subjectid);        }    }}

0 0
原创粉丝点击