简体   繁体   English

C#使用列表和文本文件执行SQL查询然后导出

[英]C# using list table and text file to execute SQL query then export

Scenario: I have a list table being populated from a combobox. 场景:我有一个从组合框填充的列表表。 That list table can have up to 50 things listed in it(whats listed in the listbox is column names). 该列表最多可以列出50个内容(列表框中列出的是列名)。 Tricky part: I have a text file that matches one field in the sql table. 棘手的部分:我有一个与sql表中的一个字段匹配的文本文件。 X1,X2,X3 are the listbox items. X1,X2,X3是列表框项。

So i basically need: 所以我基本上需要:

select <line in text file>, from <blah> where id = object_id('table')  
export <x2>,<x3>,<x4>,<x5>,<x6>,C:\Comma.text

I dont need help making the listbox, or the SQL connection. 我不需要帮助来创建列表框或SQL连接。

Just basically how do i list my listbox in strings (no matter what the order selected is to represent whatever was selected in that order, and the SQL command to match based on textfile and export it.) 基本上,我该如何在字符串中列出列表框(无论选择的顺序是代表该顺序中选择的内容,还是基于文本文件进行匹配并导出的SQL命令。)

I know this one is going to be a pain. 我知道这会很痛苦。 Thanks in advanice 提前感谢

Your question isn't very clear... does the text file contain the primary key values for rows in the table? 您的问题不是很清楚...文本文件是否包含表中行的主键值?

If that is the case, your SQL pseudo code will look like so: 如果是这样,您的SQL伪代码将如下所示:

select <selectedColumn1>, <selectedColumn2>,...  
from yourTable  
where primarykeyColumn in (<yourTextFileValues>)  

As to how to get selected items from a listbox to a string, and format a SQL statement to only select those rows from the database to export, here is one way: 关于如何将列表框中的选定项转换为字符串,以及如何格式化SQL语句以仅从数据库中选择要导出的行,这是一种方法:

string[] selectedColumns = new string[myListBox.SelectedItems.Count];
for(int i = 0; i < myListBox.SelectedItems.Count; i++)
{
    selectedColumns[i] = myListBox.SelectedItems[i].ToString();
}
string exportColumns = string.Join(",", selectedColumns);

//format your sql statement
string sqlStatement = string.Format("select {0} from YourTable WHERE blah=blah2", 
                                     exportColumns);

If everything in the list box is considered 'selected' for export, then just replace myListBox.SelectedItems with myListBox.Items 如果将列表框中的所有内容都视为“已选择”用于导出,则只需将myListBox.SelectedItems替换myListBox.SelectedItems myListBox.Items

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM