您好我正在尝试使用 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.
我究竟做错了什么?