简体   繁体   中英

How to return a string value in a method?

I have made a function like this

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;
}

In my stored procedure is

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

on writing return ans it is giving a error can not implicitly convert string to int

and on writing

return int.Parse(ans);

it gives exception that nvarchar cannot be converted to int

now try this.... `

   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;
  }`

try below, first alter your procedure, add a output parameter as below,

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

Next modify your function as below,

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);
}

Look at your string (ans) in debugger. Seems like you have some characters that can not be converted to int. Can you change the return type of the method from int to string?

You should be using the using keyword. It makes sure that the dispose method is called even if something goes wrong (ie and exception is raised).

Also, the @@Identity returns a numeric value in any case (according to MSDN), which should be convertible to an integer or a bigint. So my suggestion would be:

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();
    }
  }
}

In case your identity-column is an integer, you may of course change the signature of the method to return an int instead.

Replace

 public int InsertData(CategoryPhoto catphoto)

with

 public string InsertData(CategoryPhoto catphoto)

and follow the same for its dependancies...

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM