帮助我使用数据集和dataadpater插入数据 [英] help me in inserting data using dataset and dataadpater
本文介绍了帮助我使用数据集和dataadpater插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用oledbdataadapter和数据集插入一些数据..
我正在为此编写代码..该代码执行成功插入..但数据未插入数据库..该代码似乎还可以..我无法弄清楚..问题出在哪里..
谁能帮助我更正此代码..
i want to insert some data using oledbdataadapter and dataset..
i am writing code for this.. this code executes inserted sucessfully.. but the data is not inserted in the database.. the code seems ok.. i am not able to figure it out.. where is the problem..
can anyone help me to correct this code..
try
{
MAconn = new OleDbConnection();
MAconn.ConnectionString = connectionString;
MAconn.Open();
DataSet oDS = new DataSet();
string query = "SELECT * FROM info";
OleDbDataAdapter oOrdersDataAdapter = new OleDbDataAdapter(query, connectionString);
OleDbCommandBuilder oOrdersCmdBuilder = new OleDbCommandBuilder(oOrdersDataAdapter);
oOrdersDataAdapter.Fill(oDS);
DataTable pTable = oDS.Tables["Table"];
pTable.TableName = "info";
// Insert the Data
DataRow oOrderRow = oDS.Tables["info"].NewRow();
oOrderRow["name"] = textBox1.Text;
oOrderRow["cell"] = textBox2.Text;
oOrdersDataAdapter.Update(oDS, "info");
MessageBox.Show("inserted");
}catch(Exception ex)
{ MessageBox.Show("some prob"+ex);}
推荐答案
创建DataRow
后,必须将其添加到DataTable
.我放置了应对其进行修改的代码.
After you create aDataRow
you have to add it to theDataTable
.I put the code that you should modify it.
// Insert the Data
DataRow oOrderRow = oDS.Tables["info"].NewRow();
oOrderRow["name"] = textBox1.Text;
oOrderRow["cell"] = textBox2.Text;
oDs.Tables["info"].Rows.Add(oOrderRow); // This code should be added.
我希望这能解决您的问题.
I hope this will resolve your problem.
您需要向数据表中添加新创建的行,因此请在此代码行之前使用以下代码
you need to add newly created rows to data-tables so use below code just before to your this line of code
oOrdersDataAdapter.Update(oDS, "info");
看一下
take a look how
pTable.Rows.Add(oOrderRow);
oOrdersDataAdapter.Update(oDS, "info");
这篇关于帮助我使用数据集和dataadpater插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文