简体   繁体   中英

Updating the database c#

I am trying to update my database, When I run my project and add a new line it works data is entered into the gridview but it does not update the actual databse. Any Suggestions?

The following code is entered into button1:

DataRow dr = ds.Tables["details"].NewRow();
dr["Name"] = TextBox1.Text;
dr["Price"] = TextBox2.Text;
dr["Genre"] = TextBox3.Text;
ds.Tables["details"].Rows.Add(dr);
GridView1.DataSource = ds;
GridView1.DataBind();
localhost.Service myws = new localhost.Service();

TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = ""; 

Also I am using a Web service:

public DataSet GetDetailsSet()
{
DataSet detailsSet = new DataSet();
string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/shop.accdb;Persist Security Info=True";
OleDbConnection myConn = new OleDbConnection(database);
string queryStr = "SELECT * FROM details";
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(queryStr, myConn);
myConn.Open();
myDataAdapter.Fill(detailsSet, "Details");
myConn.Close();
return detailsSet;
}

I believe I need to somehow add update in this method, any suggestions?

New row is inserted into "Details" data table, which is a local copy of original table on database server. you have to update original table using update() method. Different approaches can be used.

Read this article. How to update a database from a DataSet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM