开发者

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

开发者 https://www.devze.com 2022-12-13 07:11 出处:网络
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).Tricky part:I have a text file that matc

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). Tricky part: I have a text file that matches one field in the sql table. X1,X2,X3 are the listbox items.

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.

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.)

I know this one is going to be a pain. Thanks in ad开发者_运维知识库vanice


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:

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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号