【问题标题】:How to return a string value in a method?如何在方法中返回字符串值?
【发布时间】:2013-10-07 05:02:46
【问题描述】:

我做了一个这样的函数

public int InsertData(CategoryPhoto catphoto)
{
    string ans = null;
    SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
    cmd.CommandText = "prcCategoryPhoto";
    cmd.CommandType = CommandType.StoredProcedure;
    // cmd.Parameters.Add(new SqlParameter("@PhotoID", prdctphoto.PhotoID));
    cmd.Parameters.Add(new SqlParameter("@PhotoName", catphoto.PhotoName));
    //cmd.Parameters.Add(new SqlParameter("@LeftPhoto", prdctphoto.LeftPhoto));
    //cmd.Parameters.Add(new SqlParameter("@RightPhoto", prdctphoto.RightPhoto));
    //cmd.Parameters.Add(new SqlParameter("@BackPhoto", prdctphoto.BackPhoto));
    //cmd.Parameters.Add(new SqlParameter("@MaterialPhoto", prdctphoto.MaterialPhoto));
    cmd.Parameters.Add(new SqlParameter("@ExtName", catphoto.ExtName));
    cmd.Parameters.Add(new SqlParameter("@PhotoType", catphoto.PhotoType));
    cmd.Parameters.Add(new SqlParameter("@PhotoSize", catphoto.PhotoSize));
    cmd.Parameters.Add(new SqlParameter("@CategoryID", catphoto.CategoryID));
    ans = cmd.ExecuteScalar().ToString();
    //var result = cmd.ExecuteScalar(); 
    //ans = int.Parse(result.ToString());
    cmd.Dispose();
    DataConnection.CloseConnection();
    return ans;
}

在我的存储过程中是

create proc [dbo].[prcCategoryPhoto]
(
@PhotoName  Varchar(100),
@ExtName    Varchar(100),
@PhotoType  Varchar(100),
@PhotoSize  int,
@CategoryID varchar(20)
)
as
insert into CategoryPhoto(PhotoName,ExtName,PhotoType,PhotoSize,CategoryID) 
    values (@PhotoName,@ExtName,@PhotoType,@PhotoSize,@CategoryID)
select @@IDENTITY

在写 return ans 时出现错误 无法将字符串隐式转换为 int

在写作中

return int.Parse(ans);

它给出了 nvarchar 不能转换为 int 的异常

【问题讨论】:

  • 你能告诉我们你在ans.??中得到了什么??
  • 您的表中的身份列是什么?什么数据类型?

标签: c# asp.net sql-server sql-server-2008


【解决方案1】:

现在试试这个.... `

   public string InsertData(CategoryPhoto catphoto)
   {

    string ans = null;
    SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
    cmd.CommandText = "prcCategoryPhoto";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@PhotoName", catphoto.PhotoName));
    cmd.Parameters.Add(new SqlParameter("@ExtName", catphoto.ExtName));
    cmd.Parameters.Add(new SqlParameter("@PhotoType", catphoto.PhotoType));
    cmd.Parameters.Add(new SqlParameter("@PhotoSize", catphoto.PhotoSize));
    cmd.Parameters.Add(new SqlParameter("@CategoryID", catphoto.CategoryID));
    ans = cmd.ExecuteScalar().ToString();
    cmd.Dispose();
    DataConnection.CloseConnection();
    return ans;
  }`

【讨论】:

    【解决方案2】:

    试试下面, 首先改变你的程序,添加如下输出参数,

    create proc [dbo].[prcCategoryPhoto]
    (
    @PhotoName  Varchar(100),
    @ExtName    Varchar(100),
    @PhotoType  Varchar(100),
    @PhotoSize  int,
    @CategoryID varchar(20),
    @ID INT OUTPUT
    )
    as
    insert into CategoryPhoto(PhotoName,ExtName,PhotoType,PhotoSize,CategoryID) 
        values (@PhotoName,@ExtName,@PhotoType,@PhotoSize,@CategoryID)
    select @ID = @@IDENTITY
    

    接下来修改你的函数如下,

    public int InsertData(CategoryPhoto catphoto)
    {
        SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
        cmd.CommandText = "prcCategoryPhoto";
        cmd.CommandType = CommandType.StoredProcedure;
        // cmd.Parameters.Add(new SqlParameter("@PhotoID", prdctphoto.PhotoID));
        cmd.Parameters.Add(new SqlParameter("@PhotoName", catphoto.PhotoName));
        //cmd.Parameters.Add(new SqlParameter("@LeftPhoto", prdctphoto.LeftPhoto));
        //cmd.Parameters.Add(new SqlParameter("@RightPhoto", prdctphoto.RightPhoto));
        //cmd.Parameters.Add(new SqlParameter("@BackPhoto", prdctphoto.BackPhoto));
        //cmd.Parameters.Add(new SqlParameter("@MaterialPhoto", prdctphoto.MaterialPhoto));
        cmd.Parameters.Add(new SqlParameter("@ExtName", catphoto.ExtName));
        cmd.Parameters.Add(new SqlParameter("@PhotoType", catphoto.PhotoType));
        cmd.Parameters.Add(new SqlParameter("@PhotoSize", catphoto.PhotoSize));
        cmd.Parameters.Add(new SqlParameter("@CategoryID", catphoto.CategoryID));
    
        cmd.Parameters.Add(new SqlParameter("@ID",System.Data.SqlDbType.Int));
        cmd.Parameters["@ID"].Direction=System.Data.ParameterDirection.Output;
    
        cmd.ExecuteNonQuery();
        var ans = cmd.Parameters["@ID"].Value;
        cmd.Dispose();
        DataConnection.CloseConnection();
        return Convert.ToInt32(ans);
    }
    

    【讨论】:

    • 你有两次声明 ans 变量!@
    【解决方案3】:

    在调试器中查看您的字符串 (ans)。似乎您有一些无法转换为 int 的字符。你能把方法的返回类型从int改为string吗?

    【讨论】:

      【解决方案4】:

      您应该使用using 关键字。它确保即使出现问题(即引发异常)也会调用 dispose 方法。

      此外,@@Identity 在任何情况下都会返回一个数值(根据 MSDN),它应该可以转换为整数或 bigint。所以我的建议是:

      public Int64 InsertData(CategoryPhoto catphoto)
      {
        using (var connection = DataConnection.GetConnection)
        {
          using (var cmd = connection.CreateCommand())
          {
            cmd.CommandText = "prcCategoryPhoto";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PhotoName", catphoto.PhotoName));
            cmd.Parameters.Add(new SqlParameter("@ExtName", catphoto.ExtName));
            cmd.Parameters.Add(new SqlParameter("@PhotoType", catphoto.PhotoType));
            cmd.Parameters.Add(new SqlParameter("@PhotoSize", catphoto.PhotoSize));
            cmd.Parameters.Add(new SqlParameter("@CategoryID", catphoto.CategoryID));
            return (Int64)cmd.ExecuteScalar();
          }
        }
      }
      

      如果您的身份列是整数,您当然可以更改方法的签名以返回int

      【讨论】:

        【解决方案5】:

        替换

         public int InsertData(CategoryPhoto catphoto)
        

         public string InsertData(CategoryPhoto catphoto)
        

        并对其依赖项遵循相同的...

        【讨论】:

        • 范围标识将返回 int 值,而不是字符串值!所以 OP 是正确的,他需要更改字符串 ans = null; to int ans = null;
        【解决方案6】:

        ScoapIdentity 将返回您的主键值,因此数据类型为 int ,因此您需要声明为 int 变量(不是字符串变量)。

        所以将这一行 string ans = null; 替换为 int ans = null;

        并且还需要在下面的行中进行更改

        ans = Convert.ToInt32(cmd.ExecuteScalar());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-05
          • 2015-03-29
          • 1970-01-01
          • 2015-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多