2

こんにちは、ADO.NET を使用してデータベースにデータを挿入しようとしています。コードは次のとおりです。これは私のC#コードです:

 public void CreateBook(FormCollection collection)
        {
            string name = collection["BookName"];
            string author = collection["Author"];
            string description = collection["Description"];
            int category = int.Parse(collection["category"]);
            DateTime currentDate = new DateTime();

            using (SqlConnection connection = new SqlConnection(connectionString)) {
                connection.Open();
                SqlCommand command = new SqlCommand("AddBook", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(createParameter(name, "@Name"));
                command.Parameters.Add(createParameter(author, "@Author"));
                command.Parameters.Add(createParameter(description, "@Description"));
                command.Parameters.Add(createParameter(currentDate.Date, "@Date"));
                command.Parameters.Add(createParameter(category, "@CategoryId"));
                command.ExecuteNonQuery();
            }
        }
        //there are 2 other overloads of this method
        private SqlParameter createParameter(string parameter , string parameterName) {
            SqlParameter param = new SqlParameter();
            param.ParameterName = parameterName;
            param.Value = param;
            return param;
        }

そして、これは私のSQLコードです:

 CREATE PROCEDURE [dbo].[AddBook]
    @Name nvarchar(MAX),
    @Author nvarchar(MAX),
    @Description nvarchar(MAX),
    @Date date  ,
    @CategoryId int
 AS
    INSERT INTO Books (Name , Author , Description , PublicationDate , CategoryId)
    VALUES (@Name , @Author , @Description , @Date , @CategoryId)

ExecuteNonQueryuery が発生すると、次のエラーが発生します。

No mapping exists from object type System.Data.SqlClient.SqlParameter to a known managed provider native type. 

私は何を間違っていますか?

4

2 に答える 2

5

この行が最も差し迫った問題だと思います:

param.Value = param;

あなたが意味したのは:

param.Value = parameter;

それ以外の場合は、パラメーター値をパラメーター自体に設定しようとしていますが、これは無意味です。

ただし、個人的にはタイプなども指定することをお勧めします。他のメソッドを削除して、次のような呼び出しを使用できます。

command.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = currentDate.Date;
// etc
于 2013-02-26T13:34:35.700 に答える
2

実際のパラメーターをパラメーターに割り当てています-param.Value = param;

于 2013-02-26T13:35:41.410 に答える