2017-06-24 11 views
-1

私はStackOverflowの新機能ですので、事実を正しく説明していない場合は事前にご容赦ください。 DoctorTblから行を削除したいのですが、このメッセージが表示されます:System.Data.SqlClient.SqlException: '不正な構文'、 '。' c#

''、 '。

誰かが私を助けてくれますか?


SqlDataAdapter DoctorAdapter; 
    DataTable Doctordt; 

    public DalDoctor() 
    { 
     DoctorAdapter = new SqlDataAdapter(); 
     PreperCommands(); 
    } 
    public DalDoctor(string conn) : base(conn) 
    { 
     DoctorAdapter = new SqlDataAdapter(); 
     PreperCommands(); 
    } 
    private void PreperCommands() 
    { //select command 
     DoctorAdapter.SelectCommand = Conn.CreateCommand(); 
     DoctorAdapter.SelectCommand.CommandText = "SELECT * FROM DoctorTbl"; 
     //delete command 
     DoctorAdapter.DeleteCommand = Conn.CreateCommand(); 
     DoctorAdapter.DeleteCommand.CommandText = "DELETE FROM DoctorTbl WHERE [email protected],[email protected],[email protected],[email protected],[email protected],[email protected]"; 
     DoctorAdapter.DeleteCommand.Parameters.Add("@DocFirstName", SqlDbType.NChar, 10, "DocFirstName"); 
     DoctorAdapter.DeleteCommand.Parameters.Add("@DocLastName", SqlDbType.NChar, 10, "DocLastName"); 
     DoctorAdapter.DeleteCommand.Parameters.Add("@ExpertiseId", SqlDbType.Int, 10, "ExpertiseId"); 
     DoctorAdapter.DeleteCommand.Parameters.Add("@License", SqlDbType.Int, 10, "License"); 
     DoctorAdapter.DeleteCommand.Parameters.Add("@DocTelephone", SqlDbType.Int, 10, "DocTelephone"); 
     DoctorAdapter.DeleteCommand.Parameters.Add("@DoctorId", SqlDbType.Int, 10, "DoctorId"); 
    } 
    public bool SaveToDB(DataTable dt) 
    { 
     //try 
     //{ 
      DoctorAdapter.Update(dt); 
      return true; 
     //} 
     //catch (SqlException ex) 
     //{ 
     // return false; 
     //} 

    } 

    public DataTable FillTable() 
    { 
     DataTable dt = new DataTable(); 
     DoctorAdapter.Fill(dt); 
     dt.TableName = "DoctorTbl"; 
     return dt; 
    } 
    public bool delete(DataRow rowdelete) 
    {  

     Doctordt = FillTable(); 
     foreach (DataRow dr in Doctordt.Rows) 
     { 
      if (Convert.ToInt32(dr["DoctorId"]) == Convert.ToInt32(rowdelete["DoctorId"])) 
      { 
       dr.Delete(); 
      } 
     } 
     return SaveToDB(Doctordt); 

    } 

ここで私はERORを得る:(この順序で:DoctorAdapter.Update(DT);)


public bool SaveToDB(DataTable dt) 
    { 
     try 
     { 
      DoctorAdapter.Update(dt); 
      return true; 
     } 
     catch (SqlException ex) 
     { 
      return false; 

     } 

    } 
+0

データベースのレコードを削除することができそのテーブルの主キーを知るには十分です。 DoctorIDは主キーですか? – Steve

答えて

1

あなたのエラーがWHERE句に誤った構文によって引き起こされるべきです。ここで、すべての条件は、ANDまたはORステートメントで次の条件にリンクする必要があります。

ただし、テーブルからレコードを削除するには、そのテーブルの主キーを知り、データの値を使用してデータベース内のそのレコードを検索するだけで済みます。だから、DoctorIDが主キーであると仮定すると、行

を削除するには、あなたのシナリオでSqlDataAdapterオブジェクトを使用する必要もありません、あなたはレコードを削除するにして....

public bool delete(DataRow rowdelete) 
{  
    using(SqlCommand cmd = conn.CreateCommand()) 
    { 
     cmd.CommandText = "DELETE FROM DoctorTbl WHERE DoctorID = @doctorid"; 
     cmd.Parameters.Add("@doctorid", SqlDbType.Int).Value = Convert.ToInt32(rowdelete["DoctorId"]); 
     cmd.ExecuteNonQuery(); 

     // Now fix the in memory table to remove the deleted row 
     DataTable dt = rowDelete.Table; 
     rowDelete.Delete(); 
     table.AcceptChanges(); 
    } 
} 
関連する問題