从DateGridView创建单引号逗号分隔字符串 [英] Create a single-quoted comma Separated String from DateGridView
本文介绍了从DateGridView创建单引号逗号分隔字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含两列的DataGridview。如何创建可以传递给SQL查询的单引号逗号分隔字符串?
例如
A1111
B2333
C3444
D4555
我想创建一个字符串为
' A1111',B2333','C3444','D4555'
解决方案
以下获取每一行值不考虑行是否有空单元格。
使用System;
使用System.Linq;
使用System.Windows.Forms;
命名空间WindowsFormsApp1
{
公共部分类Form1:表格
{
public Form1()
{
InitializeComponent() ;
}
private void Form1_Load(object sender,EventArgs e)
{
dataGridView1.Rows.Add(" A1111");
dataGridView1.Rows.Add(" B2333");
dataGridView1.Rows.Add(" C3444");
dataGridView1.Rows.Add(" D4555");
}
private void button1_Click(object sender,EventArgs e)
{
var result =
" SELECT Id FROM Customers WHERE CustomerName IN({string.Join(",",dataGridView1.Rows.Cast< DataGridViewRow>()。Where(row => row.IsNewRow == false).Select(row =>
" '{row.Cells [" column1的"]值。}' ")ToArray的())})英寸;
Console.WriteLine(result);
}
}
}输出
var items = dataGridView1.Rows.Cast< DataGridViewRow>()
.Where(row => row.IsNewRow == false&&!string.IsNullOrWhiteSpace (Convert.ToString(row.Cells [" Column1"]。Value)))
.Select(row =>
I have a DataGridview with two columns. how do I create a single-quoted comma separated string that I can pass to a SQL Query?
e.g.
A1111B2333
C3444
D4555
I want to create a string as
'A1111', B2333', 'C3444', 'D4555'
解决方案
The following gets each row value without considering if a row has a empty cell.
using System; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dataGridView1.Rows.Add("A1111"); dataGridView1.Rows.Add("B2333"); dataGridView1.Rows.Add("C3444"); dataGridView1.Rows.Add("D4555"); } private void button1_Click(object sender, EventArgs e) { var result =
"SELECT Id FROM Customers WHERE CustomerName IN ({string.Join(",", dataGridView1.Rows.Cast<DataGridViewRow>().Where(row => row.IsNewRow == false).Select(row =>
"'{row.Cells["Column1"].Value}'").ToArray())})"; Console.WriteLine(result); } } }Output
SELECT AccountNumber FROM Customers WHERE CustomerName IN ('A1111','B2333','C3444','D4555')This checks for empty cells
var items = dataGridView1.Rows.Cast<DataGridViewRow>() .Where(row => row.IsNewRow == false && !string.IsNullOrWhiteSpace(Convert.ToString(row.Cells["Column1"].Value))) .Select(row =>
这篇关于从DateGridView创建单引号逗号分隔字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文