473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

BCP for bulk insert from falt file to the database in C#

Hi,
I have the followuing code that bulks inserts data in the .fmt files in a folder to the database tables. But, when I run it, the data from the files is not copied to database properly. It copies only data from one file and leaves the rest.

Is it becuase of sleep()? Pls respond. I am struggling with this problem a lot.

foreach (string file in fileList) //this has .fmt files to be bulk copied to database
{
System.Diagnostics.Process proc = null;
bool rVal = false;
try
{
Logger.WriteMessage(string.Format("Populate data in DET_H tables {0}", file), logEventCategory, (int)LogPriority.LogAll);
strSourceFileName = strparam;
strparam = "prd_pos_ej.dbo.ej_tran_det_h in ";
strparam += file;
strparam += " -f ";
strparam += m_strSourceFilePath;
strparam += "\\ejDet_h.fmt";

string sDataSource = GetValueFromConnectionString("Data Source", connectionstr);
string sUserid = GetValueFromConnectionString("User ID", connectionstr);
string sPassword = GetValueFromConnectionString("Password", connectionstr);

strparam += " -S";
strparam += sDataSource;
strparam += " -U";
strparam += sUserid;
strparam += " -P";
strparam += sPassword;

Logger.WriteMessage(string.Format("BCP command{0}", strparam.ToString()), logEventCategory, (int)LogPriority.LogReads);
proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.UseShellExecute = false;
//proc.StartInfo.WorkingDirectory = archiveFolder;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.Arguments = strparam.ToString();
proc.StartInfo.FileName = "bcp";
proc.Start();

Logger.WriteMessage(string.Format("BCP standard output:{0}", proc.StandardOutput.ReadToEnd()), logEventCategory, (int)LogPriority.LogStatus);
int ticks = Environment.TickCount;
if ((proc.HasExited == false) && (ticks + 600000 > Environment.TickCount))
{
System.Threading.Thread.Sleep(2000); //2 seconds
}
rVal = true;
}
catch (Exception ex)
{
Logger.WriteMessage(string.Format("BCP command failed {0}", ex.ToString()), logEventCategory, (int)LogPriority.LogErrors);
Logger.WriteMessage(string.Format("BCP command failed {0}", ex.ToString()), logCategory, (int)LogPriority.LogErrors);
rVal = false;
}
finally
{
if (proc != null)
{
proc.Close();
}
}
return rVal;
}

PS: works fine if only 1 file is in the folder.
Feb 16 '07 #1
3 4463
kenobewan
4,871 Expert 4TB
What does return rVal do? Return in c# breaks loops and it is always set to false at the start anyway...
Feb 18 '07 #2
What does return rVal do? Return in c# breaks loops and it is always set to false at the start anyway...
Hi,
I removed the return stmt and checked. But does not make any difference .
Note: It works fine (writes all data to the database) if I keep a break point and debug the code!!
My hunch is that the below code is blowing it off...some thing to do with making the thread sleep?
proc.Start();

Logger.WriteMessage(string.Format("BCP standard output:{0}", proc.StandardOutput.ReadToEnd()), logEventCategory, (int)LogPriority.LogStatus);
int ticks = Environment.TickCount;
if ((proc.HasExited == false) && (ticks + 600000 > Environment.TickCount))
{
System.Threading.Thread.Sleep(2000); //2 seconds
}

Thanks,
Feb 19 '07 #3
you need the WaitForExit();

ps.Start();
string _output = ps.StandardOutput.ReadToEnd();
ps.WaitForExit();
Sep 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: php newbie | last post by:
Hello, I am trying to load a simple tab-delimited data file to SQL Server. I created a format file to go with it, since the data file differs from the destination table in number of columns. ...
5
by: me | last post by:
I'm also having problems getting the bulk insert to work. I don't know anything about it except what I've gleened from BOL but I'm not seeming to get anywhere...Hopefully there is some little (or...
7
by: iqbal | last post by:
Hi all, We have an application through which we are bulk inserting rows into a view. The definition of the view is such that it selects columns from a table on a remote server. I have added the...
20
by: akej via SQLMonster.com | last post by:
Hi, i have table with 15 columns CREATE TABLE . ( PRIMARY KEY , NULL , NULL , NULL , NULL , (50) NULL , NULL
6
by: pk | last post by:
Sorry for the piece-by-piece nature of this post, I moved it from a dormant group to this one and it was 3 separate posts in the other group. Anyway... I'm trying to bulk insert a text file of...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
11
by: Ted | last post by:
OK, I tried this: USE Alert_db; BULK INSERT funds FROM 'C:\\data\\myData.dat' WITH (FIELDTERMINATOR='\t', KEEPNULLS, ROWTERMINATOR='\r\n');
2
by: Ted | last post by:
I have BULK INSERT T-SQL statements that work for all of my basic data tables except for one. Here is the problem statement (with obvious replacement of irrelevant path info): BULK INSERT...
0
by: m31hu1 | last post by:
Hi all, I'm new to this forum... I'm trying to let sql store data from a text file, but the data won't display... what am i missing here? Your help is greatly appreciated. create table <?php...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.