2
嗨我试图插入数据到数据库使用ADO.NET.Here是我的代码。 这是我的C#代码:ExecuteNonQuery结果参数映射错误
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.
我在做什么错?
只是提示:你不需要创建一个像'createParameter'这样的方法。在'Parameters.Add()'方法中使用'new SqlParameter()'。 IMO更清晰简单.. – 2013-02-26 13:35:01