C# insertcommand插入不进去
public void DataBaseInSertFromOperateDataSet(string DataTableName, string InsertCommandString)
{
if (this.OleDbCommandString != null && this.ConnectionString != null)
{
this._ConnectionString = ConnectionString;
this._OleDbCommandString = OleDbCommandString;
MyConnection = new OleDbConnection(_ConnectionString);
MyDataAdapter = new OleDbDataAdapter(_OleDbCommandString, MyConnection);
MyDataSet = new DataSet();
MyDataAdapter.Fill(MyDataSet, DataTableName);
MyCommand = new OleDbCommand(InsertCommandString);
MyDataAdapter.InsertCommand = MyCommand;
MyDataAdapter.Update(MyDataSet, DataTableName);
//MyCommandBuilder = new OleDbCommandBuilder(MyDataAdapter);
}
else
{
MessageBox.Show("您没有设置OleDbCommandString和ConnectionString属性", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
上述代码为什么添加不进去啊
c#
dataset
insertcommand
------解决方案--------------------话说,你这个MyDataSet里面有数据吗?只有有数据且行状态被标识为“新增”的,你执行插入命令的时候才有效果哦。
------解决方案--------------------2个方法,一个是自己遍历每行数据插入,最直接。另一个就是修改行状态,有点绕路。
另外你这里没用到2个表,单表操作最好直接使用DataTable,而不是DataSet,效率可以大大提高。
var dt = new DataTable();
MyDataAdapter.Fill(dt);
foreach(DataRow dr in dt.Rows)
{
dr.SetAdded();
}
------解决方案--------------------DataRow row = MyDataSet.Tables[0].NewRow ;
添加你需要的数据
MyDataSet.Tables[0].Rows.Add(row);
然后
MyDataAdapter.Update(MyDataSet, DataTableName);