Showing posts with label sql server. Show all posts
Showing posts with label sql server. Show all posts

Friday, February 15, 2013

Country list in TSQL -MS SQL Server

This article just for easy reference of country list in TSQL format so you can just copy paste and get full country list in minutes:

If you like to save your long time please just spend 5 seconds for me by clicking  on an advertisement. :D


Table Structure SQL

CREATE TABLE [dbo].[Country](
    [SlNo] [int] IDENTITY(1,1) NOT NULL,
    [CountryName] [nvarchar](100) NOT NULL,
    [CountryCode] [nvarchar](10) NULL
    )

Insert SQL


INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AF','Afghanistan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AL','Albania');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('DZ','Algeria');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AS','American Samoa');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AD','Andorra');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AO','Angola');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AI','Anguilla');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AQ','Antarctica');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AG','Antigua and Barbuda');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AR','Argentina');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AM','Armenia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AW','Aruba');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AU','Australia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AT','Austria');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AZ','Azerbaijan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BS','Bahamas');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BH','Bahrain');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BD','Bangladesh');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BB','Barbados');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BY','Belarus');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BE','Belgium');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BZ','Belize');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BJ','Benin');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BM','Bermuda');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BT','Bhutan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BO','Bolivia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BA','Bosnia and Herzegovina');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BW','Botswana');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BV','Bouvet Island');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BR','Brazil');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('IO','British Indian Ocean Territory');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('VG','British Virgin Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BN','Brunei');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BG','Bulgaria');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BF','Burkina Faso');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('BI','Burundi');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KH','Cambodia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CM','Cameroon');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CA','Canada');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CV','Cape Verde');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KY','Cayman Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CF','Central African Republic');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TD','Chad');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CL','Chile');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CN','China');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CX','Christmas Island');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CC','Cocos (Keeling) Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CO','Colombia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KM','Comoros');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CG','Congo');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CD','Congo - Democratic Republic of');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CK','Cook Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CR','Costa Rica');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CI','Cote d''Ivoire');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('HR','Croatia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CU','Cuba');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CY','Cyprus');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CZ','Czech Republic');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('DK','Denmark');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('DJ','Djibouti');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('DM','Dominica');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('DO','Dominican Republic');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TP','East Timor');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('EC','Ecuador');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('EG','Egypt');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SV','El Salvador');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GQ','Equitorial Guinea');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('ER','Eritrea');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('EE','Estonia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('ET','Ethiopia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('FK','Falkland Islands (Islas Malvinas)');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('FO','Faroe Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('FJ','Fiji');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('FI','Finland');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('FR','France');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GF','French Guyana');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PF','French Polynesia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TF','French Southern and Antarctic Lands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GA','Gabon');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GM','Gambia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GZ','Gaza Strip');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GE','Georgia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('DE','Germany');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GH','Ghana');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GI','Gibraltar');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GR','Greece');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GL','Greenland');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GD','Grenada');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GP','Guadeloupe');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GU','Guam');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GT','Guatemala');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GG','Guernsey');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GN','Guinea');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GW','Guinea-Bissau');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GY','Guyana');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('HT','Haiti');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('HM','Heard Island and McDonald Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('VA','Holy See (Vatican City)');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('HN','Honduras');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('HK','Hong Kong');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('HU','Hungary');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('IS','Iceland');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('IN','India');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('ID','Indonesia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('IR','Iran');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('IQ','Iraq');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('IE','Ireland');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('IM','Isle of Man');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('IL','Israel');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('IT','Italy');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('JM','Jamaica');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('JP','Japan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('JE','Jersey');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('JO','Jordan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KZ','Kazakhstan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KE','Kenya');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KI','Kiribati');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KW','Kuwait');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KG','Kyrgyzstan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LA','Laos');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LV','Latvia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LB','Lebanon');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LS','Lesotho');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LR','Liberia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LY','Libya');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LI','Liechtenstein');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LT','Lithuania');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LU','Luxembourg');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MO','Macau');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MK','Macedonia - The Former Yugoslav Republic of');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MG','Madagascar');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MW','Malawi');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MY','Malaysia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MV','Maldives');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('ML','Mali');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MT','Malta');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MH','Marshall Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MQ','Martinique');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MR','Mauritania');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MU','Mauritius');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('YT','Mayotte');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MX','Mexico');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('FM','Micronesia - Federated States of');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MD','Moldova');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MC','Monaco');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MN','Mongolia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MS','Montserrat');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MA','Morocco');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MZ','Mozambique');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MM','Myanmar');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NA','Namibia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NR','Nauru');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NP','Nepal');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NL','Netherlands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AN','Netherlands Antilles');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NC','New Caledonia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NZ','New Zealand');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NI','Nicaragua');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NE','Niger');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NG','Nigeria');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NU','Niue');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NF','Norfolk Island');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('MP','Northern Mariana Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KP','North Korea');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('NO','Norway');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('OM','Oman');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PK','Pakistan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PW','Palau');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PA','Panama');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PG','Papua New Guinea');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PY','Paraguay');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PE','Peru');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PH','Philippines');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PN','Pitcairn Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PL','Poland');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PT','Portugal');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PR','Puerto Rico');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('QA','Qatar');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('RE','Reunion');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('RO','Romania');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('RU','Russia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('RW','Rwanda');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KN','Saint Kitts and Nevis');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LC','Saint Lucia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('VC','Saint Vincent and the Grenadines');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('WS','Samoa');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SM','San Marino');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('ST','Sao Tome and Principe');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SA','Saudi Arabia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SN','Senegal');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CS','Serbia and Montenegro');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SC','Seychelles');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SL','Sierra Leone');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SG','Singapore');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SK','Slovakia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SI','Slovenia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SB','Solomon Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SO','Somalia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('ZA','South Africa');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GS','South Georgia and the South Sandwich Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('KR','South Korea');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('ES','Spain');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('LK','Sri Lanka');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SH','St. Helena');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PM','St. Pierre and Miquelon');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SD','Sudan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SR','Suriname');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SJ','Svalbard');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SZ','Swaziland');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SE','Sweden');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('CH','Switzerland');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('SY','Syria');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TW','Taiwan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TJ','Tajikistan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TZ','Tanzania');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TH','Thailand');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TL','Timor-Leste');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TG','Togo');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TK','Tokelau');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TO','Tonga');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TT','Trinidad and Tobago');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TN','Tunisia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TR','Turkey');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TM','Turkmenistan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TC','Turks and Caicos Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('TV','Tuvalu');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('UG','Uganda');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('UA','Ukraine');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('AE','United Arab Emirates');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('GB','United Kingdom');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('US','United States');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('UM','United States Minor Outlying Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('VI','United States Virgin Islands');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('UY','Uruguay');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('UZ','Uzbekistan');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('VU','Vanuatu');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('VE','Venezuela');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('VN','Vietnam');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('WF','Wallis and Futuna');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('PS','West Bank');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('EH','Western Sahara');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('YE','Yemen');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('ZM','Zambia');
INSERT INTO [Country] ([CountryCode], [CountryName]) VALUES('ZW','Zimbabwe');

Saturday, October 20, 2012

Send File from Server to Client using C# Socket Programming 5/6



Full codes are here...

Client code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Client_Socket
{
//FILE TRANSFER USING C#.NET SOCKET - CLIENT
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("That program can transfer small file. I've test up to 850kb file");
IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
clientSock.Connect(ipEnd);


byte[] clientData = new byte[1024 * 5000];
string receivedPath = "C:/";

int receivedBytesLen = clientSock.Receive(clientData);

int fileNameLen = BitConverter.ToInt32(clientData, 0);
string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName);

BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);

bWrite.Close();
clientSock.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("File Sending fail." + ex.Message);
}

}
}
}



Send File from Server to Client using C# Socket Programming 3/6



4) Server Action: These codes are not directly related with socket programming. This is using to read and send file to client.


string fileName = "test.txt";// "Your File Name";

string filePath = @"C:\FT\";//Your File Path;

byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);


byte[] fileData = File.ReadAllBytes(filePath + fileName);

byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];

byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);


fileNameLen.CopyTo(clientData, 0);

fileNameByte.CopyTo(clientData, 4);

fileData.CopyTo(clientData, 4 + fileNameByte.Length);


These lines of code reading some particular file from local drive and string its data in byte array “clientData”. File data need to store in array with raw byte format to send these to client. With data file name size string at initial of file data. This is predefined between client and server and it needs to do, otherwise client will not get file name which is sending by server.


For my case I am using first four byte to represent file name length and form 5th byte file name is storing. So all file data will store after file name.


5) Server Action: Now file data is in byte array and it needs to send to client. The same thing is happening by using below code with the help of client socket (clientSock) object, which was created during client request acceptance.


clientSock.Send(clientData);


Basically server application task ends here for small file transfer. Remaining code has used for some decoration and socket closing related things.


5) Client Action: Now again turn comes to client and it will perform below tasks:


byte[] clientData = new byte[1024 * 5000];

string receivedPath = "C:/";


int receivedBytesLen = clientSock.Receive(clientData);


Here first two lines are just creating byte array to store server data and path is used to decide where data to be save. In my code, I am saving data in C: drive.


Last line is start receiving data from server. Whenever client socket starts receiving server data then it returns length of data which has captured in a integer variable.



Wednesday, November 21, 2007

File Transfer using C# .Net Socket Programming 2/3


Phase 3, Break communication channel and release resources: When last slice of byte data saved at server, client and server close the socket object and release all resource like file stream, socket object etc.

By that way a file transferred from client to server. But there has no way to call a client from server and send file. My example describe how to send file from client to server but can you imagine how opposite done? Yes if you able learn client to server data transfer properly then it is very easy. Try your self first, if failed then try it again and continue at least three times, then you will able to do it. But….but again failed then ask me I will help you about it :-).

For code url is:

Client Code:

[C#]

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Client_Socket
{
//FILE TRANSFER USING C#.NET SOCKET PROGRAMMING - CLIENT
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("That program can transfer small file. I've test up to 850kb file");
IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);


string fileName = "Your File Name";
string filePath = "Your File Path";
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);

byte[] fileData = File.ReadAllBytes(filePath + fileName);
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);

clientSock.Connect(ipEnd);
clientSock.Send(clientData);
Console.WriteLine("File:{0} has been sent.", fileName);
clientSock.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("File Sending fail." + ex.Message);
}

}
}
}
[Visual Basic]

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Namespace Client_Socket
'FILE TRANSFER USING C#.NET SOCKET PROGRAMMING- CLIENT
Class Program
Private Shared Sub Main(ByVal args As String())
Try
Console.WriteLine("That program can transfer small file. I've test up to 850kb file")
Dim ipAddress As IPAddress() = Dns.GetHostAddresses("localhost")
Dim ipEnd As New IPEndPoint(ipAddress(0), 5656)
Dim clientSock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)


Dim fileName As String = "Your File Name"
Dim filePath As String = "Your File Path"
Dim fileNameByte As Byte() = Encoding.ASCII.GetBytes(fileName)

Dim fileData As Byte() = File.ReadAllBytes(filePath + fileName)
Dim clientData As Byte() = New Byte(4 + fileNameByte.Length + (fileData.Length - 1)) {}
Dim fileNameLen As Byte() = BitConverter.GetBytes(fileNameByte.Length)

fileNameLen.CopyTo(clientData, 0)
fileNameByte.CopyTo(clientData, 4)
fileData.CopyTo(clientData, 4 + fileNameByte.Length)

clientSock.Connect(ipEnd)
clientSock.Send(clientData)
Console.WriteLine("File:{0} has been sent.", fileName)
clientSock.Close()
Console.ReadLine()
Catch ex As Exception
Console.WriteLine("File Sending fail." & ex.Message)
End Try

End Sub
End Class
End Namespace


Tuesday, November 20, 2007

File Transfer using C# .Net Socket Programming 3/3



Continued code...
Server Code:
[C#]

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace beginSocketServer
{
//FILE TRANSFER USING C#.NET SOCKET PROGRAMMING - SERVER
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("That program can transfer small file. I've test up to 850kb file");
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
sock.Listen(100);
Socket clientSock = sock.Accept();

byte[] clientData = new byte[1024 * 5000];
string receivedPath = "e:/";

int receivedBytesLen = clientSock.Receive(clientData);
int fileNameLen = BitConverter.ToInt32(clientData, 0);
string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName);
BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);

bWrite.Close();
clientSock.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("File Receiving fail." + ex.Message);
}
}
}
}

[Visual Basic]

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Namespace beginSocketServer
'FILE TRANSFER USING C#.NET SOCKET PROGRAMMING- SERVER
Class Program
Private Shared Sub Main(ByVal args As String())
Try
Console.WriteLine("That program can transfer small file. I've test up to 850kb file")
Dim ipEnd As New IPEndPoint(IPAddress.Any, 5656)
Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
sock.Bind(ipEnd)
sock.Listen(100)
Dim clientSock As Socket = sock.Accept()

Dim clientData As Byte() = New Byte(1024 5000 - 1) {}
Dim receivedPath As String = "e:/"

Dim receivedBytesLen As Integer = clientSock.Receive(clientData)

Dim fileNameLen As Integer = BitConverter.ToInt32(clientData, 0)
Dim fileName As String = Encoding.ASCII.GetString(clientData, 4, fileNameLen)

Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName)

Dim bWrite As New BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append))


bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen)

Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath)

bWrite.Close()
clientSock.Close()
Console.ReadLine()
Catch ex As Exception
Console.WriteLine("File Receiving fail." & ex.Message)
End Try
End Sub
End Class
End Namespace