What's wrong with this code?

P

Paul

Considering all aspects like style, naming conventions, logic etc...
Is this code OK? Please comment...

public void ReadMyData(string myConnString)
{
string mySelectQuery = "SELECT OrderID, Customer FROM Orders";

SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);

myConnection.Open();

SqlDataReader myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}

myReader.Close();
myConnection.Close();
}

Thanks,
Paul.
 
L

Liz

Paul said:
Considering all aspects like style, naming conventions, logic etc...
Is this code OK? Please comment...

public void ReadMyData(string myConnString)
{
string mySelectQuery = "SELECT OrderID, Customer FROM Orders";

SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);

myConnection.Open();

SqlDataReader myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}

myReader.Close();
myConnection.Close();
}

Thanks,
Paul.

I cut and pasted your offering and ran a tool "jcsc" on it. This tool
is supposed to see if your code matches the Sun coding standards for java.
Here is the output. Note that the NCSS = lines of code = 0, so it thinks
what you have is not valid enough to count it.
---
C:\tmp>jcsc test1.java
Encountered "void" at line 1, column 8.
Was expecting one of:
"abstract" ...
"interface" ...
"public" ...
"strictfp" ...
"final" ...
"class" ...

File: test1.java

Violations:

test1.java:1:1:interface Declaration JavaDoc does not provide the required
'@author' tag:TypeDeclarationAuthor:3
test1.java:1:1:interface Declaration JavaDoc does not provide the required
'@version' tag:TypeDeclarationVersion:3

2 violation(s) found

Metrics:


Total NCSS count : 0
Total Methods count : 0
 
S

Sudsy

Paul said:
Considering all aspects like style, naming conventions, logic etc...
Is this code OK? Please comment...
<snip>

How are we to know? There are methods you haven't included so we
don't know their signatures, specifically what exceptions can be
thrown.
Variable names are copacetic although the method names shouldn't
have a leading capital letter. I prefer the K&R style of braces
but this has always been a contentious issue.
I'm not about to enter into a religious discussion on that topic...
 
R

Roedy Green

public void ReadMyData(string myConnString)
{
string mySelectQuery = "SELECT OrderID, Customer FROM Orders";

SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);

myConnection.Open();

SqlDataReader myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}

myReader.Close();

You could have discovered some of the errors with JavaC or better
Jikes in pendant mode. See also http://mindprod.com/jgloss/lint.html

The things that stand out for me is the violation of the caps
conventions. See http://mindprod.com/jgloss/codingconventions.html

You are writing "string" for "String" which is not only a sylistic
error, but a syntax error as well.
 
C

Christophe Vanfleteren

Paul said:
Considering all aspects like style, naming conventions, logic etc...
Is this code OK? Please comment...

public void ReadMyData(string myConnString)
{
string mySelectQuery = "SELECT OrderID, Customer FROM Orders";

SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);

myConnection.Open();

SqlDataReader myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}

myReader.Close();
myConnection.Close();
}

Thanks,
Paul.

Why are you posting C# code on a Java newsgroup? They don't have the same
naming conventions anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top