简体   繁体   English

System.Data.SqlClient.SqlException:','附近的语法不正确。 C#

[英]System.Data.SqlClient.SqlException: 'Incorrect syntax near ','.' c#

I'm new to StackOverflow and , so sorry in advance if I am not explaining things correctly. 我是StackOverflow和的新手,所以如果我不能正确解释,请提前抱歉。 i want to delete row from DoctorTbl but i get this message: 我想从DoctorTbl中删除行,但收到​​以下消息:

'Incorrect syntax near ','.' “','附近的语法不正确。”

can someone help me please?? 有人能帮助我吗??


    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 DocFirstName=@DocFirstName,DocLastName=@DocLastName,ExpertiseId=@ExpertiseId,License=@License,DocTelephone=@DocTelephone,DoctorId=@DoctorId";
        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);

    }

here i get the eror:(in this order : DoctorAdapter.Update(dt);) 在这里,我得到了错误:(按此顺序:DoctorAdapter.Update(dt);)


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

        }

    }

Well, you can't specify conditions on multiple columns using , comma operator. 嗯,你不能指定使用多个列的条件,逗号操作符。 It has to be either a AND or OR condition. 它必须是ANDOR条件。 That's the exact error you are getting. 那就是您得到的确切错误。

WHERE DocFirstName=@DocFirstName,DocLastName=@DocLastName,ExpertiseId=@ExpertiseId,...

should be 应该

WHERE DocFirstName=@DocFirstName
AND DocLastName=@DocLastName
AND ExpertiseId=@ExpertiseId
AND License=@License
.......

Your error is caused by an incorrect syntax in the WHERE clause. 您的错误是由WHERE子句中的语法错误引起的。 Here every condition should be linked to the next with an AND or OR statement. 在这里,每个条件都应使用AND或OR语句链接到下一个条件。

However, to delete a record from a table you simply need to know the Primary Key of that table and use a value from your data to search that single record in the database. 但是,要从表中删除记录,您只需要知道该表的主键,并使用数据中的值在数据库中搜索该单个记录即可。 Also there is no need to use an SqlDataAdapter in your scenario to delete a row 另外,在您的方案中也无需使用SqlDataAdapter来删除行

So assuming that DoctorID is the primary key, you could delete the database record with.... 因此,假设DoctorID是主键,则可以使用...删除数据库记录。

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();
    }
}

暂无
暂无

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

相关问题 C# System.Data.SqlClient.SqlException:'关键字'table'附近的语法不正确。' - C# System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'table'.' System.Data.SqlClient.SqlException:“)”附近的语法不正确 - System.Data.SqlClient.SqlException: Incorrect syntax near ')' System.Data.SqlClient.SqlException:关键字'file'附近的语法不正确 - System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'file' 修复异常 System.Data.SqlClient.SqlException: '''''附近的语法不正确。' - Fix the exception System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.' System.Data.SqlClient.SqlException: ''=' 附近的语法不正确。 在数据表和对象上 - System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.' on Datatable and object 收到错误“System.Data.SqlClient.SqlException:'')'附近的语法不正确。” - Getting an error "System.Data.SqlClient.SqlException: 'Incorrect syntax near ')'." System.Data.SqlClient.SqlException:“ =”附近的语法不正确 - System.Data.SqlClient.SqlException: Incorrect syntax near '=' System.Data.SqlClient.SqlException:'值'附近的语法不正确。 - System.Data.SqlClient.SqlException: 'Incorrect syntax near 'value'.' System.Data.SqlClient.SqlException:','附近的语法不正确。 - System.Data.SqlClient.SqlException: 'Incorrect syntax near ','.' System.Data.SqlClient.SqlException:''2' 附近的语法不正确。' - System.Data.SqlClient.SqlException: 'Incorrect syntax near '2'.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM