当前位置: 代码迷 >> C# >> C#修改功能和添加功能出现异常,好像跟标识列有关,麻烦大神们看一下
  详细解决方案

C#修改功能和添加功能出现异常,好像跟标识列有关,麻烦大神们看一下

热度:293   发布时间:2016-05-05 05:14:10.0
C#修改功能和添加功能出现错误,好像跟标识列有关,麻烦大神们看一下
代码:
namespace MySchool
{
    public partial class StudentChangeForm : Form
    {
        public StudentChangeForm()
        {
            InitializeComponent();
        }

        private void StudentChangeForm_Load(object sender, EventArgs e)
        {
            // 加载数据对象
            try
            {
                SqlCommand command = new SqlCommand("select StudentId, StudentName, StudentNO, Sex, StudentIDNO,Major,Phone,Address,PostalCode from Student order by StudentId", DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    DataGridViewRowCollection dgrc = dgv.Rows;
                    Object[] objs = new Object[9];
                    objs[0] = reader["StudentId"].ToString();
                    objs[1] = reader["StudentName"].ToString();
                    objs[2] = reader["StudentNO"].ToString();
                    objs[3] = reader["Sex"].ToString();
                    objs[4] = reader["StudentIDNO"].ToString();
                    objs[5] = reader["Major"].ToString();
                    objs[6] = reader["Phone"].ToString();
                    objs[7] = reader["Address"].ToString();
                    objs[8] = reader["PostalCode"].ToString();
                    dgrc.Add(objs);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("登录失败:" + ex.ToString(), "提示");
            }
            finally
            {
                DBHelper.connection.Close();
            }
            CenterToScreen();
            dgv.ClearSelection();
            dgv.MultiSelect = false;
            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

        }


        private void dgv_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (e.StateChanged == DataGridViewElementStates.Selected)
            {
                DataGridViewRow row = e.Row;
                DataGridViewCellCollection cells = row.Cells;

                text1.Text = cells[0].Value.ToString();
                text2.Text = cells[1].Value.ToString();
                text3.Text = cells[2].Value.ToString();
                if (cells[3].Value.ToString().Equals("男")) radioSex.Select();
                else radioSex2.Select();
                text4.Text = cells[4].Value.ToString();
                text5.Text = cells[5].Value.ToString();
                text6.Text = cells[6].Value.ToString();
                text7.Text = cells[7].Value.ToString();
                text8.Text = cells[8].Value.ToString();
            }
        }

        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dgv.Rows[e.RowIndex];
            DataGridViewCellCollection cells = row.Cells;

            text1.Text = cells[0].Value.ToString();
            text2.Text = cells[1].Value.ToString();
            text3.Text = cells[2].Value.ToString();
            if (cells[3].Value.ToString().Equals("男")) radioSex.Checked = true;
            else radioSex2.Checked = true;
            text4.Text = cells[4].Value.ToString();
            text5.Text = cells[5].Value.ToString();
            text6.Text = cells[6].Value.ToString();
            text7.Text = cells[7].Value.ToString();
            text8.Text = cells[8].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (text1.Text.Length == 0)
            {
                MessageBox.Show("请输入学号!", "提示");
                return;
            }

            if (text2.Text.Length == 0)
            {
                MessageBox.Show("请输入姓名!", "提示");
                return;
            }



            char sex = radioSex.Checked ? '男' : '女';


            // 查询题目信息的sql语句
            string sql = "insert into Student ( StudentId, StudentName, StudentNO, Sex, StudentIDNO,Major,Phone,Address,PostalCode) values ('" + text1.Text;
            sql += "', '" + text2.Text + "','" + text3.Text + "','"+ sex + "', '" + text4.Text + "', '" + text5.Text + "', '" + text6.Text + "', '" + text7.Text + "', '" + text8.Text + "');";
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                command.ExecuteNonQuery();

                MessageBox.Show("添加成功!", "提示");

                DataGridViewRowCollection dgrc = dgv.Rows;
                Object[] objs = new Object[5];
                objs[0] = text1.Text;
                objs[1] = text2.Text;
                objs[2] = text3.Text;
                objs[3] = sex;
                objs[4] = text4.Text;
                objs[5] = text5.Text;
                objs[6] = text6.Text;
                objs[7] = text7.Text;
                objs[8] = text8.Text;
                int row = dgrc.Add(objs);
                dgv.CurrentCell = dgv.Rows[row].Cells[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败:" + ex.ToString(), "提示");
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dgv.CurrentRow;
            if (row == null)
            {
                MessageBox.Show("请先选择行对象", "提示");
                return;
            }

            string StudentId = row.Cells[0].Value.ToString();
            if (text1.Text.Length == 0)
            {
                MessageBox.Show("请输入学号!", "提示");
                return;
            }

            if (text2.Text.Length == 0)
            {
                MessageBox.Show("请输入姓名!", "提示");
                return;
            }



            char sex = radioSex.Checked ? '男' : '女';


            // 查询题目信息的sql语句
            string sql = String.Format("update Student set StudentId='{0}',StudentName='{1}', StudentNO='{2}', Sex='{3}', StudentIDNO='{4}',Major='{5}',Phone='{6}',Address='{7}',PostalCode='{8}'where StudentId='{9}';",
                text1 .Text , text2.Text, text3.Text, sex, text4.Text, text5.Text, text6.Text, text7.Text, text8.Text,StudentId );
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                command.ExecuteNonQuery();

                MessageBox.Show("修改成功!", "提示");

                row.Cells[0].Value = text1.Text;
                row.Cells[1].Value = text2.Text;
                row.Cells[2].Value = text3.Text;
                row.Cells[3].Value = sex;
                row.Cells[4].Value = text4.Text;
                row.Cells[5].Value = text5.Text;
                row.Cells[6].Value = text6.Text;
                row.Cells[7].Value = text7.Text;
                row.Cells[8].Value = text8.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败:" + ex.ToString(), "提示");
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }

错误页面:一个是无法更新标识列"StudentId",一个是当IDENTITY_INSERT设置为OFF时,不能为表Student中标识列插入显式值


------解决思路----------------------
      string sql = String.Format("update Student set StudentId='{0}',StudentName='{1}', StudentNO='{2}', Sex='{3}', StudentIDNO='{4}',Major='{5}',Phone='{6}',Address='{7}',PostalCode='{8}'where StudentId='{9}';",

把套红的干掉。也就是说ID那一列不能往回写
------解决思路----------------------
设置成主键,自动增长,那么它就不能手动修改了,而且你也不应该去修改它,即使你传入的数据和它原来的值一样也不行
  相关解决方案