|
i am reading a CSV file and getting error that file is in use. i am trying to read CSV file by ado.net and fill the dataset.
my code is as follows :-
---------------------------
DataTable dt = null;
bool flag;
DataSet objDS = new DataSet();
OdbcConnection mobjCon = null;
try
{
string mstrConnectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + Path.GetDirectoryName(strFilePath) + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
string strQuery;
mobjCon = new OdbcConnection(mstrConnectionString.Trim());
mobjCon.Open();
strQuery = "select * from [" + Path.GetFileName(strFilePath) + "]";
OdbcDataAdapter mobjAdapter = new OdbcDataAdapter(strQuery, mobjCon);
mobjAdapter.Fill(objDS, "phone");
CreateTableInDatabase(tableName);
using (SqlBulkCopy bc = new SqlBulkCopy(Business.ProgramConnections.OrcsConnectionString))
{
bc.DestinationTableName = tableName;
bc.WriteToServer(objDS.Tables["phone"]);
bc.Close();
flag = true;
}
if (flag)
{
mobjCon.Dispose();
FetchData(tableName);
}
}
catch (Exception objEx)
{
throw objEx;
}
finally
{
if (mobjCon.State == ConnectionState.Open)
{
mobjCon.Close();
}
}
in this above way i am trying to fill dataset with data from CSV file.
i am getting error because file is opened by another application.
i want that if the file is opened by another application inspite of i want to read this CSV file and populate my dataset. so please anybody rectify my code to read the locked CSV file and populate the dataset.
if possible give me a sample code by which i can read any locked CSV file, i mean in use CSV file and populate my dataset.please help me this is urgent.
Thanks in advance
tbhattacharjee
|
|
|
|
|
Tridip Bhattacharjee wrote: want that if the file is opened by another application inspite of i want to read this CSV file and populate my dataset
If the other application has the file opened with DenyShareWrite, or any other variant that deny's you access to the file while it has it open, you're screwed. You simply have no choice but to wait for the other application to close the file before you can read it.
|
|
|
|
|
but i know that the file is not opened with DenyShareWrite mode. i could open that file with excel and could read that is why i think it can be read with readonly mode. so please guide me with sample code.
thanks
tbhattacharjee
|
|
|
|
|
If that's the case, then you've got a shot at this.
The problem is, AFAICT, there's no way to tell the Text driver to open the file with SharedRead permissions. You'd have to use the standard file operations to open the file yourself, then read it and parse each line. An example of opening the file can be found here[^].
You'll find a few CSV libraries laying around CodeProject. Here's[^] a short list.
|
|
|
|
|
Hi again, more questions.
In my project I have this service layer I am using, to simplify it could be like this:
ServiceClient myService = new ServiceClient();
SomeDataType[] result = myService.GetSomeData(...arguments...);
myService.Close();
It's 3rd party layer and lacks proper documentation so I played around with this. With some arguments the result is null . I already have my application use these results in grids and with CurrencyManager and BindingContext . The design is assuming that these results from service methods should never be null (but what if they are).
So I decided to do "helper" class which will contain all those service calls and throw exception if data is not valid instead of checking if the data is valid outside.
ServiceClient myService = new ServiceClient();
SomeDataType[] result;
try
{
result = myService.GetSomeData();
if (result == null)
{
throw new ArgumentNullException("result", "Service method GetSomeData() returned null.");
}
}
finally
{
myService.Close();
}
return result;
Because the service raises no exception in the testing case I had it return null, it just returned null, though it has to be a case of an exception because it never shouldnt. So would this be the "proper" way to make sure I get the exception when I think the underlying layer (service) should have raised it?
|
|
|
|
|
Why there is a static constructor in the following Singleton example? :
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
static Singleton()
{
}
Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
|
|
|
|
|
Well, because its a homework question and you are meant to know
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Dont spam the forum with answers that are helpless and stupid
|
|
|
|
|
you want only clueless and stupid questions then?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
erfgerfgerger wrote: Dont spam the forum with answers that are helpless and stupid
So I can spam with stuoid questions then, just not answers you don't like.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
If you read the comments in the snippet you posted, and then search based on the keywords there, you will find your answer
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
|
|
Well, that page explains the difference just fine.
With static constructor (=without beforefieldinit): the singleton instance is created exactly when the class is used for the first time.
No static constructor (=with beforefieldinit): the .NET runtime gets more freedom - it may initialize the singleton at any time before it's used, maybe even directly at the start of the program before "void Main()" gets to run.
|
|
|
|
|
|
The article links to an explanation, read it.
|
|
|
|
|
Hi,
Previously from the SCROLL EVENT I checked with the condition like;
bool scrollLeft = false ;
MINVALUE = 0;
MAXVALUE =10;
private void hScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
scrollLeft = e.Type.ToString ().ToLower () == "largeincrement" ? false : true;
}
private void hScrollBar1_ValueChanged(object sender, System.EventArgs e)
{
MINVALUE = scrollLeft == false ? MINVALUE + 10 : MINVALUE - 10;
MAXVALUE = scrollLeft == false ? MAXVALUE + 10 : MAXVALUE - 10;
}
And in the Paint function I am doing all the logics...
Suppose for <---------------------------------> Horizontal scroll bar if we click extreme right end > then the value should change.. In the value change event i am checking with the scrollLeft variable. it will move always forward not backward...
I am having the panel and plotting graph in the panel and below i am placing horizontal scroll bar...
How todo by clicking the right end of scrollbar any event?
Reg,
Subbu
|
|
|
|
|
Hi,
Right now you are trying to design your program's logic but you don't really know how a scroll bar works. In this situation I would create a small project with one purpose, which would be to display the events from a scroll bar. The knowledge gained from the test project can then then be applied to your main project to help you with your design.
A simple test project would have a main form containing
1) a horizontal scroll bar
2) a multiline textbox.
Create handlers for the scrollbar's Scroll and ValueChanged events, e.g.
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e) {
this.textBox1.AppendText(String.Format("Scroll Event {0} {1} {2} {3}" + Environment.NewLine,
e.ScrollOrientation, e.Type, e.OldValue, e.NewValue));
}
private void hScrollBar1_ValueChanged(object sender, EventArgs e) {
this.textBox1.AppendText(String.Format("ValueChanged Event {0}" + Environment.NewLine,
this.hScrollBar1.Value));
}
What you have now is a test system which displays event information in the text box whenever you move the scrollbox.
This will teach you a lot about scroll bars and will enable you to figure out what you need to do to in your main app. You will probably be suprised to see how many events a scroll bar generates!
Alan.
|
|
|
|
|
Thanks for your valuable points..
|
|
|
|
|
Hello Everyone
I need someone who can help me.I want to check the oldest file in the directory, and if that file is older than one hour then send an alert.
Here is my current work.
for (int index = 0; index < files.Length; index++)
{
string lastMTime = File.GetLastAccessTime(files[index]).ToString("ddMMyyymmss");
files[index] = lastMTime + files[index];
}
Array.Sort(files);
string oldFile = Path.GetFileName(files[0].Substring(15));
if(File.GetCreationTime(oldFile))
<br />
<br />
if(File.GetCreationTime(oldFile))
Please my code project peeps, help me out....
Thank you all in Advance
Marvel...
|
|
|
|
|
TimeSpan ts = DateTime.Now.Subtract(File.GetCreationTime(oldFile));
if (ts.TotalHours >= 1) ...
This should do the trick.
Note that you are calling GetLastAccessTime() for sorting and GetCreationTime() for checking the file age, I'm not sure this is what you want.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
For date and time handling you must use the builtin DateTime type, it gives you all the methods you need.
You can get the current time with DateTime.Now , and you can compare two date times or subtract one to another to obtain a TimeSpan value representing the difference between the two (Moreno Airoldi sowed you how to do this).
Also, you don't need all the string parsing you are doing, just use a SortedDictionary with last access time for key and file path for value.
modified on Thursday, July 30, 2009 6:21 AM
|
|
|
|
|
Hi,
two comments:
1.
File.GetLastAccessTime does not really work on most systems. By default Windows does not track file access as that may create a heavy load on the file system; you can enable it through the registry somehow but I would not recommend that.
2.
IMO LastModificationTime is much more useful than CreationTime, since LastModificationTime is about the file content, whereas CreationTime is about the empty file. So when I now copy an old fle, it normally keeps its LastModificationTime but it gets its CreationTime set to now.
BTW: The above could be different for each and every file system (NTFS, FAT, FAT32, ...)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi.
How to run C# program in network pc without install dotnet??
i Install .netframework in network pc.
|
|
|
|
|
Don't use the .Net framework in your application and you don't need to install it. If you use it, you need to install it - simples
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|