1. 程式人生 > >c#事務、using釋放資源、實體類的高階應用

c#事務、using釋放資源、實體類的高階應用

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 
[email protected]
"; 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 [email protected] and [email protected] "; 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); } } }