Posted on

auto-generate INSERT statements for a SQL Server table

  1. Right-click on the database and go to Tasks > Generate Scripts.
  2. Select the tables that you want to generate the script.
  3. Go to Set scripting options tab and click on the Advanced button.
  4. In the General category, go to Type of data to script
  5. There are 3 options: Schema OnlyData Only, and Schema and Data. Select the appropriate option and click on OK.

Posted on

WordPress on Azure: Error establishing a database connection

You have a WordPress site hosted on Microsoft Azure.Perhaps your site is part of a free or shared app service plan.

Trying to connect to the site, one day, you have this wordpress error message:  Error establishing a database connection

How to find out and solve the problem?

Connecting  to Microsoft Azure Service with FTP

First of all you have to connect to the site via ftp to understand where the error is. We can connect to Azure web application with FTP also. In the Overview section of the your Azure web application, select “Get publish profile”. It will download a file that has a lot of things and teh details about FTP username and password, which is constant and can be used for FTP connection.

Enable WordPress Debug Logging

You need FTP connection to  enable WordPress Debug Logging. To enable error logging in WordPress, you will have to make both of the following changes :

  1. In wwwroot directory, create a file named .user.ini Add the following line:log_errors=on
  2. In wwwroot directory, open wp-config.php
    Add the following lines  :

//Enable WP_DEBUG mode
define('WP_DEBUG', true);

//Enable Debug Logging to /wp-content/debug.log
define('WP_DEBUG_LOG', true);

//Supress errors and warnings to screen
define('WP_DEBUG_DISPLAY', false);

//Supress PHP errors to screen
ini_set('display_errors', 0);

Also comment the line :

    /* define('WP_DEBUG', false); */

Analyze the Log File

Try to load your site. Now, through ftp, you can enter in the directoy LogFiles and analyze the file

php_errors.log

Database error

Perhaps you have a database error, for example a duplicate key problem. On azure, in your app service, select Mysql In -app button. On the top of the section, you can see the Manage button. Click on the button and you’ll load the phpMyAdmin portal on your local Mysql DB.

Duplicate entry

If you have this kind of error : Duplicate entry ‘XXXXX’ for key ‘PRIMARY’ for the  query INSERT INTO `table_name`, just ran following command :

REPAIR TABLE table_name

That’s it! It was done.

Posted on

ASP.NET MVC : DATABASE CONNECTION

ApplicationDbContext

In the previous article we introduced the ApplicationDbContext class.

Search it in the project. You should find it within the IdentityModels.cs file. This class derives from the class that derives from IdentityDbContext DbContext.

DbContext is the connection class to a database. The class constructor has as a parameter the name of the connection string that we had previously found in the Web.config file.

public ApplicationDbContext() : base("DefaultConnection")
 {
 }

Let’s think about this information: in the Web.config you can put more connection strings, and then test the application on different databases, changing the parameter above, or you can create an application that connects to different databases by defining more than one DbContext.

Within a DbContext will be referenced all tables in the database we want to use in our application: or rather, all the tables that have constraints between them.

If for example we want to bind to the table of users created by Visual Studio, a second table created by us, the two tables should be part of the same DbContext. But we know nothing of the tables for the management of users created automatically. The class IdentityDbContext contains references to these tables. For this reason, our ApplicationDbContext not derived from DbContext but IdentityDBContext that contains, in fact, a little more information.

As you add tables for your application, you will work on ApplicationDbContext class. For a neater code, you go to create a new file in ApplicationDbContext.cs Models directory and copy, in this class, the ApplicationDbContext code (the only) which is IdentityModels.cs.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
 {
     public ApplicationDbContext()
     : base("DefaultConnection")
     {
     }

     public static ApplicationDbContext Create()
     {
       return new ApplicationDbContext();
     }
 }

Try running the application and perform a login to test that everything is working.