Inserting Records in a Database

You can use the ExecuteNonQuery() method of EDBCommand to add records to a database stored on an Advanced Server host with an INSERT command.

In the example that follows, the INSERT command is stored in the variable cmd. The values prefixed with a colon (:) are placeholders for EDBParameters that are instantiated, assigned values, and then added to the INSERT command’s parameter collection in the statements that follow. The INSERT command is executed by the ExecuteNonQuery() method of the cmdInsert object.

The example adds a new employee to the emp table:

<% @ Page Language="C#" Debug="true"%>
<% @Import Namespace="EnterpriseDB.EDBClient" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Configuration" %>

<script language="C#" runat="server" >

private void Page_Load(object sender, System.EventArgs e)
{
      string strConnectionString = ConfigurationSettings.AppSettings
      ["DB_CONN_STRING"];
      EDBConnection conn = new EDBConnection(strConnectionString);

      try
      {
        conn.Open();

        string cmd = "INSERT INTO emp(empno,ename) VALUES(:EmpNo, :EName)";

        EDBCommand cmdInsert = new EDBCommand(cmd,conn);

        cmdInsert.Parameters.Add(new EDBParameter(":EmpNo",
          EDBTypes.EDBDbType.Integer));

        cmdInsert.Parameters[0].Value = 1234;

        cmdInsert.Parameters.Add(new EDBParameter(":EName",
          EDBTypes.EDBDbType.Text));

        cmdInsert.Parameters[1].Value = "Lola";

        cmdInsert.ExecuteNonQuery();
        Response.Write("Record inserted successfully");
      }

      catch(Exception exp)
    {
              Response.Write(exp.ToString());
      }
      finally
{
              conn.Close();
      }
}
</script>

Save the sample code in a file in a web root directory named:

insertEmployee.aspx

To invoke the sample code, open a web-browser, and browse to:

http://localhost/insertEmployee.aspx