我正在拥有一个包含强类型数据集的学生数据库。我有10列。问题出在性别栏目上。每当我手动输入数据网格并保存其保存罚款时。但是当我从excel文件导入数据并将其保存到数据库时,我得到了这个错误
@ p4:字符串截断:max = 4,len = 6,value ='女性'
我在dataset.xsd中将MaxLength设置为6,在数据库模式中设置为nchar(6)。以下是我的代码..
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow row = this.dataSet.StudentsDatabase.NewRow();
this.dataSet.StudentsDatabase.Rows.Add(row);
this.sbdgv.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
this.dataSet.StudentsDatabase.Rows[i]["Class"] = dt.Rows[i]["Class"];
this.dataSet.StudentsDatabase.Rows[i]["Section"] = dt.Rows[i]["Section"];
this.dataSet.StudentsDatabase.Rows[i]["Name"] = dt.Rows[i]["Name"];
this.dataSet.StudentsDatabase.Rows[i]["Gender"] = dt.Rows[i]["Gender"];
this.dataSet.StudentsDatabase.Rows[i]["Caste"] = dt.Rows[i]["Caste"];
this.dataSet.StudentsDatabase.Rows[i]["Present"] = dt.Rows[i]["Present"];
this.dataSet.StudentsDatabase.Rows[i]["Meals"] = dt.Rows[i]["Meals"];
this.dataSet.StudentsDatabase.Rows[i]["DateAdded"] = dt.Rows[i]["DateAdded"];
this.dataSet.StudentsDatabase.Rows[i]["DateDeleted"] = dt.Rows[i]["DateDeleted"];
}
其中dt是由excel填充的数据表。这是保存逻辑
private void studentsDatabaseBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.bsStudentsDatabase.EndEdit();//binding source
this.taStudentsDb.Update(this.dataSet.StudentsDatabase);
MessageBox.Show("Save Successfull.", "Success!", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
有人可以告诉我哪里出错了?
答案 0 :(得分:1)
@p4 : String truncation: max=4, len=6, value='Female'
女性的最大值是四个字符,并且您试图插入六个字符,您必须减少要插入的值的长度。
答案 1 :(得分:0)
我找到了解决方法。出于某种原因,DataSetTableAdapter.Update
无效,但使用DataSetTableAdapter.Insert
时一切正常。这是更新的保存方法,
for (int i = 0; i < this.dataSet.StudentsDatabase.Rows.Count ; i++)
{
this.taStudentsDb.Insert((byte)dataSet.StudentsDatabase.Rows[i]["Class"],
dataSet.StudentsDatabase.Rows[i]["Section"].ToString(),
dataSet.StudentsDatabase.Rows[i]["Name"].ToString(),
dataSet.StudentsDatabase.Rows[i]["Gender"].ToString(),
dataSet.StudentsDatabase.Rows[i]["Caste"].ToString(),
dataSet.StudentsDatabase.Rows[i]["Present"].ToString(),
dataSet.StudentsDatabase.Rows[i]["Meals"].ToString(),
dataSet.StudentsDatabase.Rows[i]["DateAdded"].ToString(),
dataSet.StudentsDatabase.Rows[i]["DateDeleted"].ToString() );
}
MessageBox.Show("Save Successfull.", "Success!", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
如果可能的话,我会请求mod不要关闭此线程,因为我没有得到正确的解决方案