代码改变世界

排球比赛积分程序

2016-12-18 16:40  lyx1212  阅读(289)  评论(0编辑  收藏  举报

一、计划

二、界面布局及代码

    class SqlHelper

    {//获取连接字符串

        private static readonly string constr = ConfigurationManager.ConnectionStrings["connectionStr"].ConnectionString;

        //ExecuteNonQuery()方法

        //ExecuteScalar()方法

        //ExecuteReader()方法

        //ExecuteDataTable()方法

         

        public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)

        {

            //使用using关键字定义一个范围,在范围结束时骸自动调用这个类实例的Dispose处理对象

            using (SqlConnection con = new SqlConnection(constr))

            {

                //创建执行DSql命令对象

                using (SqlCommand cmd = new SqlCommand(sql, con))

                {

                    //判断是否传递了sql参数

                    if (pms != null)

                    {

                        //讲参数添加到Parameters集合中

                        cmd.Parameters.AddRange(pms);

                    }

                    con.Open();

                    return cmd.ExecuteNonQuery();

                }

            }

 

        }

        //执行返回单个值的

        public static object ExecuteScalar(string sql, params SqlParameter[] pms)

        { 

            using (SqlConnection con = new SqlConnection(constr))

            {

                using (SqlCommand cmd = new SqlCommand(sql, con))

                {

                    if (pms != null)

                    {

                        cmd.Parameters.AddRange(pms);

                    }

                    con.Open();

                    return cmd.ExecuteScalar();

                }

            }

            

        }

        //执行返回SqlDataReader

        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)

        {

            SqlConnection con = new SqlConnection(constr);

            

                using (SqlCommand cmd = new SqlCommand(sql, con))

                {

                    if (pms != null)

                    {

                        cmd.Parameters.AddRange(pms);

                    }

                    try

                    {

                        con.Open();

                        return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

                    }

                    catch (Exception)

                    {

                        con.Close();

                        con.Dispose();

                        throw;

                    }

                }

        }

        //执行返回DataTable

        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)

        {

            DataTable dt = new DataTable();

            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, constr))

            {

                if (pms != null)

                {

                    adapter.SelectCommand.Parameters.AddRange(pms);

                }

                adapter.Fill(dt);

 

            }

            return dt;

        }

    }

  class Info

    {

        public string Name { get; set; }

        public int First { get; set; }

        public int Second { get; set; }

        public int Third { get; set; }

        public int Fourth { get; set; }

        public int Fifth { get; set; }

        public int Score { get; set; }

       

    }

     private void Select1_Click_1(object sender, EventArgs e)

   {

           string sql = "select * from Bifen where Name=@Name";

            SqlParameter[] paras = new SqlParameter[]

            {

                new SqlParameter("@Name",Name);   

            };

            using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, paras))

            {

                if (reader.Read())

                {

                   Info bf = new Info();

                   bf.Name = (string)reader["Name"];

                   bf.Second = reader.GetInt32(1);

                   bf.Third = reader.GetInt32(2);

                   bf.Fourth = reader.GetInt32(3);

                   bf.Fifth = reader.GetInt32(4);

                   bf.Score = reader.GetInt32(5);               

                }

            }

         }   

    }

private void button1_Click(object sender, EventArgs e)

{

if(comboBox1.selectIndex==0)

{

txtbox1.Text = bf.first.ToString();

}

 

if(comboBox1.selectIndex==1)

{

txtbox1.Text = bf.second.ToString();

}

 

if(comboBox1.selectIndex=2)

{

txtbox1.Text = bf.third.ToString();

}

if(comboBox1.selectIndex=3)

{

txtbox1.Text = bf.fourth.ToString();

}

if(comboBox1.selectIndex=4)

{

txtbox1.Text = bf.fifth.ToString();

}

 

 

private void button2_Click(object sender, EventArgs e)

{

    txtbox2.Text = bf.score.ToString();

}