无法启用约束。一行或多行包含违反非空,唯一或外键约束的值
我的错误很简单,但对我来说很难修复它,因为我的知识不好。这是当我通过单击AddStudent记录添加按钮输入记录,然后记录,我删除记录StudentID = 1,名字= Raju,姓氏= Abbasi无法启用约束。一行或多行包含违反非空,唯一或外键约束的值
后删除它,然后按保存更改甚至之后,当我再次尝试使用ID = 1只表示与ID = 1进入记录,我已被删除并记录StudentID = 1,名字=拉姆,姓氏=俏皮。然后误差发生的是
private static SqlDataAdapter CreateSutdentDataAdapter() { string gettSQL = "SELECT * FROM Student"; string insertSQL = "SET IDENTITY_INSERT Student ON;INSERT INTO Student(StudentID, FirstName,LastName,Gender,GPA,MyImage)" + "VALUES (@StudentID,@FirstName,@LastName,@Gender,@GPA,@MyImage);SET IDENTITY_INSERT Student OFF"; string updateSQL = "UPDATE Student SET [email protected],[email protected] ,[email protected], [email protected]," + " [email protected] WHERE [email protected]"; string deleteSQL = "DELETE FROM Student WHERE [email protected]"; SqlDataAdapter dataAdapter = new SqlDataAdapter(); dataAdapter.SelectCommand = new SqlCommand(gettSQL, ConnectionManager.GetConnection()); dataAdapter.InsertCommand = new SqlCommand(insertSQL, ConnectionManager.GetConnection()); dataAdapter.InsertCommand.Parameters.Add("@StudentID", SqlDbType.Int).SourceColumn = "StudentID"; dataAdapter.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar,25).SourceColumn = "FirstName"; dataAdapter.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 25).SourceColumn = "LastName"; dataAdapter.InsertCommand.Parameters.Add("@Gender", SqlDbType.VarChar ,1).SourceColumn = "Gender"; dataAdapter.InsertCommand.Parameters.Add("@GPA", SqlDbType.Float).SourceColumn = "GPA"; dataAdapter.InsertCommand.Parameters.Add("@MyImage", SqlDbType.VarBinary).SourceColumn = "MyImage"; dataAdapter.UpdateCommand = new SqlCommand(updateSQL, ConnectionManager.GetConnection()); dataAdapter.UpdateCommand.Parameters.Add("@StudentID", SqlDbType.Int).SourceColumn = "StudentID"; dataAdapter.UpdateCommand.Parameters.Add("@FirstName", SqlDbType.VarChar,25).SourceColumn = "FirstName"; dataAdapter.UpdateCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 25).SourceColumn = "LastName"; dataAdapter.UpdateCommand.Parameters.Add("@Gender", SqlDbType.VarChar ,1).SourceColumn = "Gender"; dataAdapter.UpdateCommand.Parameters.Add("@GPA", SqlDbType.Float).SourceColumn = "GPA"; dataAdapter.UpdateCommand.Parameters.Add("@MyImage", SqlDbType.VarBinary).SourceColumn = "MyImage"; dataAdapter.DeleteCommand = new SqlCommand(deleteSQL, ConnectionManager.GetConnection()); dataAdapter.DeleteCommand.Parameters.Add("@StudentID", SqlDbType.Int).SourceColumn = "StudentID"; return dataAdapter; } private static void DefinestudentTableSchema(DataTable table) { DataColumn StudentIDColumn = table.Columns.Add("StudentID", typeof(string)); StudentIDColumn.AllowDBNull = false; table.PrimaryKey = new DataColumn[] { StudentIDColumn }; DataColumn StudentFirstName = table.Columns.Add("FirstName", typeof(string)); StudentFirstName.MaxLength = 150; DataColumn StudentLastName = table.Columns.Add("LastName", typeof(string)); StudentLastName.MaxLength = 150; DataColumn StudentGender = table.Columns.Add("Gender", typeof(string)); DataColumn StudentGPA = table.Columns.Add("GPA", typeof(string)); DataColumn StudentImage = table.Columns.Add("MyImage", typeof(Byte[])); } private static DataSet CreateStudentTrackerDataSet() { DataSet StudentTrackerDataSet = new DataSet(); DataTable StudentTable = StudentTrackerDataSet.Tables.Add("Student"); DefinestudentTableSchema(StudentTable); return StudentTrackerDataSet; } public static DataSet GetData() { DataSet StudentTrakerDataSet = CreateStudentTrackerDataSet(); StudentTrakerDataSet.EnforceConstraints = false; StudentDataAdapter.Fill(StudentTrakerDataSet.Tables["Student"]); StudentTrakerDataSet.EnforceConstraints = true; return StudentTrakerDataSet; } public AddModifyStudentRecords(DataSet ds, DataRow row) { public static void SaveData(ref DataSet changesDataSet) { DataSet addedDataSet = changesDataSet.GetChanges(DataRowState.Added); if (addedDataSet != null) { StudentDataAdapter.Update(addedDataSet.Tables["Student"]); changesDataSet.Merge(addedDataSet); // Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints } DataSet modifiedDataSet = changesDataSet.GetChanges(DataRowState.Modified); if (modifiedDataSet != null) { StudentDataAdapter.Update(modifiedDataSet.Tables["Student"]); changesDataSet.Merge(modifiedDataSet); } DataSet deletedDataSet = changesDataSet.GetChanges(DataRowState.Deleted); if (deletedDataSet != null) { StudentDataAdapter.Update(deletedDataSet.Tables["Student"]); deletedDataSet.Merge(deletedDataSet); }
这里是我的Addmodifyform逻辑,用于添加StudentID
public partial class AddModifyStudentRecords : Form { DataSet StudentTrackerDataSet; DataRow currentRow; public AddModifyStudentRecords() { InitializeComponent(); } public AddModifyStudentRecords(DataSet ds) { InitializeComponent(); StudentTrackerDataSet = ds; currentRow = null; } public AddModifyStudentRecords(DataSet ds, DataRow row) { InitializeComponent(); StudentTrackerDataSet = ds; currentRow = row; textBox1.Text =currentRow["StudentID"] .ToString(); textBox2.Text = currentRow["FirstName"].ToString(); textBox4.Text = currentRow["LastName"].ToString(); textBox3.Text = currentRow["Gender"].ToString(); textBox5.Text = currentRow["GPA"].ToString(); txtBrowseFile.Text = currentRow["MyImage"].ToString(); byte[] data = (byte[])currentRow ["MyImage"]; MemoryStream ms = new MemoryStream(data); pictureBox1.Image = Image.FromStream(ms); string StudentID = textBox1.Text.ToString(); string StudentFirstName = textBox2.Text.ToString(); string StudentLastName = textBox4.Text.ToString(); string Gender = textBox3.Text.ToString(); string GPA = textBox5.Text.ToString(); Image MyImage = pictureBox1.Image; DataTable table = StudentTrackerDataSet.Tables["Student"]; if (currentRow == null) { currentRow = table.NewRow(); currentRow["StudentID"] = textBox1.Text.ToString(); table.Rows.Add(currentRow); } currentRow .BeginEdit(); currentRow ["StudentID" ]=StudentID ; currentRow["FirstName"] = StudentFirstName; currentRow["LastName"] = StudentLastName; currentRow["Gender"] = Gender; currentRow["GPA"] = GPA; currentRow["MyImage"] = convertToByte(txtBrowseFile.Text); currentRow.EndEdit(); Close(); }
公共部分类AditStudent:表格 { //创建类可变数据集以跟踪学生 私人DataSet StudentTrackerDataset;
public AditStudent()
{
InitializeComponent();
dataGridView1.DataError += DataGridView1_DataError;
StudentTrackerDataset = ProjectOfSchool.DataAccessLayer.DAC.GetData();
DataTable StudentTable = StudentTrackerDataset.Tables["Student"];
dataGridView1.DataSource = StudentTable;
//StudentTable.Columns["ID"].AutoIncrement = true;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
if (dataGridView1.Columns[i] is DataGridViewImageColumn)
{
((DataGridViewImageColumn)dataGridView1.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
break;
}
}
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
string message = string.Format("Error in {0} columan in row {1}:{2}", e.ColumnIndex, e.RowIndex, e.Exception.Message);
MessageBox.Show(message, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
private void button1_Click(object sender, EventArgs e)
{
AddModifyStudentRecords AddStudent = new AddModifyStudentRecords(StudentTrackerDataset);
AddStudent.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
object id = dataGridView1.CurrentRow.Cells["StudentID"].Value;
DataRow StudentRow = StudentTrackerDataset.Tables["Student"].Rows.Find(id);
AddModifyStudentRecords modifyStudent = new AddModifyStudentRecords(StudentTrackerDataset, StudentRow);
modifyStudent.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure", "Delete Current Row", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
object id = dataGridView1.CurrentRow.Cells["StudentID"].Value;
DataRow currentRow = StudentTrackerDataset.Tables["Student"].Rows.Find(id);
currentRow.Delete();
}
}
private void button4_Click(object sender, EventArgs e)
{
if (!StudentTrackerDataset.HasChanges())
{
MessageBox.Show("There are no Change to Save ", "Save Changes", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
try
{
DataSet changesDateSet = StudentTrackerDataset.GetChanges();
ProjectOfSchool.DataAccessLayer.DAC.SaveData(ref changesDateSet);
StudentTrackerDataset.Merge(DAC.GetData());
MessageBox.Show("Data Save Successfully.", "Save Changes");
}
catch (SqlException ex)
{
MessageBox.Show("Data Not Saved:" + ex.Message, "Save Changes", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
private void button5_Click(object sender, EventArgs e)
{
if (!StudentTrackerDataset.HasChanges())
{
MessageBox.Show("There are no Changes to Save.", "Save Changes", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
DialogResult result = MessageBox .Show ("Are you Sure?","Reject Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
StudentTrackerDataset.RejectChanges();
}
}
}
,当我终止该程序,并重新执行该程序然后我看到StudntID = 1也保存到ID = 1
数据库或当我删除StudentID = 1然后按保存更改后按保存更改时,我也终止程序并重新计算,然后当我输入StudentID = 1然后没有错误发生
而其他方式没有终止是删除记录StudentID1但当你添加t他学生记录,但不添加StudentID = 1但在此情况下添加StudentID,否则错误也不会发生。亲爱的谢里克我已经解决了我的其他错误,但我仍然有错误,这是在我的Tittle所以请回应我的这个错误,我重新编辑我的代码也请所有重播我谢谢
我有记录在我的数据库有哪些以下数据类型作为
StudentID = INT,
姓= VARCHAR,则
名字= VARCHAR,则
性别= VARCHAR,则
GPA =浮动,
MYIMAGE = Vabinary(MAX)
,或者可以是数据行无法删除记录或更新该记录。我不知道请告诉我,谢谢回复我,我正在等待您的回复。
错误消息似乎表明您正在击中主键约束。 我建议你先停止允许用户输入学生证字段。让它在任何你使用的数据库中作为自动递增键生成。在Access中,它被称为自动编号。
一旦你这样做了,重新测试并发布结果。
尊敬的尖叫我的程序是手动输入StudentID,如果我会这样做然后错误发生,如果不是然后我必须传递StudentID TextBox1.text形式AddstudentRecord,我不知道如何传递StudentID单击AddStudent窗体的Addbutton时,显示textbox1.text。并且还告诉我如何生成自动增量键 – 2012-03-14 13:29:51
这里是我的Addmodifyform逻辑,用于添加StudentID public AddModifyStudentRecords(DataSet ds,DataRow row) InitializeComponent(); StudentTrackerDataSet = ds; textBox1.Text = currentRow [“StudentID”] .ToString(); textBox2.Text = currentRow [“FirstName”]。ToString(); textBox4.Text = currentRow [“LastName”]。ToString(); textBox3.Text = currentRow [“Gender”]。ToString(); textBox5.Text = currentRow [“GPA”]。ToString(); txtBrowseFile.Text = currentRow [“MyImage”]。ToString(); byte [] data =(byte [])currentRow [“MyImage”]; MemoryStream ms = new MemoryStream(data); pictureBox1.Image = Image.FromStream(ms); – 2012-03-14 13:33:32
以及此字符串StudentID = textBox1.Text.ToString(); string StudentFirstName = textBox2.Text.ToString(); string StudentLastName = textBox4.Text.ToString(); string Gender = textBox3.Text.ToString(); string GPA = textBox5.Text.ToString(); 图片MyImage = pictureBox1.Image; DataTable table = StudentTrackerDataSet.Tables [“Student”]; if(currentRow == null) { – 2012-03-14 13:40:52