简体   繁体   中英

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

Additional information: Parameter @Name has no default value.

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

听起来您的newAlbum.Name为null。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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