Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用数组删除数据库值_C#_Sql_Database - Fatal编程技术网

C# 如何使用数组删除数据库值

C# 如何使用数组删除数据库值,c#,sql,database,C#,Sql,Database,我使用此代码选择DataTable中的特定列。现在如何使用myRows[]值删除数据库中的值?例如: DataView view = new DataView(dt); DataTable distinctValues = view.ToTable(true, "ProductID"); DataRow[] myRows = distinctValues.Select(); 假设myRows[]有多个值。您可以在运算符中使用,并创建相应的字符串: cmd.CommandText = "Dele

我使用此代码选择DataTable中的特定列。现在如何使用myRows[]值删除数据库中的值?例如:

DataView view = new DataView(dt);
DataTable distinctValues = view.ToTable(true, "ProductID");
DataRow[] myRows = distinctValues.Select();

假设myRows[]有多个值。

您可以在运算符中使用
,并创建相应的字符串:

cmd.CommandText = "Delete from tblindividualproduct where ProductID = myRows[]";
cmd.ExecuteNonQuery();
您需要创建如下内容:

var idList = String.Join(",", myRows.Select(u => u["ColumnName"].ToString()));
string sql = string.Format("delete from tblindividualproduct where ProductID IN({0})", idList);

这将取决于你的后端是什么,假设你不想做循环或使用数据适配器,你的意思是什么?@ConradFrix:也许使用循环更容易理解,因为MikeSmithDev给我的链接对我来说太难理解它是如何工作的。很抱歉,只是一个noob。
string sql=“从tblindividualproduct中删除,其中ProductID位于(1,2,3)”
我应该把它放在哪里?这与
stringsql=string.Format(“delete from tblindividualproduct where ProductID in({0})”不同,idList)?不,这是您需要创建的
string sql=“delete from tblindividualproduct where ProductID IN(1,2,3)”
,或者类似的东西。最后,您需要使用
string.Format()
操作符中的
创建一个
delete
语句
string sql = "delete from tblindividualproduct where ProductID IN (1, 2, 3)"