The overall operation of the database can be divided into two categories: query (SELECT) and update (insert,delete,update). Why do you want to divide it? Take a closer look at the differences between the two categories, select just use the data from the database, while the other three will modify the physical data of the database. Capucivar in the last article, the query operation of the database has been described in detail. This article will go on to explain the update data.
Update database information First is the connection database, this capucivar in the "C # Connection database query database" has been introduced. Updates to the database require an object: OleDbCommand. This object represents the SQL statement or stored procedure to execute on the data source.
This object has three properties: 1, CommandText represents the text to set the command, 2, connection represents the connection to set the command, 3, CommandType represents the type of the set command, and the default is the SQL statement (but if the SQL statement is not executed, Be sure to specify the type of command). After the OleDbCommand object is set up, the SQL statement should be executed. The method ExecuteNonQuery () is the execution of the SQL statement. If you can't remember this method, teach you a simple notation: "ExecuteNonQuery" words are divided into three parts, that is, "do not query", that is, update the data.
The following is an example of familiarity with updating a database:
First use visual Studio2005 to make the following interface:
After the interface is done, it is equivalent to making an empty shell. The next step is to add events to the inside. We would also like to borrow the Conndb class in the previous article to add a method in the class: Update () to update the database, which has a parameter string sql.
public class Conndb
{OleDbConnection conn = null;//object to connect to the database
The following is a constructor connection database
Public Conndb ()
{if (conn==null)//To determine whether the connection is empty
{conn = new OleDbConnection ();
Conn. Connectionstring= "Provider=SQLOLEDB.1;Data source=.; Initial Catalog=capucivar;user id=sa;pwd= ";//connection to database string}
IF (Conn. state = = connectionstate.closed)
{Conn. Open ();//Opening database connection
} }
The following method is a way to find data from a database
Public DataSet query (String sql)
{DataSet ds = new DataSet ();//dataset is a collection of tables
OleDbDataAdapter da = new OleDbDataAdapter (sql,conn);//Query from database
Da. Fill (DS);//populate DataSet with data
Connclose ()//close connection
Return ds;//returns results
}
The following method is to update the database
public int update (String sql)
{OleDbCommand oc = new OleDbCommand ()//represents the SQL statement or stored procedure to be executed on the data source
Oc.commandtext = sql;//To set the text of the command
Oc.commandtype = commandtype.text;//Set the type of command
Oc. Connection = conn;//connection to set command
int X=oc. ExecuteNonQuery ()//Execute SQL statement
Connclose ()//close connection
return x; Returns a number of affected rows
}
The following Connclose () method closes the database connection
public void Connclose ()
{if (conn). state = = ConnectionState.Open)
{//To determine the connection state of the database, turn it off if the state is open
Conn. Close ();
}
}
}