使用npgsql在insert-function上出现奇怪的语法错误

时间:2012-04-21 15:03:08

标签: .net postgresql plpgsql npgsql

我有一个函数可以简单地将一些值插入表中。

CREATE OR REPLACE FUNCTION register_new_user(
   pnick character varying
 , ppasshash character varying
 , pmail character varying
 )
RETURNS void AS
$BODY$
Declare
Begin
Insert into registration
values(pNick, pPasshash, pMail);
End;
$BODY$   LANGUAGE plpgsql;

现在我在pgAdmin中使用此功能,如

Select register_new_user('a','b','c') as Answer;

它的工作正常,但是当我尝试通过我的.NET-Databasemanagement进行操作时,我从$ 2的npgsql中获得了语法错误(这就是异常所说的)。

这就是我通过VB.NET调用函数的方法:

Using func As DatabaseFunction = DatabaseFunction.CreateNewFunctionInstance("register_new_user")
    func.AddParameter("pnick", txt_Nick.Text, Data.ParameterDirection.Input)
    func.AddParameter("ppasshash", passhash, Data.ParameterDirection.Input)
    func.AddParameter("pmail", txt_Mail.Text, Data.ParameterDirection.Input)
    func.ExecuteNonQuery()
End Using

在passhash的情况下,它并不关心我在那里写什么,即使“你好”也行不通。我使用UTF8-Encoding作为passhash,并在连接字符串中说。我的.NET-Programms的数据库管理如下所示:

Public Class DatabaseFunction
            Implements IDisposable
            Public dbcon As IDbConnection
            Private comm As NpgsqlCommand
            Public trans As IDbTransaction
            Private funcname As String = String.Empty
            Private dr As IDataReader = Nothing
            Public Sub New(ByVal functionname As String, ByVal Connection As IDbConnection, ByVal Transaction As IDbTransaction)
                Try
                    dbcon = Connection
                    trans = Transaction
                    funcname = functionname
                    comm = New NpgsqlCommand(functionname, dbcon, trans)
                    comm.CommandType = CommandType.StoredProcedure
                Catch ex As Exception
                    Throw New Exception("Fehler bei der Initierung einer Datenbankfunktion: " & ex.Message)
                End Try
            End Sub

            Public Shared Function CreateNewFunctionInstance(ByVal FunctionName As String) As DatabaseFunction
                Try
                    Dim dbcon As IDbConnection = DatabaseConnection.CreateNewOpenConnection()
                    Return New DatabaseFunction(FunctionName, dbcon, dbcon.BeginTransaction)
                Catch ex As Exception
                    Throw ex
                End Try
            End Function

            Public Sub AddParameter(ByVal Name As String, ByVal parameter As Object, ByVal Direction As ParameterDirection)
                Try
                    Dim newparam As NpgsqlParameter = comm.CreateParameter()
                    newparam.ParameterName = Name
                    newparam.Value = parameter
                    newparam.Direction = Direction
                    comm.Parameters.Add(newparam)
                Catch ex As Exception
                    Throw ex
                End Try
            End Sub

            Public Function ExecuteReader() As IDataReader
                Try
                    comm.Connection = dbcon
                    comm.Transaction = trans
                    comm.Prepare()
                    dr = DirectCast(comm.ExecuteReader(), IDataReader)
                    Return dr
                Catch ex As Exception
                    Throw ex
                End Try
            End Function

            Public Sub ExecuteNonQuery()
                Try
                    comm.Connection = dbcon
                    comm.Transaction = trans
                    comm.Prepare()
                    comm.ExecuteNonQuery()
                Catch ex As Exception
                    Throw ex
                End Try
            End Sub

            Public Shared Function CreateNewDatabaseConnection() As IDbConnection
                Try
                    Return DatabaseConnection.CreateNewOpenConnection()
                Catch ex As Exception
                    Throw ex
                End Try
            End Function

            Public Sub Dispose() Implements IDisposable.Dispose
                Try
                    If Not dr Is Nothing Then
                        If Not dr.IsClosed Then
                            dr.Close()
                            dr.Dispose()
                        End If
                    End If
                    trans.Commit()
                    comm.Dispose()
                    dbcon.Close()
                    dbcon.Dispose()
                Catch ex As Exception
                    Throw ex
                End Try
            End Sub
        End Class

有人可以帮我解决这个问题,因为它让我疯狂。


我最近发现Syntaxerror是由command.prepare() - Sub引起的。当我直接执行我的东西时它工作正常。

1 个答案:

答案 0 :(得分:1)

是否可以在此处传递号码1234而不是文字'1234'(带单引号):

...
func.AddParameter("ppasshash", passhash, Data.ParameterDirection.Input)
...

如果这样做也没有解决您的问题,请查看数据库日志。使用标准配置,您将找到更详细的错误消息,包括实际发送到数据库的语句 在这种情况下,PostgreSQL还有很多话要说:

syntax error at $2

log_destination文件中查找postgresql.conf。在那里定义了记录 More about where to log in the manual here.


您可能希望简化/改进PostgreSQL功能:

CREATE OR REPLACE FUNCTION register_new_user(
  _pnick text
 ,_ppasshash text
 ,_pmail text
 )
  RETURNS void AS
$BODY$
INSERT INTO registration (pnick, ppasshash, pmail) -- assuming these col names
VALUES($1, $2, $3);
$BODY$   LANGUAGE sql;
  • 这里不需要PL / pgSQL(虽然它当然是可能的),一个普通的SQL函数可以解决问题。 (你必须在SQL函数的函数体中使用带编号的参数($ 1,$ 2,..))

  • 始终在函数中使用INSERT的目标列表。 否则,如果基础表发生变化,它将以意想不到的方式中断。

  • 通过使参数名称唯一来避免命名冲突。我喜欢使用_前缀,但这是我的任意选择。