kevin33809
asked on
How do I capture the RowCnt in my sql statement from the ExecuteNonQuery function?
Hello
Does anyone know how to check a recordset for duplicate rows using the EXECUTENONQUERY function? I need to know the numbers of record the sql statement produces
See code below.
Declared as...............
Private Const cSQL_DuplicateRowCheck As String = _
"SELECT DB_NAME, COUNT (DB_NAME) AS ROWCNT " & _
"FROM CPR_DB_ENVIRONMENTS " & _
cWHERE & " " & _
"GROUP BY DB_NAME " & _
"HAVING COUNT(DB_NAME) > 1"
Code in app.................
sWHERE = " DB_NAME Like '%" & value & "%'"
cSQL_DupRowCk = Replace(cSQL_DuplicateRowC heck, cWHERE, cWHERE & sWHERE)
DT.SQLString = cSQL_DupRowCk
sErrMsg = DT.RunCommandSQL()
Function the sql is passed to....................
Function RunCommandSQL() As String
Try
CMDExec.CommandType = CommandType.Text
CMDExec.CommandText = _SQL
iRetn = CMDExec.ExecuteNonQuery()
If iRetn > 0 Then
CMDExec.CommandText = "COMMIT"
iRetn = CMDExec.ExecuteNonQuery()
End If
Return ""
Catch EXC As OracleClient.OracleExcepti on 'Exception
Return Str(EXC.ErrorCode) & " - " & EXC.Message
End Try
End Function
Thanks
Kevin
Does anyone know how to check a recordset for duplicate rows using the EXECUTENONQUERY function? I need to know the numbers of record the sql statement produces
See code below.
Declared as...............
Private Const cSQL_DuplicateRowCheck As String = _
"SELECT DB_NAME, COUNT (DB_NAME) AS ROWCNT " & _
"FROM CPR_DB_ENVIRONMENTS " & _
cWHERE & " " & _
"GROUP BY DB_NAME " & _
"HAVING COUNT(DB_NAME) > 1"
Code in app.................
sWHERE = " DB_NAME Like '%" & value & "%'"
cSQL_DupRowCk = Replace(cSQL_DuplicateRowC
DT.SQLString = cSQL_DupRowCk
sErrMsg = DT.RunCommandSQL()
Function the sql is passed to....................
Function RunCommandSQL() As String
Try
CMDExec.CommandType = CommandType.Text
CMDExec.CommandText = _SQL
iRetn = CMDExec.ExecuteNonQuery()
If iRetn > 0 Then
CMDExec.CommandText = "COMMIT"
iRetn = CMDExec.ExecuteNonQuery()
End If
Return ""
Catch EXC As OracleClient.OracleExcepti
Return Str(EXC.ErrorCode) & " - " & EXC.Message
End Try
End Function
Thanks
Kevin
you will need to change the select to use ExecuteScalar. That only returns the first column. Just return the count.
"SELECT COUNT (DB_NAME) AS ROWCNT " & _
...
"SELECT COUNT (DB_NAME) AS ROWCNT " & _
...
If you need both values create a reader and ExecuteReader.
Then it is a simple: reader.SetString(0) for the database name and reader.GetDecimal(1) for the count.
Then it is a simple: reader.SetString(0) for the database name and reader.GetDecimal(1) for the count.
ASKER
Could you show me an example code. I need to count for any unique record that is more than one. Do I cut down the sql by taking off the Group By and Having clauses?
ASKER
Is the code something like this code -- in C++
string query="SELECT TP_PIN FROM Demographics_tbl WHERE TP_PIN='"+UserPIN.Trim()+" '";
SqlCommand myCommand = new SqlCommand(query, myConnection);
SqlDataReader Read1 = null;
Read1 = myCommand.ExecuteReader();
myCommand.CommandText = query;
myCommand.Connection = myConnection;
Read1.Read();
if (Read1!=null)
{
if (Read1.GetString(0) == UserPIN)
{
string query="SELECT TP_PIN FROM Demographics_tbl WHERE TP_PIN='"+UserPIN.Trim()+"
SqlCommand myCommand = new SqlCommand(query, myConnection);
SqlDataReader Read1 = null;
Read1 = myCommand.ExecuteReader();
myCommand.CommandText = query;
myCommand.Connection = myConnection;
Read1.Read();
if (Read1!=null)
{
if (Read1.GetString(0) == UserPIN)
{
Yes.
What dataprovider are you using to access Oracle? There are a lot of VB.Net n the datareader but the exact usage varies a little by provider.
If you are using ODP.Net, I can provide a working example.
What dataprovider are you using to access Oracle? There are a lot of VB.Net n the datareader but the exact usage varies a little by provider.
If you are using ODP.Net, I can provide a working example.
ASKER
Private Const cConnProvider As String = "oraoledb.oracle"
ASKER
Seems to me like it a regular ADO db connection.
Private Shared InstanceBaseCn As New ADODB.Connection
Private Shared InstanceORABaseCn As New OracleClient.OracleConnect ion
Private Shared InstanceBaseCn As New ADODB.Connection
Private Shared InstanceORABaseCn As New OracleClient.OracleConnect
Ever thought about migrating to ODP.Net?
I'm not sure how the oledb driver handles it.
Below is a code snippet I have in some of my ODP.Net code. Hopefully it will run in the oleDB code.
I'm not sure how the oledb driver handles it.
Below is a code snippet I have in some of my ODP.Net code. Hopefully it will run in the oleDB code.
reader = cmd.ExecuteReader()
do while reader.read()
retStr = retstr & reader.GetString(1) & " " & reader.GetString(2) & " " & reader.GetString(3) & " " & reader.GetString(4) & "<br/>"
loop
reader.close()
reader.dispose()
ASKER
I wrote this code that begets the value of 2 for iRetn, but what is E-Scalar saying? Its counting all the records in the table, or its counting every instance where it finds a record that has a duplicate?
Dim iRetnCnt As Integer
CMDExec.CommandType = CommandType.Text
CMDExec.CommandText = _SQL
iRetnCnt = CMDExec.ExecuteScalar()
If iRetn > 0 Then
MessageBox.Show("Duplicate record!", "MESSAGE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation )
Return True
Else
Return False
End If
Dim iRetnCnt As Integer
CMDExec.CommandType = CommandType.Text
CMDExec.CommandText = _SQL
iRetnCnt = CMDExec.ExecuteScalar()
If iRetn > 0 Then
MessageBox.Show("Duplicate
Return True
Else
Return False
End If
ASKER
As far as ExecuteReader is concerned. It looks great, but I have to learn to set it up first before I use it. I'm working on it.
I don't think you can use executescalar once I re-read the question.
executescalar returns the first value of the first row. That's it.
It's perfect for things like a simple COUNT or simple select.
Since you are possible returning more than one row with your query, you need a reader or dataset so you can loop through the results.
Sorry for confusing you with scalar.
executescalar returns the first value of the first row. That's it.
It's perfect for things like a simple COUNT or simple select.
Since you are possible returning more than one row with your query, you need a reader or dataset so you can loop through the results.
Sorry for confusing you with scalar.
ASKER
I think I'm going to have to learn how to setup an ExecuteReader in Visual Basic, slightwv. If you have any setup info/code let me know. If there is someone else who can help me setup an ExecuteReader, I would appreciate you help. This is just another learning experience.
Straight VB or VB.Net?
ASKER
VB6 at this point.
Sorry can't help with that.
I'll add a few more zones to get more Experts involved.
I'll add a few more zones to get more Experts involved.
ASKER
Here's what I've done so far. I finally was able to setup an Oracle Reader in the declaration area in VB. See code below.
Dim iOraReader As OracleClient.OracleDataRea der
I then made this subroutine below that passes the sql. ;
Function RunDupCkCommandSQL() As String
Try
Dim iRetnCnt As Integer = 0
CMDExec.CommandType = CommandType.Text
CMDExec.CommandText = _SQL
iOraReader = CMDExec.ExecuteReader()
Do While iOraReader.Read()
ERRORS HERE --> Console.WriteLine(iOraRead er.GetStri ng(0)) 'JUST SHOW ME WHAT I GOT IN THE CELL.
Console.WriteLine(iOraRead er.GetStri ng(1))
Console.WriteLine(iOraRead er.GetStri ng(2))
Console.WriteLine(iOraRead er.GetStri ng(3))
Loop
iOraReader.Close()
iOraReader.Dispose()
Catch EXC As OracleClient.OracleExcepti on 'Exception
Return Str(EXC.ErrorCode) & " - " & EXC.Message
End Try
End Function
I get the following messages:
--A first chance exception of type 'System.InvalidCastExcepti on' occurred in System.Data.OracleClient.d ll
--A first chance exception of type 'System.IndexOutOfRangeExc eption' occurred in System.Data.OracleClient.d ll
--In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
At this point, I'm not sure where to go with this code to fix it. Any help out there?
Dim iOraReader As OracleClient.OracleDataRea
I then made this subroutine below that passes the sql. ;
Function RunDupCkCommandSQL() As String
Try
Dim iRetnCnt As Integer = 0
CMDExec.CommandType = CommandType.Text
CMDExec.CommandText = _SQL
iOraReader = CMDExec.ExecuteReader()
Do While iOraReader.Read()
ERRORS HERE --> Console.WriteLine(iOraRead
Console.WriteLine(iOraRead
Console.WriteLine(iOraRead
Console.WriteLine(iOraRead
Loop
iOraReader.Close()
iOraReader.Dispose()
Catch EXC As OracleClient.OracleExcepti
Return Str(EXC.ErrorCode) & " - " & EXC.Message
End Try
End Function
I get the following messages:
--A first chance exception of type 'System.InvalidCastExcepti
--A first chance exception of type 'System.IndexOutOfRangeExc
--In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
At this point, I'm not sure where to go with this code to fix it. Any help out there?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It worked
Checking for duplicates is a different question.