首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 数据库更新有关问题

2013-01-19 
c# 数据库更新问题private void AdminPlacement_Load(object sender, EventArgs e){pobjconn.Open()SqlCo

c# 数据库更新问题

        private void AdminPlacement_Load(object sender, EventArgs e)
        {
            pobjconn.Open();
            SqlCommand pobjcom = new SqlCommand();
            pobjcom.CommandText = "select CourseName,ElectivesID from View_1";
            pobjcom.Connection = pobjconn;
            pobjcom.ExecuteNonQuery();
            SqlDataAdapter pobjdat = new SqlDataAdapter(pobjcom);
            pobjdat.Fill(table);
            comboBox1.DataSource = table;
            comboBox1.DisplayMember = "CourseName";
            comboBox1.ValueMember = "ElectivesID";
            pobjconn.Close();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            table1.Clear();
            pobjconn.Open();
            SqlCommand pobjcom = new SqlCommand();
            pobjcom.CommandText = "select CourseName as 选修课,TeacherName as 教师,StuName as 学生 from AdminPlacement where ElectivesID=@ElectivesID";
            pobjcom.Parameters.Add("@ElectivesID", SqlDbType.Int).Value = comboBox1.SelectedValue;
            pobjcom.Connection = pobjconn;
            pobjcom.ExecuteNonQuery();
            SqlDataAdapter pobjdat1 = new SqlDataAdapter(pobjcom);
            pobjdat1.Fill(table1);
            dataGridView1.DataSource = table1;
            dataGridView1.AllowUserToAddRows = false;
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
                dataGridView1.Columns[j].SortMode = DataGridViewColumnSortMode.NotSortable;
            pobjconn.Close();
            textBox1.Text = dataGridView1.Rows.Count.ToString();
        }

        private void button2_Click(object sender, EventArgs e)


        {
            if (int.Parse(textBox1.Text.ToString()) > 30 && int.Parse(textBox1.Text.ToString()) < 50)
            {
                textBox2.Text = "1100";
                Class.Class1 = textBox2.Text;
                pobjconn.Open();
                SqlCommand pobjcom = new SqlCommand();
                pobjcom.CommandText = "update ElectiveDetails set Class=1100";

                pobjcom.Connection = pobjconn;
                pobjcom.ExecuteNonQuery();
                pobjconn.Close();
                MessageBox.Show("班级生成成功!");
            }
            else if (int.Parse(textBox1.Text.ToString()) < 30)
                MessageBox.Show("人数不够不予开班!");
          
        }


c# 数据库更新有关问题
入门新手想问下,我这样在生成班级的时候,只能生成“1100”,如何修改代码,可以循环把班级加上去,就是第二次生成的班级是1101..1102..1103  这样。还有就是  代码有累赘的地方,希望大家可以指导下。
数据库 c# textbox table object
[解决办法]
        private void AdminPlacement_Load(object sender, EventArgs e)
        {
            pobjconn.Open();
            SqlCommand pobjcom = new SqlCommand();
            pobjcom.CommandText = "select CourseName,ElectivesID from View_1";
            pobjcom.Connection = pobjconn;
            pobjcom.ExecuteNonQuery();
            SqlDataAdapter pobjdat = new SqlDataAdapter(pobjcom);
            pobjdat.Fill(table);
            comboBox1.DataSource = table;
            comboBox1.DisplayMember = "CourseName";
            comboBox1.ValueMember = "ElectivesID";


            pobjconn.Close();
        }
 
        private void button1_Click_1(object sender, EventArgs e)
        {
            table1.Clear();
            pobjconn.Open();
            SqlCommand pobjcom = new SqlCommand();
            pobjcom.CommandText = "select CourseName as 选修课,TeacherName as 教师,StuName as 学生 from AdminPlacement where ElectivesID=@ElectivesID";
            pobjcom.Parameters.Add("@ElectivesID", SqlDbType.Int).Value = comboBox1.SelectedValue;
            pobjcom.Connection = pobjconn;
            pobjcom.ExecuteNonQuery();
            SqlDataAdapter pobjdat1 = new SqlDataAdapter(pobjcom);
            pobjdat1.Fill(table1);
            dataGridView1.DataSource = table1;
            dataGridView1.AllowUserToAddRows = false;
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
                dataGridView1.Columns[j].SortMode = DataGridViewColumnSortMode.NotSortable;
            pobjconn.Close();
            textBox1.Text = dataGridView1.Rows.Count.ToString();
        }
 
private int classNum = 1100;
        private void button2_Click(object sender, EventArgs e)
        {
            if (int.Parse(textBox1.Text.ToString()) > 30 && int.Parse(textBox1.Text.ToString()) < 50)
            {
                textBox2.Text = classNum.ToString();
                Class.Class1 = textBox2.Text;
                pobjconn.Open();
                SqlCommand pobjcom = new SqlCommand();
                pobjcom.CommandText = "update ElectiveDetails set Class=1100";
 
                pobjcom.Connection = pobjconn;


                pobjcom.ExecuteNonQuery();
                pobjconn.Close();
                MessageBox.Show("班级生成成功!");
            }
            else if (int.Parse(textBox1.Text.ToString()) < 30)
                MessageBox.Show("人数不够不予开班!");
           
        }
红色代码是多余的,因为使用SqlDataAdapter的时候不需要显示的开闭连接,也不需要ExecuteNonQuery,否则就执行2次了。

绿色代码是新更正的,定义一个字段记录就可以累加了。

热点排行