简体   繁体   English

参数没有默认值,数据库c#

[英]Parameter has no default value , database c#

I'm trying to add information to my database. 我正在尝试向数据库添加信息。 I have this code 我有这个代码

private OleDbConnection connect;
private OleDbCommand command;

public DataBaseManager()
{
        connect = new OleDbConnection();
        command = new OleDbCommand();
        connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Aurelian\Desktop\Photo.accdb;
        Persist Security Info=False;";
        connect.Open();
        command.Connection = connect;
}

public void AddAlbum (Album newAlbum)
{
   command.CommandText = "INSERT INTO Album VALUES(@Id,@Name,@Description,@Location,@Data)";// + Int32.Parse(newAlbum.ID.ToString()) + ",'" + newAlbum.Name.ToString() + "','" + newAlbum.Description.ToString() + "','" + newAlbum.Location.ToString() + "'," + Convert.ToDateTime(newAlbum.Date.ToString()) + ")";

   command.Parameters.AddWithValue("@Id", newAlbum.ID);
   command.Parameters.AddWithValue("@Name", newAlbum.Name);
   command.Parameters.AddWithValue("@Description", newAlbum.Description);
   command.Parameters.AddWithValue("@Location", newAlbum.Location);
   command.Parameters.AddWithValue("@Data", newAlbum.Date);

   command.ExecuteNonQuery();
}

and the call: 并致电:

DataBaseManager dm = new DataBaseManager();
Album alb=new Album(5,"TEST","TestDescriere","TestLocatie",new DateTime(2012,12,12));
dm.AddAlbum(alb);

The problem is on this line: 问题在这条线上:

command.ExecuteNonQuery(); 

and this is the error: 这是错误:

An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code System.Data.dll中发生类型'System.Data.OleDb.OleDbException'的异常,但未在用户代码中处理

Additional information: Parameter @Name has no default value. 附加信息:参数@Name没有默认值。

请检查您是否确实在类构造函数中设置了name属性。

听起来您的newAlbum.Name为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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