Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How would a loop be written in C# that uses DFirst?

Posted on 2012-12-23
12
Medium Priority
?
373 Views
Last Modified: 2012-12-26
I am developing a C# Windows platform application using VS2005.

How would you rewrite the following VBA code in C#?

VBA
-----
For intBranch = 1 To 999
tst = DFirst("[CPS Account Number]", "tblFlLNExp", "Left([CPS Account Number],3)='" & Format(intBranch, "000") & "'")
If Not IsNull(tst) Then
           strSQL = "INSERT INTO tblFlLNMod SELECT tblFlLNExp.* FROM tblFlLNExp WHERE Left([CPS Account Number],3)='" & Format(intBranch, "000") & "'"
     
C#
----    
for (int intBranch = 1; intBranch < 1000; intBranch++)
{
}
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 2
12 Comments
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 38717566
In your VBA example you are using data stored on an excel spreadsheet, for the C# where will this data be stored?

If it is still in an Excel spreadsheet why try to use to C# at all. While C# is a more powerful programming language, VBA has been developed specifically for use with MS Office and personally I believe you are better off leaving the code in VBA.

Michael
0
 

Author Comment

by:zimmer9
ID: 38717572
I don't have a choice. My company is dropping support for MS Access so my apps have to be rewritten in C#.
0
 

Author Comment

by:zimmer9
ID: 38717574
The code is based on SQL Server 2005 tables.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:zimmer9
ID: 38717613
There are no Excel files involved here.
0
 
LVL 1

Expert Comment

by:igordevelop
ID: 38718071
Hi,

The syntax for the DFirst function is:
DFirst ( expression, domain, [criteria] )

expression is the field from which you want the first value.
domain is the set of records. This can be a table or a query name.
criteria is optional. It is the WHERE clause to apply to the domain.

In other words, it is a SELECT TOP(1) clause in SQL.

In .NET, you will replace this with SqlCommand.

This is just an example of how you should go with this:

SqlConnection conn = new SqlConnection("ConnectionString");
conn.Open();

for (int intBranch = 1; intBranch < 1000; intBranch++)
{
    SqlCommand cmd = new SqlCommand("SELECT TOP(1) [CPS Account Number] FROM          
    tblFlLNExp WHERE [CPS Account Number] LIKE '%" + intBranch.ToString() + "%'", conn);
}

conn.Close();


Please let me know if anything.

Regards,
Igor
0
 

Author Comment

by:zimmer9
ID: 38719453
How would you make the comparison so that the intBranch value are 3 positions in length with leading zeroes? (for ex: 001, 002 ... 099, ...999)

Because the values in Left([CPS Account Number],3) =  001, 002, 003
0
 

Author Comment

by:zimmer9
ID: 38719472
I tried the following but it doesn't translate to the first value being 001

sql = "SELECT TOP(1) [CPS Account Number] FROM tblFlLNExp WHERE [CPS Account Number] LIKE '%" + intBranch.ToString("000") + "%'";
0
 

Author Comment

by:zimmer9
ID: 38719493
Would I need to cast it to int and keep the zeros to create a copy of it and then cast it to int, then have 2 versions of it, one as int and one as string?
0
 

Author Comment

by:zimmer9
ID: 38719503
intBranch num = 001; // 3 digit number

string sNum = num.ToString("000");

How would I incorporate the above into my loop if this is what needs to be done?
0
 
LVL 1

Accepted Solution

by:
igordevelop earned 2000 total points
ID: 38719698
Hi,

You can try by concatenating the string.

for (int intBranch = 1; intBranch < 1000; intBranch++)
{
    string branch = intBranch.ToString() + "000";    

    sql = "SELECT TOP(1) [CPS Account Number] FROM tblFlLNExp WHERE [CPS Account      
    Number] LIKE '%" + branch + "%'";
}


All this is dependable. I don't know if this should be a number with leading zeros or reverse and what type is your CPS Account Number column.

Let me know if anything.

Regards,
Igor
0
 

Author Comment

by:zimmer9
ID: 38720199
My goal is to create values from 001 to 999 and by using

intBranch.ToString() + "000";    

the values generated would appear to be 1000, 2000, 3000, 4000, ... 50000 ... 400000 ...

up to 999000
0
 

Author Comment

by:zimmer9
ID: 38720206
What I'm trying to state is that Left([CPS Account Number],3) has values
such as 001, 002, 003, 004, 005 up to 999.

So the intBranch would have to have values such as 001, 002, 003, 004, 005 up to 999
to create a match when comparing the 2 values.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question