2013-12-23 54 views
1

程序在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); 
} 

回答

4

您有幾個問題。使用此:

command = "SELECT movieId, name, headActor, Director, ReleaseDate, lenghtMin" 
     + " FROM Movies" + 
     " WHERE headActor = '" + lastName + "'"; 

基本上你缺少你FROM和最後一列之間的空間。您還遺漏了lastName附近的字符串限定符。

如果您正在爲生產做某事,您不應該這樣做。你應該看看使用參數化查詢。

How do parameterized queries help against SQL injection?

正如布賴恩指出,列名也是不匹配您嘗試從數據行字段檢索什麼。您需要使用正確的名稱進行更正。

+0

用於提及參數化查詢。 – Brian

+0

圍繞lastName添加字符串限定符的做法訣竅。這是我們第一學期編程課的一個學校項目,教授說這是一個不好的方法,但我們需要先學習基礎知識。 – user3127227

+0

@ user3127227太好了。很高興它的工作。 – TyCobb

3

變化:

command = "SELECT movieId, name, headActor, Director, ReleaseDate, lenghtMin" 
      + "FROM Movies" + " WHERE headactor = " + lastName ; 

要:

command = "SELECT movieId, name, headActor, director, releaseDate, lenghtMin" 
      + " FROM Movies" + " WHERE headactor = " + lastName ; 

列名不匹配的是什麼是你的原始查詢。

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"); // Doesn't match the query 
movie.releaseDate = datatable.Rows[i].Field<DateTime>("releaseDate"); // Doesn't match the query 
movie.lenghtMin = datatable.Rows[i].Field<int>("lenghtMin"); 
list.Add(movie); 
+0

良好的漁獲一個同樣的錯誤不匹配的情況。 – TyCobb

+0

@TyCobb - 謝謝先生。 – Brian

7

當您收到此錯誤時,表示Access Engine無法正確找到一個或多個列名或整個表名。

所以,第一步是確保列的名稱是正確的。
特別是minLenght看起來不對(minLength ???)

此外字符串連接還包含一個錯誤。在FROM子句之前缺少一個空格,但這應該會引發語法錯誤,而不是缺少參數。

command = "SELECT movieId, name, headActor, Director, ReleaseDate, lenghtMin " + // space added 
      "FROM Movies WHERE headactor = " + lastName ; 

,最後的headactor場似乎是一個文本字段,如果這是真的,你需要周圍使用lastName變量單引號。但我建議使用參數化查詢來代替

command = "SELECT movieId, name, headActor, Director, ReleaseDate, lenghtMin " + 
      "FROM Movies WHERE headactor = ?"; 

OleDbDataAdapter adapter = new OleDbDataAdapter(command, connectionString); 
adapter.SelectCommand.Parameters.AddWithValue("@p1", lastName); 
..... 
0

如果lastName是一個變量,寫:

command = "SELECT movieId, name, headActor, Director, ReleaseDate, lenghtMin FROM Movies WHERE headactor = '" + LastName + '"; 
0

如果你得到了「插入」查詢您可以使用此方法避免異常的列名

string sqlQuery = "INSERT into EndResultOfTestCases(IDsOfCases,TestCaseName,ResultCase,ResultLog) VALUES(@ids, @casename, @results, @logs)"; 

     connection = new OleDbConnection(connectionStringToDB); 
     command = new OleDbCommand(sqlQuery, connection); 
     command.Parameters.AddWithValue("@ids",IDs); 
     command.Parameters.AddWithValue("@casename", CaseName); 
     command.Parameters.AddWithValue("@results", resultOfCase); 
     command.Parameters.AddWithValue("@logs", logs); 
     connection.Open(); 
     command.ExecuteNonQuery(); 
     connection.Close(); 
    } 
相關問題