Archive for August, 2011
Getting Request.ServerVariables from asp.net
Here is the method of getting all server variables in asp.net from Request.ServerVariables
Response.Write(“<table border=’1′>”);
for (int i = 0; i < Request.ServerVariables.Count; i++)
{
string key = Request.ServerVariables.Keys[i];
Response.Write(
string.Format(“<tr><td style=’border:1px solid;’>{0}</td><td>{1}</td>
“);
This will list all the server variables in the page in table format…
Enjoy…
How to connect sqlserver from command prompt using sqlcmd / bulk insert
Hi all,
I need to insert large amount of records into sql server. The script is ready in file. The insert query is ready but the record is around 15 Lacs.
SQL server gets time out every time I run the file from “SQL Server Management Studio”.
Then I thought to do it from command prompt (whether anything can be done using cmd) as in my previous article I have used to replace text file from command prompt.
Then I came to know that you can do everything in sql server using command prompt. The main thing is you need to go to below path in command prompt. C:\Program Files\Microsoft SQL Server\100\Tools\Binn.
You just need to use below commands to connect to sql server using cmd.
sqlcmd -S lpc:ComputerName\<instancename> -U username -P password
Remember that, -S, -U -P are all case sensitive. Now you write any query but to fire the query you need to type go and then enter.
e.g. once you have typed above command correct, you’ll see
1> in your command prompt. You can write below thing.
1> select * from tbl1 then enter and then type Go then enter… it’ll show you result.
But, I need to run the script file so to run the file use below command.
sqlcmd -S lpc:ComputerName\<instancename> -U username -P password -i <MyScript.sql>
This will run this script file and give you output in command window. Suppose you need to get output in different file then you can use below command.
sqlcmd -S lpc:ComputerName\<instancename> -U username -P password -i <MyScript.sql> -o <MyOutput.txt>
If you want to know all the things of sqlcmd then use below command. It’ll give you list of all the tags.
sqlcmd -?
Again I came into another problem. I need to insert 15 Lac record and the script will not allow to run 1000 record per query. Now I am hope less with the command prompt as well
Then I came to know about bulk insert. I have another file with .dat extension and that file contains records like below
1;xyz;test
2;abc;temp
Create one table in sql server Table1.
Create table Table1(id bigint, name varchar(50), column1 varchar(100))
Run below query for bulk insert
BULK INSERT Table1
FROM ‘C:\filename.dat’
WITH
(
FIELDTERMINATOR =’;',
ROWTERMINATOR = ‘\n’
)
And it worked very well. By this way it has inserted all 15 Lac records in just 30 sec into database.
Happy Programming
How to find and replace text in large file
Hey folks,
Back after a looooong break.
This time I would like to replace text in a file. Hmm…
You might wonder that it’s so simple, Open file press ctrl + F and replace from there. But here the file is very big in size. It’s 300 MB File.
I’ve tried different editor to replace it but I’m failed and hopeless. I thought I need to replace it one by one.
Then one of my friend told me to try command line argument through cmd and I have surfed for that and I found below article.
http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/08/how-can-i-find-and-replace-text-in-a-text-file.aspx
Here you need to create one VB Script (.vbs) file and put these text inside it. let’s say the name of the file is replace.vbs.
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close
After creating file you need to fire below command from command prompt.
cscript replace.vbs “C:\Scripts\Text.txt” “test ” “temp “
Remember, you need to fire this command from the same drive/directory where your replace.vbs is.
But In my case this trick doesn’t work because I need to replace double quote (“) into single quote(‘) and I have tried different solution but it didn’t work.
Finally I thought to change the replace.vbs file. and I have do below change in file.
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
'strOldText = Wscript.Arguments(1)
'strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, """", "'")
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close
and then I’ve run below command.
cscript replace.vbs "C:\Scripts\Text.txt"
its done the trick in just 3 sec. Hurray
…
Recent Comments