我是SQL新手,因此需要一些帮助。我有一个简单的存储过程,该过程对select语句中的行进行计数并返回行数。我创建一个ODBC命令并向其中添加所有信息。当我调用存储过程时,出现错误。过程或函数“ CountUsers”需要未提供的参数“ @cacLogin”。当我运行它时,存储过程工作正常。我不知道问题是什么。任何帮助将不胜感激。
存储过程为:
CREATE PROCEDURE [dbo].[CountUsers]
(@cacLogin VARCHAR(100),
@rowcount INT OUTPUT)
AS
SELECT @rowcount = COUNT(*)
FROM UserInfo
WHERE strCACLogin = @cacLogin
RETURN @rowcount
GO
SQL语句为:
public void storedprocedure()
{
int i;
OdbcConnection conn = new OdbcConnection(ConfigurationManager.ConnectionStrings["dbConnect3"].ConnectionString);
OdbcCommand dbComm = new OdbcCommand();
dbComm.Connection = conn;
dbComm.CommandType = CommandType.StoredProcedure;
dbComm.CommandText = "CountUsers";
dbComm.Parameters.Add("@cacLogin", OdbcType.VarChar, 100).Value = "MAULDIN.THOMAS.C.12345";
dbComm.Connection.Open();
i = dbComm.ExecuteNonQuery();
}
答案 0 :(得分:1)
我对自己通过OCDB调用存储过程不是很有经验,但是此文档(https://support.microsoft.com/en-us/help/310130/how-to-execute-sql-parameterized-stored-procedures-by-using-the-odbc-n)建议您的示例调用存储过程的正确方法是:
127.0.0.1 - - [13/Aug/2018 17:43:29] "POST /login HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/vitorpatalano/Documents/Code/Apps/myapp/app.py", line 459, in do_admin_login
if db.session.query(Playlist).filter_by(title=d[0]).count() < 1:
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 3089, in count
return self.from_self(col).scalar()
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2843, in scalar
ret = self.one()
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2814, in one
ret = self.one_or_none()
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2784, in one_or_none
ret = list(self)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2854, in __iter__
self.session._autoflush()
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1407, in _autoflush
util.raise_from_cause(e)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1397, in _autoflush
self.flush()
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2171, in flush
self._flush(objects)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2291, in _flush
transaction.rollback(_capture_exception=True)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2255, in _flush
flush_context.execute()
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 389, in execute
rec.execute(self)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 548, in execute
uow
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 181, in save_obj
mapper, table, insert)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 835, in _emit_insert_statements
execute(statement, params)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 945, in execute
return meth(self, multiparams, params)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
compiled_sql, distilled_params
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
exc_info
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/Users/vitorpatalano/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
**IntegrityError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely) (sqlite3.IntegrityError) UNIQUE constraint failed: track.track_id [SQL: u'INSERT INTO track (title, artist, uri, track_id) VALUES (?, ?, ?, ?)'] [parameters: ('No Intention', 'Dirty Projectors', 'spotify:track:4Ozug40zUcMiE2s1neJQRb', 1)]**
127.0.0.1 - - [13/Aug/2018 17:43:29] "GET /login?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 17:43:29] "GET /login?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 17:43:29] "GET /login?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 17:43:29] "GET /login?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 17:43:29] "GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
答案 1 :(得分:0)
根据注释,直接的SQL连接应该可以正常工作。
Using SqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("dbConnect3").ConnectionString)
SqlConn.Open()
Using cmd As New SqlCommand("CountUsers", SqlConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@cacLogin", "MAULDIN.THOMAS.C.12345")
cmd.Parameters.Add("@rowcount", SqlDbType.Int).Direction = ParameterDirection.Output
cmd.ExecuteNonQuery();
End Using
End Using
很抱歉使用VB,但DB调用是等效的。
答案 2 :(得分:0)
将存储过程更改为此:
CREATE PROCEDURE [dbo].[CountUsers]
@cacLogin VARCHAR(100)
AS
BEGIN
DECLARE @rowcount INT
SELECT @rowcount = COUNT(*)
FROM UserInfo
WHERE strCACLogin = @cacLogin
RETURN @rowcount
END
以及您的C#代码:
OdbcConnection conn = new OdbcConnection(ConfigurationManager.ConnectionStrings["dbConnect3"].ConnectionString);
OdbcCommand dbComm = new OdbcCommand();
dbComm.Connection = conn;
dbComm.CommandType = CommandType.StoredProcedure;
dbComm.CommandText = "CountUsers";
dbComm.Parameters.AddWithValue("@cacLogin", "MAULDIN.THOMAS.C.12345");
var returnParameter = dbComm.Parameters.Add("@rowcount", OdbcType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
conn.Open();
dbComm.ExecuteNonQuery();
var i = returnParameter.Value;
希望对您有帮助。