|
-
August 3rd, 2007, 11:56 AM
#1
c# program and ODBC command
Hi,
I am trying to call a stored procedure within a C# program. I get an
error [I]"The type or namespace name 'CommandType' could not be found (are you missing a using directive or an assembly reference?)",[/I] at the line where I am setting the command type as a stored procedure.
m_cmdPINInsert.CommandType = CommandType.StoredProcedure;
Can someone help what I am overlooking here.
Thanks,
Gurupot.
-
August 3rd, 2007, 12:03 PM
#2
Re: c# program and ODBC command
I am using 1.1. .Net version and SQL server.
-
August 3rd, 2007, 12:13 PM
#3
Re: c# program and ODBC command
can you show us the entire block of code making the call to the database?
do you have all the necessary libraries in your "using" statements at the top ?
using System.Data;
using System.Data.SqlClient;
etc...
hth,
mcm
rate my posts!
mcm
-
August 3rd, 2007, 12:55 PM
#4
Re: c# program and ODBC command
I had been using System.Data.Odbc but not using System.Data.
Adding it fixed the compiler issue.
However I see another problem now. Here is my code.
Code:
OdbcCommand m_cmdStoredProc = new OdbcCommand();
m_cmdStoredProc.CommandType = CommandType.StoredProcedure;
m_cmdStoredProc.CommandText = "BEGIN " + storProc + "(); END;";
m_oConn.Open();
m_cmdStoredProc.Connection = m_oConn;
try
{
m_cmdStoredProc.ExecuteNonQuery();
m_oLog.Append("\tTRACE\tSUCCESSFULLY EXECUTED STORED PROCEDURE\t");
bReturn = true;
}
catch (Exception ex)
{
m_oLog.Append("\tSQL EXCEPTION WITH STORED PROC\t" + ex.Message);
bReturn = false;
}
m_oConn.Close();
}
It throws an exception while executing the SP and gives me this error
SQL EXCEPTION WITH STORED PROC ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near 'sp_pinlock'.
where sp_pinlock is the name of my SP.
Thanks for your help.
-
August 3rd, 2007, 01:09 PM
#5
Re: c# program and ODBC command
you dont need to put BEGIN and all that. it knows if its a stored procedure
try
Code:
m_cmdStoredProc.CommandType = CommandType.StoredProcedure;
m_cmdStoredProc.CommandText = storProc;
hth,
mcm
rate my posts!
mcm
-
August 3rd, 2007, 01:36 PM
#6
Re: c# program and ODBC command
I have been doing it like that but the problem is the SP did not seem to execute completely. Basically I have a table called PIN with 23 rows in there.
My SP procedure updates a column in this pin table.
When I run the SP in Query analyser, all the pins are updated. However when I call the SP through a C# program only the first 8 pins are updated. Here is what my SP does.
Code:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_pinlock]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_pinlock]
GO
CREATE PROCEDURE sp_pinlock AS
DECLARE PIN_UPDATE CURSOR
FOR SELECT DESCR,PIN FROM PIN
OPEN PIN_UPDATE
DECLARE @@DESCRIPTION NCHAR(150)
DECLARE @@PIN CHAR(9)
FETCH NEXT FROM PIN_UPDATE INTO @@DESCRIPTION, @@PIN
DECLARE @@BEDNUM CHAR(2)
DECLARE @@BLDG CHAR(3)
DECLARE @ERRORVAL CHAR(10)
WHILE @@FETCH_STATUS = 0
BEGIN
SET @@BEDNUM = RIGHT(@@DESCRIPTION, 94)
SET @@BEDNUM = LEFT(@@BEDNUM, 2)
SET @@BLDG = RIGHT(@@DESCRIPTION,68)
SET @@BLDG = LEFT(@@BLDG, 3)
DECLARE @@GRP INT
PRINT @@BLDG + ' ' + @@BEDNUM + ' pin: ' + @@PIN
IF @@BLDG IN ('BRD', 'BKG','REC','TOC')
BEGIN
SET @@GRP = (SELECT GRP FROM SITE WITH (NOLOCK) WHERE GNAME = @@BLDG)
UPDATE PIN SET SITE = @@GRP WHERE PIN = @@PIN
PRINT 'PIN: ' + @@PIN + 'was in a BKG, BRD, REC, TOC group'
END
SET @ERRORVAL = @@ERROR
IF @ERRORVAL <> 0
BEGIN
UPDATE PIN SET site = 0 WHERE PIN = @@PIN
PRINT @@PIN + ' WAS SET TO TIMEGROUP 1, LOCATION IS: ' + @@BLDG
FETCH NEXT FROM PIN_UPDATE INTO @@DESCRIPTION, @@PIN
END
FETCH NEXT FROM PIN_UPDATE INTO @@DESCRIPTION, @@PIN
END
CLOSE PIN_UPDATE
DEALLOCATE PIN_UPDATE
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
Thanks for your help again.
-
August 3rd, 2007, 01:39 PM
#7
Re: c# program and ODBC command
how many parameters are you passing the Stored Proc from C#?
rate my posts!
mcm
-
August 3rd, 2007, 01:45 PM
#8
Re: c# program and ODBC command
None. I am just using the SP name to call it with no parameters.
-
August 3rd, 2007, 01:47 PM
#9
Re: c# program and ODBC command
ok show me your whole code block your using to call the SP Again please?
and verify with me you get No errors when you execute it?
mcm
rate my posts!
mcm
-
August 3rd, 2007, 01:48 PM
#10
Re: c# program and ODBC command
also may want to try "ExecuteUpdate()" if your stored proc is updating (i dont know if it will help though)
hth,
mcm
rate my posts!
mcm
-
August 3rd, 2007, 02:00 PM
#11
Re: c# program and ODBC command
Here is my code.
Code:
try
{
OdbcCommand m_cmdStoredProc = new OdbcCommand();
m_cmdStoredProc.CommandType = CommandType.StoredProcedure;
m_cmdStoredProc.CommandText = storProc;
m_cmdStoredProc.CommandTimeout = 0;
m_oConn.Open();
m_cmdStoredProc.Connection = m_oConn;
try
{
int count = 0;
count = m_cmdStoredProc.ExecuteUpdate();
m_oLog.Append("\tTRACE\tSUCCESSFULLY EXECUTED STORED PROCEDURE\t");
bReturn = true;
}
catch (Exception ex)
{
m_oLog.Append("\tSQL EXCEPTION WITH STORED PROC\t" + ex.Message);
bReturn = false;
}
m_oConn.Close();
}
catch(System.Data.SqlClient.SqlException e)
{
//usage log error, if we return false from this function our code should already take care of it.
m_oLog.Append("\tSQL EXCEPTION\t" + e.Message);
}
Also if I use ExecuteUpdate I am getting the following error.
'System.Data.Odbc.OdbcCommand' does not contain a definition for 'ExecuteUpdate'
-
August 3rd, 2007, 02:03 PM
#12
Re: c# program and ODBC command
swap these lines
Code:
m_oConn.Open();
m_cmdStoredProc.Connection = m_oConn;
you have to assign the connection to the command object before you open it.
also check this article out, it should help
http://support.microsoft.com/kb/310130
hth,
mcm
rate my posts!
mcm
-
August 3rd, 2007, 02:11 PM
#13
Re: c# program and ODBC command
That did not make any difference.
Actually I have some print statements in my SP. Can I write those lines to a text file so I can see it after I run the SP from my program to see what was happening.
Thanksagain.
-
August 3rd, 2007, 02:17 PM
#14
Re: c# program and ODBC command
i suppose, although i dont know how to do it.
Also what kind of database are you using? if your finding troubles with ODBC and your using a SQL Database i would suggest using SqlCommand objects instead.
it works almost exactly like ODBC just differnet classes and names.
you use
Code:
using System.Data;
using System.Data.SqlClient;
SqlConnection = ODBCConnection
SQLCommand = ODBCCommand
SqlConnection conn = new SqlConnection(etc...);
SqlCommand cmd = new SqlCommand(myStoredProc,conn);
conn.open();
cmd.ExecuteNonQuery();
conn.close();
etc...
hth,
mcm
rate my posts!
mcm
-
August 3rd, 2007, 06:32 PM
#15
Re: c# program and ODBC command
Suprisingly if I use SQL connection it works fine. Not sure what I have been missing with the ODBCConnection.
I have my DSN name as configurable and SQLConnection uses the server name and not the DSN name. Is there any way I could get the server name from the DSN I have or set up SQL connection using the DSN name.
Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|