0

我做了这样的功能

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 的异常

4

6 回答 6

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;
  }`
于 2013-10-07T05:09:04.927 回答
1

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

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);
}
于 2013-10-24T04:18:26.753 回答
0

代替

 public int InsertData(CategoryPhoto catphoto)

 public string InsertData(CategoryPhoto catphoto)

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

于 2013-10-24T04:32:42.147 回答
0

The ScoapIdentity will return your primarykey value , So that datatype is int , So you need to declare to int variable(not a string variable) .

So Replace this line string ans = null; to int ans = null;

and also need to change this below line

ans = Convert.ToInt32(cmd.ExecuteScalar());
于 2013-10-24T05:09:32.993 回答
0

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

于 2013-10-07T05:08:17.660 回答
0

您应该使用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

于 2013-10-07T05:29:00.163 回答