我是面向对象编程的菜鸟,特别是C#。我正在上夜校学习C#,但我的知识水平是在猴子看猴子级别。我在这里有一些代码和一些基本问题:
public static String InsertProduct(SqlConnection cnSQL, String strProductDesc, String strProductHS, String strManufacturer)
{
SqlCommand cmdSQL;
Int32 intRetCode = 0;
String strProductKey;
if (cnSQL != null)
{
cmdSQL = new SqlCommand();
cmdSQL.Connection = cnSQL;
cmdSQL.CommandText = "uspInsertProduct";
cmdSQL.CommandType = CommandType.StoredProcedure;
cmdSQL.Parameters.Add(new SqlParameter("@ProductDesc", SqlDbType.NVarChar, 250));
cmdSQL.Parameters["@ProductDesc"].Direction = ParameterDirection.Input;
cmdSQL.Parameters["@ProductDesc"].Value = strProductDesc;
********************************
Code removed for brevity
*********************************
cmdSQL.Parameters.Add(new SqlParameter("@ProductID", SqlDbType.NVarChar, 10));
cmdSQL.Parameters["@ProductID"].Direction = ParameterDirection.Output;
try
{
cmdSQL.ExecuteNonQuery();
}
catch (Exception ex)
{
intRetCode = -1;
}
************Offending Code Start
strProductKey = cmdSQL.Parameters["@ProductID"].ToString;
************Offend Code End
cmdSQL.Parameters.Clear();
cmdSQL.Dispose();
}
return strProductKey;
}
当我有这个语句时返回什么“cmdSQL.Parameters [”@ ProductID“]。Direction = ParameterDirection.Output;”?记录号或实际密钥?
假设答案是实际的键,我如何构造变量赋值,以便将其返回到调用例程?
感谢。
答案 0 :(得分:1)
问题的第一部分:
实际返回值由存储过程uspInsertProduct
确定。您必须查看SQL以查看该存储过程的定义以找到答案。
第二部分:
调用存储过程后,必须获取输出参数的值。
try
{
cmdSQL.ExecuteNonQuery();
strProductKey = cmdSQL.Parameters["@ProductID"].Value;
}
...
return strProductKey;