c# - 如何修复 C# 中键约束的重复?
问题描述
这是我的代码,
//Set the property AllowUserToAddRows to false will prevent a new empty row
dataGridView1.AllowUserToAddRows = false;
//registration of students
connect.Open();
SqlCommand cmd = connect.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO TableStudents(Student_ID,Fullname,Gender,Course,Year,Section,Guardian,Contact,Address)VALUES('" + textBox2.Text + "','" + textBox1.Text + "','" + ComboBox4.Text + "','" + ComboBox1.Text + "','" + ComboBox2.Text + "','" + ComboBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')";
cmd.ExecuteNonQuery();
connect.Close();
当我尝试单击我的注册按钮时,我的datagridview中出现了一个空格,那么如果它总是在datagridview中添加一个空格,我该如何修复注册按钮或者提示一些警告
解决方案
如果您已将 Student_ID 字段设置为数据库中的主键,则无需将其包含在插入语句中,您可以简单地排除或使该字段在 Datagrid 中不可见:
cmd.CommandText = "INSERT INTO TableStudents(Fullname,Gender,Course,Year,Section,Guardian,Contact,Address)VALUES(textBox1.Text + "','" + ComboBox4.Text + "','" + ComboBox1.Text + "','" + ComboBox2.Text + "','" + ComboBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')";
推荐阅读
- image - 部分图片文件无法在 Android Q 上修改
- javascript - 我想在 javascript 或 Jquery 中使用 XPath 3.0 语言支持
- javascript - 无法在反应组件中显示来自提取的序列化 api 的信息
- hy - Hy的漂亮印刷品?
- python - Python 中的数组操作 HackerRank
- java - 由于无法比较的类型,我的 java 代码出错
- java - 在 mainList 中存储 tempList | Java 集合
- python - 如何高效地拆分时间序列数据集?
- python - 尝试在 Windows 10 上使用 pip 安装 opencv-python 时出错
- python - 带有 3 个参数的 Python Sqlite 更新