如何从C#中删除SQL Compact 4.0中的表中的一行 [英] How do I delete just a row in my table in SQL compact 4.0 from C#

查看:82
本文介绍了如何从C#中删除SQL Compact 4.0中的表中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计算项目的数据库。

我想从我的表中删除一行,然后使用Visual Studio 2017进行C#。



我使用此代码在我的表和culamn中增加价值:



SqlCeConnection myconnection = new SqlCeConnection();

myconnection.ConnectionString =

数据库地址



SqlCeCommand mycommand = new SqlCeCommand();

mycommand.Connection = myconnection;



myconnection.Open();



mycommand.CommandText =

插入[Data_Calculate](c_value)值(@c_value);



mycommand.Parameters.AddWithValue(@ c_value,TextBox_Value.Text);

mycommand.ExecuteNonQuery();



myconnection.Close();



现在为删除ar我有这段代码:

SqlCeConnection myconnection = null;



myconnection = new SqlCeConnection();

myconnection.ConnectionString =

@Data Source = C:\ Users \ pc-mohsen \Documents\Visual Studio 2017 \Projects\CalculatorForm_project\DB Source\DB_Source_CalculatorForm.sdf ;



SqlCeCommand mycommand = myconnection.CreateCommand();

mycommand.CommandText =DELETE FROM Data_Calculate WHERE(c_value ='+ TextBox_Value.Text +');



myconnection.Open();

mycommand.ExecuteNonQuery();

myconnection.Close();

但这段代码删除了我的所有记录。我怎么能这样做?



我尝试过:



代码现在在这里。

SqlCeConnection myconnection = null;



myconnection = new SqlCeConnection();

myconnection.ConnectionString =

@Data Source = C:\ Users \ pc-mohsen \Documents\Visual Studio 2017 \Projects\CalculatorForm_project\DB Source\DB_Source_CalculatorForm.sdf ;



SqlCeCommand mycommand = myconnection.CreateCommand();

mycommand.CommandText =DELETE FROM Data_Calculate WHERE(c_value ='+ TextBox_Value.Text +');



myconnection.Open();

mycommand.ExecuteNonQuery();

myconnection.Close();

I have a data base for my calculate project.
I want delete a row from my table and I work C# with visual studio 2017.

I USE from this code to add value in my tables and culamn :

SqlCeConnection myconnection = new SqlCeConnection();
myconnection.ConnectionString =
database address

SqlCeCommand mycommand = new SqlCeCommand();
mycommand.Connection = myconnection;

myconnection.Open();

mycommand.CommandText =
"insert into [Data_Calculate](c_value)Values(@c_value)";

mycommand.Parameters.AddWithValue("@c_value", TextBox_Value.Text);
mycommand.ExecuteNonQuery();

myconnection.Close();

NOW for delete a row I have this code :
SqlCeConnection myconnection = null;

myconnection = new SqlCeConnection();
myconnection.ConnectionString =
@"Data Source= C:\Users\pc-mohsen\Documents\Visual Studio 2017\Projects\CalculatorForm_project\DB Source\DB_Source_CalculatorForm.sdf";

SqlCeCommand mycommand = myconnection.CreateCommand();
mycommand.CommandText = "DELETE FROM Data_Calculate WHERE(c_value= '"+TextBox_Value.Text+"')";

myconnection.Open();
mycommand.ExecuteNonQuery();
myconnection.Close();
but this code remove all my record. how can I do that?

What I have tried:

the codes are here now.
SqlCeConnection myconnection = null;

myconnection = new SqlCeConnection();
myconnection.ConnectionString =
@"Data Source= C:\Users\pc-mohsen\Documents\Visual Studio 2017\Projects\CalculatorForm_project\DB Source\DB_Source_CalculatorForm.sdf";

SqlCeCommand mycommand = myconnection.CreateCommand();
mycommand.CommandText = "DELETE FROM Data_Calculate WHERE(c_value= '"+TextBox_Value.Text+"')";

myconnection.Open();
mycommand.ExecuteNonQuery();
myconnection.Close();

推荐答案

不是那样的!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期进行备份,不是吗?



然后仔细查看文本框内容和列数据 - 删除所有匹配的行WHERE标准因此重要的是它只匹配您想要的行。通常,我们使用主键值来专门标识单个行。

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Then take a close look at the text box content and your column data - the delete with remove all rows that match the WHERE criteria so it's important that it matches just the row you want. Normally, we use the Primary Key value to specifically identify a single row.


这篇关于如何从C#中删除SQL Compact 4.0中的表中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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