使用约束主键删除行时出错 [英] Error in Deleting row with constraint Primary Key

查看:82
本文介绍了使用约束主键删除行时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试删除行数据时,它会给我错误'cust_id'附近的sysntax错误。请任何好友解决这个问题









 创建 客户

cust_id 数字 10 null
cust_name nvarchar (max) null
cust_contact_no nvarchar (max) null
cust_order_machine nvarchar 255 not not null
cust_order_date date null
CONSTRAINT pk_CustomerID PRIMARY KEY (cust_id,cust_order_machine),
);





 private void Button_Click(object sender,RoutedEventArgs e)
{
string dbconnection =(@Data Source = EMIII-PC;初始目录= gymdatabase; Integrated Security = True);
SqlConnection conn = new SqlConnection(dbconnection);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(DELETE FROM Customer+WHERE cust_id = @id and cust_order_machine = @ com,conn);

cmd.Parameters.AddWithValue(@ id,textmidd.Text) ;

cmd.Parameters.AddWithValue(@ com,textcom.Text);




cmd.ExecuteNonQuery() ;

}

catch(exception ex)
{

MessageBox.Show(ex.Message.ToString());
}

最后
{
conn.Close();


}
}

解决方案

更改

 SqlCommand cmd =  new  SqlCommand(   DELETE FROM Customer +   WHERE cust_id = @id和cust_order_machine = @ com,conn); 



to

 SqlCommand cmd =  new  SqlCommand(  DELETE FROM Customer WHERE cust_id = @id and cust_order_machine = @ com, conn);在





你需要表名和<$ c之间的空格$ C>其中

well when i try to delete row data its give me Error "incorrect sysntax near 'cust_id'." please can any buddy sort out this problem





create table Customer 
(
cust_id numeric(10) not null,
cust_name nvarchar(max) not null,
cust_contact_no nvarchar(max) null,
cust_order_machine nvarchar(255) not null,
cust_order_date date null,
CONSTRAINT pk_CustomerID PRIMARY KEY (cust_id,cust_order_machine), 	
);



private void Button_Click(object sender, RoutedEventArgs e)
        {
            string dbconnection = (@"Data Source=EMIII-PC; Initial Catalog=gymdatabase;Integrated Security=True");
            SqlConnection conn = new SqlConnection(dbconnection);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("DELETE FROM Customer" +  "WHERE cust_id = @id and cust_order_machine=@com" , conn);

                cmd.Parameters.AddWithValue("@id", textmidd.Text);

                cmd.Parameters.AddWithValue("@com", textcom.Text);

                
                
                
                cmd.ExecuteNonQuery();
                
            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }

            finally
            {
                conn.Close();
               

            }
        }

解决方案

change

SqlCommand cmd = new SqlCommand("DELETE FROM Customer" +  "WHERE cust_id = @id and cust_order_machine=@com" , conn);


to

SqlCommand cmd = new SqlCommand("DELETE FROM Customer WHERE cust_id = @id and cust_order_machine=@com" , conn);



you need space between Table name and Where


这篇关于使用约束主键删除行时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆