-1

皆さん、このバグは 4 時間ほど私を困惑させてきましたが、試したことはありません。

 SqlParameter id = new SqlParameter("@bookId",System.Data.SqlDbType.Int.ToString());
 id.Direction = System.Data.ParameterDirection.Output;
 cmd.Parameters.Add(id);
 cmd.ExecuteNonQuery();
 book.BookId = new int(id.Value.ToString()); // <------ERROR

エラーは次のとおりです。

int には、引数を 1 つ取るコンストラクターが含まれていません

私が試した他のことのいくつかを次に示します。

int x = id.value.ToString();
int xi = Convert.ToInt32(x);

book.BookId = x;
4

8 に答える 8

4

Since none of the answers so far have actually explained what you are doing wrong - this occurs because you are trying to call a constructor of int and pass an argument. As you can see here, there is no int constructor that accepts a string argument (or any other type, for that matter).

There are a few ways of converting a string to an integer, but the most robust (as already posted by Thomas) is to use Int32.TryParse.

于 2013-06-11T08:32:29.783 に答える
1

これを試して

 book.BookId = Convert.ToInt32(id.Value);
于 2013-06-11T08:28:43.173 に答える
1

文字列に整数が含まれていることを 100% 確信しており、それ以外の例外をスローしても問題ない場合は、次のことができます。

string id = "55";
int x = Int32.Parse(id);

それ以外の場合は、文字列が整数でない場合を管理できます。

        string id = "55";
        int x = 0;
        if(!Int32.TryParse(id, out x))
        {
            //Manage the special case here where id is not a int
        }
于 2013-06-11T08:29:38.477 に答える
0

このステートメントは正しくないようです

SqlParameter id = new SqlParameter("@bookId",System.Data.SqlDbType.Int.ToString());

IntまたはStringのいずれかに変更する必要があると思います

SqlParameter id = new SqlParameter("@bookId",System.Data.SqlDbType.Int);

発言を変えるだけ

 book.BookId = Convert.ToInt32(id.Value.ToString()); 
于 2013-06-11T08:29:08.403 に答える
0

私の観点からは、ToString() を多く使用しています。これを試して:

SqlParameter id = new SqlParameter("@bookId", System.Data.SqlDbType.Int);
id.Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(id);
cmd.ExecuteNonQuery();
Int32.TryParse(id.Value.ToString(),out book.BookId);
于 2013-06-11T08:32:14.820 に答える
0

id.Value が NULL でないことを確認する必要があります。確認したら、これは機能するはずです:

book.BookId = Convert.ToInt32(id.Value);

あるいは

book.BookId = (int) id.Value;

編集

Int32.Parse()がフォーマット例外をスローしている場合は、整数ではない何かを指定する必要があります。あなたはそうではないと自信を持っていると言いますがNULL、それは何ですか?

于 2013-06-11T08:35:53.360 に答える
0

You have to try any of this:

book.BookId = Int32.Parse(id.Value.ToString());

OR

book.BookId = (int)id.Value.ToString();

OR

book.BookId = int.Parse(id.Value.ToString());
于 2013-06-11T08:31:27.633 に答える