Execuate insert command against MySQL database : MySQL « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e) {
        string ConnectionString = ConfigurationSettings.AppSettings["MySQLConnectString"];
        OdbcConnection myConnection = new OdbcConnection(ConnectionString);
    
        try{
            string CommandText = "INSERT Publisher (PublisherName, PublisherCity, PublisherContact_Email, PublisherWebsite) VALUES ('New Publisher', 'a', 'a@a.com', 'http://www.a.com')";
            OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
    
            myConnection.Open();
    
            lblRecords.Text = Convert.ToString(myCommand.ExecuteNonQuery());
        } catch (Exception ex){
            throw(ex);
        } finally{
            myConnection.Close();
        }
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        Records affected: <asp:Label id="lblRecords" runat="server"></asp:Label>
    </form>
</body>
</html>

File: Web.config

<configuration>
    <appSettings>
        <add key="MySQLConnectString" value="driver={MySQL ODBC 3.51 Driver};server=localhost;database=Books;uid=yourID;pwd=letmein;" />
    </appSettings>
</configuration>








18.40.MySQL
18.40.1.Execute Delete command against MySQL database with OdbcCommand (C#)
18.40.2.Execuate insert command against MySQL database
18.40.3.Read scalar data from MySQL database by using OdbcCommand
18.40.4.Execuate select statement against MySQL database with OdbcCommand
18.40.5.Execute update command against MySQL database with OdbcCommand