C# : Write DataTable To CSV

Here’s a quick code to write data from database to a CSV format file in C# .NET.

I find this function really useful as I frequently come across this scenario where I have to create a report for the client.

Once our data is loaded into a DataTable, this function can be used.


public static string ToCsv(DataTable table)
{
var result = new StringBuilder();
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append(table.Columns[i].ColumnName);
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}

foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append("'"+row[i].ToString());
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
}

return result.ToString();
}

The first “foreach” loop creates table headers and the second loop writes data from rows.

Hope this will be useful to you as well 🙂

 

Visual Studio: Converting a Class Library to Portable Class Library

Before you even start asking GOOGLE about how to convert a Class Library Project to Portable Class Library (PCL) in Visual Studio, why don’t you try to do it by yourself?

In fact that is the best approach for this scenario because if you try to google for the solution, you will realize that none of the suggested answers actually agree.

@ StackOverflow:

@GeeksWithBlogs

The main reason the solutions provided do not work is that because of the difference in Visual Studio versions. The solutions vary by Visual Studio version: 2012, 2013, 2015 and now 2017.

Solution

As I said before, this will require some manual work from your side.

You will have to manually compare the differences between regular Class Library project and a Portable Class Library project.

To do this comparison, first create a new PCL project (no you do not have to move all of your class libraries to PCL project).

Once we have  a sample PCL project, we can compare the .csproj file of this project with the class library project’s .csproj file.

So, basically just add/remove the lines that are not available in you class library project from your portable class library project.

Once you have made necessary adjustments, Visual Studio will recognize the Class Library project as a Portable Class Library.

With this approach, you can also convert a Portable Class Library project back to Class Library project as well.

Good luck!

 

SQL: Get Database Name From Physical Name

Here’s a SQL Function to get name of the database from it’s physical full name.

This function can be used in conjunction with RESTORE FILELISTONLY which can generate physicalname,  the parameter of this function, from a database’s backup file.

You can read more about RESTORE FILELISTONLY here.


create function get_db_name_from_physical_name
(@physicalname varchar(500))
returns varchar
as
begin

declare @dbName as varchar(500)

select @dbName =  DB_NAME(database_id) from sys.master_files where name = @physicalname

return @dbName
end

SQL: Save Result From RESTORE FILELISTONLY

RESTORE FILELISTONLY

is a Transact Statement in MS-SQL that returns a result set containing a list of the database and log files contained in the backup set in SQL Server.

Basically it contains meta-data pertaining to the database’s:

  • LogicalName
  • PhysicalName
  • Type
  • Size
  • MaxSize
  • etc.

This information is quite useful when performing Restore operation. For example, the physical name can be used to restore an existing database to a new database using the backup of the existing database by identifying and changing it’s physical name.

Here’s a quick script to create a table and store result from RESTORE FILELISTONLY operation. This table can then be used to select desired columns only like PhysicalName and LogicalName.


IF OBJECT_ID('tempdb..#Restore') IS NOT NULL DROP TABLE #Restore

CREATE TABLE #Restore (
LogicalName NVARCHAR(128),
PhysicalName NVARCHAR(260),
[Type] CHAR(1),
FileGroupName NVARCHAR(128),
Size NUMERIC(20,0),
MaxSize NUMERIC(20,0),
FileID BIGINT,
CreateLSN NUMERIC(25,0),
DropLSN NUMERIC(25,0),
UniqueID UNIQUEIDENTIFIER,
ReadOnlyLSN NUMERIC(25,0),
ReadWriteLSN NUMERIC(25,0),
BackupSizeInBytes BIGINT,
SourceBlockSize INT,
FileGroupID INT,
LogGroupGUID UNIQUEIDENTIFIER,
DifferentialBaseLSN NUMERIC(25,0),
DifferentialBaseGUID UNIQUEIDENTIFIER,
IsReadOnly BIT,
IsPresent BIT,
TDEThumbprint VARBINARY(32)
)

INSERT INTO #Restore
RESTORE FILELISTONLY FROM DISK='' -- database backup path here

 

 

 

Becoming An Indie Game Developer From A Programmer Background

As Wikipedia defines it,

Independent video game development is the video game development process of creating indie games; these are video games, commonly created by individual or small teams of video game developers and usually without significant financial support of a video game publisher or other outside source.”

While both game development and regular software development share a few things in common, the differences weigh a lot more.

Unlike a software, a game is a mix of many other components besides regular programming logic. Graphics, sound and animation play vital roles in any game and these are not the objects of regular interactions for a software programmer. It takes years and years to become masters of these arts in themselves. Plus a game requires advanced knowledge of Math and Newtonian Physics!

So it’s not an easy switch from software development to game development.

Here’s a very good read on Quora on this topic:

https://www.quora.com/Can-one-developer-make-a-successful-indie-game

As you can read from the answers on Quora, there are some people who have “moderately” successfully managed to jump into indie game development coming from programming background.

While revenue wise, the first few years do not look as good as the regular full-time software development jobs, if someone keeps at it for a long time, there could be a brighter future.

If all goes well, as Joe Cassavaugh dreams,  maybe someday indie game developers could stop being a one-man-shop and turn their games into a game like Clash Royale that generate daily revenues of $1,992,870.

Freelance Developers Now Earn Through Writing

Since popular freelancing sites are overrated, today I would like to share with you guys one another way to generate extra income.

Freelance Writing!

Get paid to write articles and tutorials.

There are tons of sites on the internet that are craving for excellent content to be published on their sites.

Writers In Charge lists 110 such sites for freelancer writers. These sites pay anywhere between 50 to 300 Dollars per article which I believe  is way way better than many of the freelancing jobs where developers have to battle among one another to get paid in nuts like 5 and 10 dollars.

Here I would like to list some sites which are excellent platforms specifically for Developers/Designers to write and get published.


CodeTuts+

You can submit articles and tutorials about any programming language. I have seen people write about Javascript frameworks, .NET, SQL, WordPress and Game Development and many other topics.

Even I have written few articles for them!

You’ll be paid $100 for a quick tip tutorial and $250 for a regular tutorial; if you are a high profile author/educator, you could earn more.


Smashing Magazine

Based in Freiburg, Germany, Smashing Magazine works with writers from all over the world.They accept articles about web development and web design, and they would prefer a rough draft of your article if you have it already.

Again developers from many different backgrounds can write and publish their articles here. Smashing Magazine even publishes e-book where your article could get published!


Sitepoint

SitePoint pays $150 for articles and $200 for tutorials and are even willing to pay more quality posts. They focus articles mainly on HTML, CSS and Javascript technologies. But they seem to have posts on mobile development and even Entrepreneurship topics so I guess they take any good topic as their content to publish.


Developer Tutorials

You can submit articles about anything related to design, programming, marketing, management and business tips. They have a preference for list articles and prefer tutorials to be at least 1,000 words.


CodingSight

Codingsight is a community platform created and maintained by Devart.They accept articles on SQL Server, Azure, MySql and .NET topics from writers. Besides getting payments, writers also get benefit of up to 3 licenses for any Devart product: one license for a published article


So, this is just a small list of platforms where programmers, developers and designers from any background can write and get paid for their content. I am pretty sure there are many more than listed here available on the web.

Please feel free to add any other platforms that you guys know on the comments below.

SQL Pivot with Dynamic Column Names

Found a great solution to writing a complex PIVOT function in SQL on the web!

The solution was lying on a thread at one of the ASP.NET forums where the question was:

how to display row values in columns in sql server 2008

and the solution was provided by M. Atif Nadeem from Pakistan.

He has even taken time to create a SQL Fiddle as well!

Basically his solution allows to write a dynamic column name using STUFF and FOR XML functions and later using this dynamic column names to create a Dynamic SQL Pivot Statement.

How sweet !?