Use the CommandText method of the SqlCommand class (to construct an insert statement for it) and ExecuteNonQuery to insert data into the table. The main program code is as follows. First, instantiate a SqlCommand object (cmd), and then use the SQL parameter (SQL statement) to create a cmd object. Finally, run the SQL statement using the ExecuteNonQuery METHOD OF THE cmd object.
Public bool insertDB (String SQL)
{
Using (SqlCommand cmd = new SqlCommand ())
{
Try
{
Cmd. CommandText = SQL; // set the SQL statement
Cmd. Connection = ConDB (); // call the method for opening the database Connection
Cmd. ExecuteNonQuery (); // Execute
Return true;
}
Catch
{
Return false;
}
}
} Click the "add information" button to create an SQL statement ). Then, the statement is passed to the insertDB method to add data.
Private void button#click (object sender, EventArgs e)
{
ClsDB. ClsDBControl DBC = new OptDB. ClsDB. ClsDBControl ();
String strSql = "insert into t_people values (" + this. textBox1.Text. Trim (). ToString () + "," +
This. textBox2.Text. Trim (). ToString () + "," + this. textBox3.Text. Trim (). ToString () + ")";
If (DBC. insertDB (strSql ))
{
MessageBox. Show ("OK ");
}
} NOTE: When adding information, the SQL reference variable format is "+ variable +"