程序在adapter.Fill(datatable)
行停止,提供以下錯誤:oleDbException未處理:未給出一個或多個所需參數的值。對於一個或多個所需參數沒有給出任何值/ SQL應該是正確的
當我複製到SQL訪問沒有給出錯誤..
的SQL:
public static List<Movie> AllMoviesSameActor(string lastName)
{
string command;
command = "SELECT movieId, name, headActor, Director, ReleaseDate, lenghtMin"
+ "FROM Movies" +
" WHERE headactor = " + lastName ;
OleDbDataAdapter adapter = new OleDbDataAdapter(command, connectionString);
DataTable datatable = new DataTable();
adapter.Fill(datatable);
List<Movie> list= new List<Movie>();
for (int i = 0; i < datatable.Rows.Count; i++)
{
Movie movie = new Movie();
movie.movieId = datatable.Rows[i].Field<int>("movieId");
movie.name = datatable.Rows[i].Field<string>("name");
movie.headActor = datatable.Rows[i].Field<string>("headActor");
movie.director = datatable.Rows[i].Field<string>("director");
movie.releaseDate = datatable.Rows[i].Field<DateTime>("releaseDate");
movie.lenghtMin = datatable.Rows[i].Field<int>("lenghtMin");
list.Add(movie);
}
return list;
}
中的代碼形式:
public FormMoviesSameActor()
{
InitializeComponent();
List<Actor> actors = Actor.AllActors();
comboBoxActeur.DataSource = actors;
comboBoxActeur.DisplayMember = "LastName";
comboBoxActeur.ValueMember = "LastName";
}
private void buttonSearchMovies_Click(object sender, EventArgs e)
{
Actor dedicatedActor = new Actor();
dedicatedActor.LastName = Convert.ToString(comboBoxActeur.SelectedValue);
listBox1.DataSource = dedicatedActor.Movies;
listBox1.DisplayMember = "AllData";
}
的Actor.Movies方法如下所述
public List<Movie> Movies
{
get { return Movie.AllMoviesSameActor(this.lastName);
}
用於提及參數化查詢。 – Brian
圍繞lastName添加字符串限定符的做法訣竅。這是我們第一學期編程課的一個學校項目,教授說這是一個不好的方法,但我們需要先學習基礎知識。 – user3127227
@ user3127227太好了。很高興它的工作。 – TyCobb