如何建立SQL选择查询

时间:2019-01-29 11:32:25

标签: c# sql xml

我有用于解析xml文档和填充数据集的代码。

我可以创建简单的SELECT查询(即SELECT GRAPHIC_ID AS INDEX FROM GRAPHICS)。因此,我需要使用“ for each”循环或类似方式构建像selectString这样的复杂SELECT查询(SELECT GRAPHIC_ID AS INDEX, GRAPIC_NAME AS NAME, etc... FROM GRAPHICS)。我需要逐一添加GRAPIC_NAME AS NAME之类的值块,以建立正确的SELECT查询连接

这是我的填充方法

    var connect = new OracleConnection(ConnectionString);
commandBuilder = new OracleCommandBuilder(dataAdapter);
Dictionary<string, string> listBoxDict = new Dictionary<string, string>();
XDocument doc = XDocument.Load(@"D:\TableName_reworked.xml");
foreach (XElement tableElement in doc.Element("TABLES").Elements("TABLE"))
{
    tableName = tableElement.Attribute("NAME");
    tableNameDic = tableElement.Element("TABLENAMEDIC");
    linkField = tableElement.Element("LINKFIELD");
    listBoxDict.Add(tableNameDic.Value.ToString(), tableName.Value.ToString());
    foreach (XElement columnsElement in tableElement.Elements("COLUMNS").Elements("COLUMN"))
    {
        columnName = columnsElement.Element("COLUMNNAME");
        columnNameDic = columnsElement.Element("COLUMNNAMEDIC");
    }
    string selectString = "SELECT " + columnName.Value.ToString() + " AS " + columnNameDic.Value.ToString() + " FROM " + tableName.Value.ToString();



     dataAdapter.SelectCommand = New OracleCommand(selectString, connect)
     connect.CreateCommand()
     dataAdapter.Fill(ds, tableName.Value.ToString())
    selectString = null;
}
ListBox1.DataSource = new BindingSource(listBoxDict, null);
listbox1.DisplayMember = "Key";
listbox1.ValueMember = "Value";

那是我的XML文件

<?xml version="1.0"?>
<TABLES>
<TABLE NAME ="GRAPHICS">
<TABLENAMEDIC>GRAPHS</TABLENAMEDIC>
 <LINKFIELD>GRAPHIC_ID</LINKFIELD>
 <COLUMNS>
 <COLUMN>
    <COLUMNNAME>GRAPHIC_ID</COLUMNNAME>
    <COLUMNNAMEDIC>INDEX</COLUMNNAMEDIC>
</COLUMN>
<COLUMN>
    <COLUMNNAME>GRAPHIC_NAME</COLUMNNAME>
    <COLUMNNAMEDIC>NAME</COLUMNNAMEDIC>
</COLUMN>
</COLUMNS>
</TABLE>

<TABLE NAME ="METHODS">
<TABLENAMEDIC>METHS</TABLENAMEDIC>
<LINKFIELD>GRAPHIC_ID</LINKFIELD>
<COLUMNS>
<COLUMN>
    <COLUMNNAME>GRAPHIC_ID</COLUMNNAME>
    <COLUMNNAMEDIC>INDEX</COLUMNNAMEDIC>
    </COLUMN>
     <COLUMN>
    <COLUMNNAME>ID</COLUMNNAME>
    <COLUMNNAMEDIC>INDEX2</COLUMNNAMEDIC>
     </COLUMN>
      <COLUMN>
     <COLUMNNAME>METHOD</COLUMNNAME>
    <COLUMNNAMEDIC>METH NAME</COLUMNNAMEDIC>
    </COLUMN>
</COLUMNS>
</TABLE>
</TABLES>

1 个答案:

答案 0 :(得分:0)

尝试在循环内构建字符串,并将每一列一一添加到您的SELECT语句中,就像这样

   Dim selectString As String = "SELECT " 

   For Each dd As XElement In tableElement.Elements("COLUMNS").Elements("COLUMN")
            columnName = dd.Element("COLUMNNAME")
            columnNameDic = dd.Element("COLUMNNAMEDIC")
            columnStatement = columnName.Value.ToString() & " AS " & columnNameDic.Value.ToString() & ", "
            // add each columnStatement to your SELECT ... 
            selectString = selectString & columnStatement
   Next
   // cut the last ", "
   selectString = selectString . Substring(0, selectString . Length-1);

   // and your FROM 
   selectString = selectString & " FROM " & tableName.Value.ToString()