ADO命令说,我插入空,当我不 [英] ADO Command saying that I am inserting NULL, when I'm not

查看:269
本文介绍了ADO命令说,我插入空,当我不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ADO与我的Sybase服务器进行通信。

I'm using ADO to communicate with my Sybase server..

这是我如何执行一个简单的命令:

This is how I'm executing a simple command:

_ConnectionPtr m_ConnPtr;
//... Instantiate connection

_CommandPtr m_CommPtr;
m_CommPtr->CreateInstance(__uuidof(Command))
m_CommPtr->ActiveConnection = m_ConnPtr;

Variant m_variant;
m_variant.SetString("My Param Value");
_ParameterPtr ParamPtr;
ParamPtr = m_CommPtr->CreateParameter("@StrParam", (DataTypeEnum) m_variant.vt, adParamInput, NULL, m_variant);
m_CommPtr->Parameters->Append(PrmPtr);

m_CommPtr->CommandText = "EXECUTE my_stored_procedure @StrParam";

m_CommPtr->Execute(NULL, NULL, adOptionUnspecified);

@StrParam应该是一个VARCHAR类型。

@StrParam is supposed to be a VarChar type..

运行这给了我一个错误:

Running this gives me an error:

Attempt to insert NULL value into column 'StrParam'. table 'MYTABLE'; 
column does not allow nulls. Update fails.

我不知道为什么我收到这个错误,因为我specifiying它的价值(我的参数值)。

I'm not sure why I'm getting this error, since I am specifiying its value ("My Param Value")..

有谁知道我在做什么错了?

Does anyone know what I'm doing wrong?

(我并没有包括存储过程,,因为我敢肯定,没有什么不对的过程本身..使用相同的过程的其他应用程序正常工作,所以一定是有什么毛病我是如何使用parametized命令)

(I didn't include the Stored procedure,, because I'm sure there's nothing wrong with the procedure itself.. Other application using the same procedure works fine. So there must be something wrong with how I'm using the parametized command)

推荐答案

我不知道你的变式类甚至是。但传统的变量类型(VT)和ADO数据类型是没有的代名词。其次,你没有建立呼叫,也没有正确的参数为典型的存储进程内调用。

I have no clue what your Variant class even is. But the traditional variant type (vt) and the ADO data type are not synonymous. Second, you're not setting up the call nor parameters correctly for a typical stored-proc invoke.

下面是使用存储的标准PROC通话和 variant_t comutil 库你会怎么做:

Below is how you would do this using a standard stored proc call and variant_t from the comutil library:

_CommandPtr m_CommPtr(__uuidof(Command));
m_CommPtr->ActiveConnection = m_ConnPtr;
m_CommPtr->CommandType = adoCmdStoredProc;
m_CommPtr->CommandText = L"my_stored_procedure";

// setup parameter
variant_t vParam = L"My Param Value";
_ParameterPtr ParamPtr = m_CommPtr->CreateParameter(L"@StrParam", adBSTR, adParamInput, 0, vParam);
m_CommPtr->Parameters->Append(ParamPtr);
m_CommPtr->Execute(NULL, NULL, adOptionUnspecified);

注意 ParamPtr 一般是可选的,您可以直接外卖附加参数,命令的参数集合如果你不需要它为别的,像这样的:

Note that the ParamPtr is generally optional and you can straight-away append the parameter to the command's Parameters collection if you don't need it for anything else, like this:

m_CommPtr->Parameters->Append(m_CommPtr->CreateParameter(
     L"@StrParam", adBSTR, adParamInput, 0, vParam));

您正在使用的方法是很常见的是两个输入的和参数的输出,为您保留参数对象引用提取参数的输出端。我看到了你的电话,这是我说出来,这里的唯一理由没有证据证明这一点。

The method you're using is common for parameters that are both input and output, as you retain the parameter object reference to extract the output side of the parameter. I see no evidence of that in your call, which is the only reason I mention it here.

另外请注意,除非该命令返回行的结果集,你也应该调用与 adExecuteNoRecords 在执行第三个选项(这是典型的许多消防和忘记存储过程的执行)

Also note that unless the command returns rows for a result set you should also invoke with adExecuteNoRecords for the execution third option (which is typical for many fire-and-forget stored procedure executions)

最后,参数的名称是的没有的重要,除非你使用的命令对象的 NamedParameters 属性。这是常见的做,当你有默认值额外的参数,你想挽留,只设置特定参数的附加列表的一部分。

Finally, the names of the parameters are not important unless you use the NamedParameters property of the command object. This is commonly done when you have additional parameters with default values that you would like to retain, setting only specific parameters as part of your append list.

祝您好运。

这篇关于ADO命令说,我插入空,当我不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆