Saturday, October 12, 2024

MySQL - From .ibd file to table structure json file

Retrieving Table Structure from MySQL .ibd file on Windows.  

Note: Tested solution on MySQL version 8.1 and above.

----

You can achieve this using ibd2sdi utility tool comes with MySQL.


Step 1: Go to MySQL Bin directory and open a command line.

Example: C:\Program Files\MySQL\MySQL Server 8.1\bin 

Adjust above path based on MySQL version installed on your PC


Step 2: Copy your MySQL data directory to c:\Temp for safety reason.

That is copy C:\ProgramData\MySQL\MySQL Server 8.1\Data  directory to C:\Temp\data


Step 3: Run the below command 

C:\Program Files\MySQL\MySQL Server 8.1\bin>ibd2sdi C:\Temp\data\mydbname\mytablename.ibd

Note: Replace mydbname with your database name and mytablename with your table name.


Note: you can dump this json into a file. There are many other options you can try.

You can also generate Json file from .ibd file.
Example: C:\Program Files\MySQL\MySQL Server 8.1\bin>ibd2sdi --dump-file=C:\Temp\mytablename.json C:\Temp\data\mydatabasename\mytablename.ibd


------

Some useful links for MySQL Crash troubleshooting:

MySQL Service stopped and unable to start
  • https://docs.rackspace.com/docs/how-to-resolve-mysql-service-starting-and-stopping-unexpectedly
  • After trying a lot, I concluded it's not worth spending more time on this option.

  • https://dev.mysql.com/doc/refman/8.4/en/ibd2sdi.html
  • https://www.percona.com/blog/mysql-8-frm-drop-how-to-recover-table-ddl/
------

Lesson learned take MySQL backup on a regular basis.

Tuesday, November 14, 2023

SEO is simplified in Blazor .Net Core 8

SEO is simplified in Blazor .Net Core 8.  Simply put whatever HTML you may want to use in HeadContent Tag.  Dynamic parameters can be added as shown below.


 @page "/"

<HeadContent>
    <PageTitle>@title</PageTitle>
    <meta name="description" content="@description">
        <meta name="keywords" content="@keywords" />
        <meta name="author" content="@author" />

        <meta property="og:type" content="article" />
        <meta property="og:title" content="something_og:title" />
        <meta property="og:description" content="something_og:description" />
        <meta property="og:url" content="something_og:url" />
        <meta property="og:site_name" content="something_og:site_name" />
        <meta property="og:locale" content="en_US" />

        <meta name="twitter:card" content="summary_large_image" />
        <meta name="twitter:title" content="something_twitter:title" />
        <meta name="twitter:description" content="something_twitter:description" />
        <meta name="twitter:creator" content="something_twitter:creator" />
        <meta name="twitter:domain" content="something_twitter:domain">
        <meta name="twitter:site" content="something_twitter:site">
</HeadContent>

<h1>Hello, world!</h1>

Welcome to your new app.

    @code {
        private string description = "Description set by component";
        private string title = "Title set by component";
        private string keywords = "keyword1, keyword2, keyword3";
        private string author = "author1";
    }
  

----

Output

-------




Friday, April 20, 2018

Content-Security-Policy in ASP.NET MVC

Few good links for securing your asp.net mvc site

  1. After applying SecurityHeaders validate
  2. Content-security-policy-for-asp-net-mvc
  3. Improving security in ASP.NET MVC using custom headers
  4. Content-Security-Policy in ASP.NET MVC
  5. Adding HTTP Headers to Improve Security

Tuesday, March 28, 2017

Running Asp.net Core Web Application and Web API project on Ubuntu

Step 1: Install .Net Core on Ubuntu using this link

Step 2: On Terminal (Cntrl + Alt + T), go to /home/ directory.  Create dir.  i.e.: mkdir coredemo  If you are new to ubuntu:  /home/ directory is place where it is good to store user specific stuff.

Step 3: If you are creating

Asp.net Core web application, run command:  dotnet web

Asp.net web api, run command:  dotnet api


Step 4: dotnet restore

Step 5: dotnet run


For Testing:

  • Asp.net Core Web application, type in browser: http://localhost:5000  
  • Asp.net Web Api application, type in browser: http://localhost:5000/api/values


(Note: It is possible that port might be different for you, if that is the case change accordingly)

Things to note:  Above approach is using kestrel as web server.  In next post, I will explain how to host both web and api on nginx server.

Monday, March 27, 2017

Efficient way of performing filtering, sorting and pagination with MongoDB C#

Problem: I run into issue where pagination was bringing almost all the rows from the collection and then doing pagination.  In other words it is doing in memory pagination which is inefficient.  Recommendation:  In order to do MongoDB pagination: avoid using LINQ Query.

Solution:
With following way you can easily and efficiently do filtering, sorting and pagination on mongoDB collection.  Below code snippet will bring back only records which are required.  Example:  If pageSize is 10 it will only bring back 10 records.  This code is tested on over 12 million records and it is working efficiently.  I am open for any comment/suggestion to make it more robust.

Note:  It uses predicate way of writing lambda expression.

C# code for filtering, sorting and pagination on MongoDB Collection

//Get MongoDB Collection
var collection = database.GetCollection("Member");


//Query the collection with criteria formed as lambda expression.
var filter = Builders
                 .Filter
                 .Where
                   (
                          a => a.City == "Ahmedabad" &&
                          a.DateOfBirth < DateTime.Now.AddYears(-18)
                    );


//Build sort definition based on your need
var sort = new SortDefinitionBuilder()
                       .Descending(x => x.DateOfJoin);


//Apply Pagination and return back list of records.
var resultSet = collection
                     .Find(filter)
                     .Sort(sort)
                     .Skip(pageSize * (currPage - 1))
                     .Limit(pageSize).ToList();


Tuesday, February 28, 2017

Getting SQL Server admin access

Following commands will provide you admin access for SQL Server instance.

Step 1:  Run > Services.msc

Step 2: Stop SQL Server (Express)




Step 3: sc start mssql$sqlexpress -m -T3659 -T4010 -T4022




Step 4: sqlcmd -S [SQLInstanceName] -E
Example: sqlcmd -S ComputerName\sqlexpress -E

Step 5:
exec SP_ADDSRVROLEMEMBER 'domainname\username', 'sysadmin'
go
exit

Example:
exec SP_ADDSRVROLEMEMBER 'domainname\vxp1122', 'sysadmin'
go
exit


Step 6: sc start mssql$sqlexpress



FYI:
We also have admin access for (localdb)\v11.0 

Saturday, November 26, 2016

Installing .Net Core on Digital Ocean Hosting

Supporting .Net on Linux OS is the best thing Microsoft gifted to .Net Developers.  Linux hosting reduces cost, plus provide good stable support.

This post will walk you through how to get started by installing .Net Core on Digital Ocean Hosting.

I am assuming you already have created DigitalOcean Hosting account and are good to start by creating droplet.  Reason I have choose digital ocean is because it is cheap to get started and have freedom to scale as you grow.

Step 1:  Create droplet.























Step 2: On creation of droplet:  Digital Ocean will send you an email with details of root password.




Step 3: 
Download SSH Client Putty to login into Ubuntu from Windows







Step 4: 
Login as root.  Use root password as sent by digital ocean in email.




Till this point we have setup Ubuntu OS.  


Step 5: Now we will install .Net Core.
I am following installation instruction from this url: https://www.microsoft.com/net/core#linuxubuntu

Commands for Ubuntu 16.04
1. sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
2. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893
3. sudo apt-get update
  4. sudo apt-get install dotnet-dev-1.0.0-preview2.1-003177 

















Step 6: Initialize some code
1. mkdir hwapp
2. cd hwapp
3. dotnet new


1. dotnet restore
2. dotnet run















---------------------------------------------------------------------------------------

If you want to access file system of your droplet then login as sftp://root@IPAddress from any popular ftp client.

I am using FileZilla Ftp Client.





























If above post have truly helped you, then please use below Referral link for digital ocean.  This will help me in lowering down some of my researching cost.  Thank you!  https://m.do.co/c/8a610f2c11ab

Monday, April 04, 2016

Install Mongodb and run as a service to start automatically for windows

This blog post will explain where to download mongodb, how to install and how to configure to start service automatically.  

Step 1: Download mongodb, I am using community edition.  Follow step by step installation process to install mongodb.

After installation a directory inside C:\Program Files is created.  
Example:  In my case:  C:\Program Files\MongoDB\Server\[version]

Please note: [version] can be current version you have downloaded.  Example: 2.4

Step 2:  Move the Mongodb directory to c:\ for easy access. i.e. C:\MongoDB\Server\[version]

Step 3:  Create directories inside C:\MongoDB\Server\[version]
mkdir C:\MongoDB\Server\[version]\data
mkdir C:\MongoDB\Server\[version]\logfiles

Step 4: Create mongod.conf file in C:\MongoDB\Server\[version], Add following content in it and save the file.
# Where data files will reside
dbpath=C:\MongoDB\Server\[version]\data

# Where the log file will be stored
logpath=C:\MongoDB\Server\[version]\data\logfiles\mongo-server.log

# how verbose the server will be logging
verbose=vvvvv

Step 5:
Open command line as administrator.
Type following commands to start mongodb as service, so that you don't have to manually start mongo server each time.

C:\MongoDB\Server\[version]\bin> mongod -f C:\MongoDB\Server\[version]\mongod.conf --install

C:\MongoDB\Server\[version]\bin>net start MongoDB

And you are good to go.  Your mongodb is up and running.

-----------------

For creating database, collection and document for mongodb you can download robomongo which is nice GUI for mongodb.


That's it.  It is that easy.

--------------------

In case you are interested in installing mongodb on ubuntu then following link will be helpful.
https://www.youtube.com/watch?v=CkDd6jClqEE
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-16-04


Friday, June 05, 2015

Example of Inversion of Control and Dependency Injection

What is an IOC?  or What is Inversion of Control?

IOC is mechanism by which we can add abstraction between dependencies to make them loosely coupled code.  Abstraction is added through interface.

Now lets understand this by example.

Before Implementing IOC:

I have a class called "Black_And_White_Printer"  which has one print() method.  Print() method will print in black and white.  I also have consumer class which is dependent on Black_And_White_Printer class.


    public class consumer
    {
        public void DoWork()
        {
            Black_and_White_Printer obj = new Black_and_White_Printer();
            obj.print();
        }
    }

Notice:  Consumer class is tightly coupled with Black_And_White_Printer class.  

Now let say after few months of this implementation management comes up with ColourFull_Printer and now they want Colour print rather than black and white.


public class consumer
    {
        public void DoWork()
        {
            ColourFull_Printer obj = new ColourFull_Printer();
            obj.print();
        }
    }
Notice:  Because our Consumer class is tightly coupled with Black_And_White_Printer class, we now have to change consumer class on changing the method of print.  In other words our consumer class is dependent on mechanism of print and that is reason we have to change every single time.


Understanding Problem without IOC:
  • Tightly coupled code.  i.e. Consumer is tightly dependent on Printing mechanism.
  • Requires more maintenance.  i.e. Whenever printing mechanism change we have to change our consumer class.
  • Not good for writing unit test cases.

Now, lets perform IOC on above code.  Again:  IOC is mechanism by which we can add abstraction between dependencies to make them loosely coupled code.  Abstraction is added through interface.

so In order to remove dependency betweeen consumer class and printing mechanism add an layer of abstraction using interface.


public interface IPrinter
    {
        void print();
    }

public class Black_And_White_Printer : IPrinter
    {
        public void print()
        {
            Console.WriteLine("Black and White Print");
        }
    }

public class ColourFull_Printer : IPrinter
    {
        public void print()
        {
            Console.WriteLine("Colourfull Print");
        }
    }

public class consumer
    {
        IPrinter printer;

        public consumer(IPrinter printer)
        {
           this.printer = printer;
        }


        public void DoWork()
        {
            printer.print();
        }
    }



In above code:  We first added interface name IPrinter which contains print() method.  Now each printing mechanism is required to implement IPrinter interface.  That is our case both Black_And_White_Printer class and ColurFull_Printer class has to implement IPrinter.  

Now notice inside consumer class we introduce Constructor Dependency Injection method to perform IOC.  

Advantage after implementing IOC:
  • Loosely coupled code.  i.e. Consumer is no more depended on Printing mechanism.
  • Requires less maintenance.  i.e. Whenever printing mechanism change we are not required to change our consumer class.
  • Good for writing unit test cases.


Dependency Injection Example 

Dependency Injection is mechanism by which we can inject dependency into component.  Considering our above example concept now I will show how can we perform Dependency Injection using Unity Framework.

As per above code create a console application and add files along with code as mentioned above.
  • Interface: IPrinter
  • Class: Black_And_White_Printer
  • Class: ColourFull_Printer
  • Class: Consumer

Also add nuget package: Unity container



After its installation it will add folder App_Start and 2 files.

Now open UnityConfig.cs.  We should be maintain all dependencies in this files.

For this example:  Inside method RegisterTypes()
Replace following line:
// container.RegisterType<iproductrepository, productrepository>();
With
container.RegisterType<iprinter, black_and_white_printer>();

Now open Program.cs

using DependencyInjectionExample.App_Start;
using Microsoft.Practices.Unity;

 class Program
    {
        static void Main(string[] args)
        {
            //Start the Unity Container
            UnityWebActivator.Start();
                        
            var ioc = UnityConfig.GetConfiguredContainer();

            //Create instance of consumer class
            var client = ioc.Resolve<consumer>();
            client.DoWork();

            Console.ReadLine();
        }
    }

Output:

Note:  Above code is displaying "Black and White Print" because inside UnityConfig.cs our IPrinter interface is pointing to Black_And_White_Printer class.
container.RegisterType<iprinter, black_and_white_printer>();


Now let say we want to display colurfull print then all we have to do is change the UnityConfig.cs file to following:
Replace following line:
container.RegisterType<iprinter, black_and_white_printer>();
With
container.RegisterType<iprinter, colourfull_printer>();

After making change run the application again and the output will be:





Tuesday, June 02, 2015

Sql Server Restrict Memory usage

With growing of sql server database size and increase in page views of website, you might run in to memory crunch situation.

Please note:  You might run into memory crunch situation for number of reasons like memory leak in code, etc, but in this article we will be focusing on only one scenario that is restricting sql server memory usage.

Cause of Memory crunch situation:

By default if you have not restricted sql server memory usage, it can use upto 2147483647.
























Solution:

Restrict the memory consumption of SQL Server by changing this limit to something which is reasonably your server can handle.



































If you like commands to change the limit:

— Turn on advanced options
EXEC  sp_configure‘Show Advanced Options’,1;
GO
RECONFIGURE
;
GO

— Set max server memory = 3500MB for the server
EXEC  sp_configure‘max server memory (MB)’,3500;
GO
RECONFIGURE
;
GO

Reference:
https://sqlserverperformance.wordpress.com/2009/10/29/suggested-max-memory-settings-for-sql-server-20052008/


Special Thanks to my friend Pankaj Mishra for sharing this information.

Friday, May 29, 2015

Securing request pipeline of an ASP.NET MVC 5 Application

Good link describing how asp.net mvc request/response pipeline works.

If you are planning to make a custom security layer then this links may interest you.





Monday, April 27, 2015

Create xml file from sql server table


Following code will create xml file from sql server table.

SQL Server Table








XML Output
<myroot>
    <blogs>>/blogs>
          <blogid>2601</blogid>
          <title>Program to illustrate operator overloading from string</title>
          <contentvalue>blah blah blah</contentvalue>
          <creationdate>2013-02-18 13:57:00</creationdate>
   
     ....
     ....
     ....
     ....
     ....
</myroot>


Script to convert sql server table to xml file

declare @XmlOutput xml

set @XmlOutput =
(select BlogId, Title, ContentValue, CreationDate from Blogs
FOR XML AUTO, ROOT('MyRoot'), ELEMENTS)

select @XmlOutput

Monday, November 17, 2014

Could not locate the assembly "System.Web.Mvc,Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35,processorArchitecture=MSIL

Microsoft recent security update has resulted in compilation error.  After spending some time came across this link.  This is a known issue.

Error: Could not load file or assembly 'System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)


Cause of Error: Microsoft recent security update


Solution: Install Nuget Package Microsoft.AspNet.Mvc for all the project referencing System.Web.Mvc dll
Example: Install-Package Microsoft.AspNet.Mvc 

Sunday, November 16, 2014

Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.Start()' to access security critical method 'System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)' failed.


Error: Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.Start()' to access security critical method 'System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)' failed.


Solution: Install Nuget Package Microsoft.AspNet.WebHelpers
Example: Install-Package Microsoft.AspNet.WebHelpers


Could not load file or assembly 'WebMatrix.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)


Error: Could not load file or assembly 'WebMatrix.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Solution: Install Nuget Package Microsoft.AspNet.WebPages.Data
Example: Install-Package Microsoft.AspNet.WebPages.Data


Monday, September 08, 2014

Solution: SSD Drive not getting detected on Win 10 OS

Recently I have purchase Samsung - 850 EVO 500GB Internal Serial ATA Solid State Drive for Laptops - Black (Model: MZ-75E500B)  in order to boost the performance of my laptop.


Issue:  SSD Drive was not getting detected on Win 10 OS.

Solution:

This issue was occurring because SSD drive was new and their was no previous partition and disk was not been formatted.  Steps to overcome this problem:

  1. Open Control Panel
  2. Click on Administrative Tools
  3. Click on Computer Management
  4. Double click on Disk Management from the left pane.
  5. Select the unlabeled drive from middle pane. i.e. Select SSD Drive which will be unlabeled.
  6. Select disk from lower pane and right click > New Simple Volume...
  7. Click Next to the wizard for Size and drive letter.
  8. And then give drive a volume label name of your choice and begin format.
  9. Ones formatting is completed it will show: Healthy (Primary Partition).
  10. Now your disk drive will appear in windows explorer.


Refer to this video for more details and exact steps on how to perform: Why can I not see my new SSD or hard drive?



Issue:  Samsung Data Migration for cloning was not working on Win 10.

Error: Samsung Data Migration Cloning failed target disk has been disconnected -00001 (ffffffff)

Solution:

Samsung data migration utility CD which comes with SSD Drive hardware is still not compatible with Win 10, so you can either used different software for drive cloning or can degrade to previous version of windows on which samsung data migration utility will works just good.


Few other references you would find useful if run into any such issues:


Monday, July 07, 2014

Update Table from Another Table in SQL Server

Update Table from Another Table in SQL Server:  A simple scenario demonstrating how we can update Persons table with data from Employees table using update statement.  A simple but very effective query which can saves you from creating unnecessary cursor for updating from different tables.  By performance wise also Update statement is more efficient compared to creating update cursor.


Update DestTbl
Set DestTbl.ColumnABC = SourceTbl.ColumnXYZ
FROM    Employees SourceTbl
JOIN    Persons DestTbl
ON      DestTbl.KeyCol1 = SourceTbl.KeyCol5


Here:
SourceTbl stands for Source Table
DestTbl stands for Destination Table



Friday, July 04, 2014

C# Object to Json Object String Example

Single C# Object to Json Object String Example

Download:  https://github.com/dotnetguts/json2csharpAndcsharp2json

Install NewtonSoft.Json Nuget Package

C# Object we will be using in following example
class MemberPics
{
    public string PhotoName { get; set; }
    public bool IsModerated { get; set; }
}

Json Object String we will be using in following example
@"{'PhotoName':'mypic.jpg','IsModerated':'false'}"

Code Snippet
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace JsonPrac1
{
    class Program
    {
        static void Main(string[] args)
        {          
            string myJsonString = @"{'PhotoName':'mypic.jpg','IsModerated':'false'}";

            //From Json String to C# Object
            MemberPics cSharpObj = JsonConvert.DeserializeObject(myJsonString);         
            Console.WriteLine("Photo Name: " + cSharpObj.PhotoName + ",  IsModerated: " + cSharpObj.IsModerated);


            //From C# Object to Json String
            string jsonString = JsonConvert.SerializeObject(cSharpObj, Formatting.Indented);
            Console.WriteLine(jsonString);

            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
    }

    class MemberPics
    {
        public string PhotoName { get; set; }
        public bool IsModerated { get; set; }
    }
}


Output











C# Object Array to Json Object String Example

Install NewtonSoft.Json Nuget Package

C# Object we will be using in following example
class MemberPics
{
    public string PhotoName { get; set; }
    public bool IsModerated { get; set; }
}

Json Object String we will be using in following example
@"[
    {'PhotoName':'pic1.png','IsModerated':'true'},
    { 'PhotoName':'pic2.jpg','IsModerated':'true'}
]"

Code Snippet
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace JsonPrac1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myJsonArrayString = @"[{'PhotoName':'pic1.png','IsModerated':'true'},{ 'PhotoName':'pic2.jpg','IsModerated':'true'}]";
         
            //From Json String to C# Object          
            List cSharpObjList = JsonConvert.DeserializeObject>(myJsonArrayString);
         
            foreach (var item in cSharpObjList)
            {
                Console.WriteLine("Photo Name: " + item.PhotoName + ",  IsModerated: " + item.IsModerated);
            }
         
            //From C# Object to Json String
            string jsonArrayString = JsonConvert.SerializeObject(cSharpObjList, Formatting.Indented);
            Console.WriteLine(jsonArrayString);

            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
    }

    class MemberPics
    {
        public string PhotoName { get; set; }
        public bool IsModerated { get; set; }
    }
}

Output















Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape