日期:2014-05-18  浏览次数:20524 次

怎么在select语句里面把查询列名设置为变量
请教各位大虾,原来select查询语句中把要查询的列名设置为数据库表中的固定列名来查询数据,现在想把里面要查询的列名设置为变量,在前面先定义两个变量,通过给它们赋不同的值,来改变select语句中要查询的列名....
本人初学C#,资历尚浅,不能解决这个问题,希望各位高手详细指点一二,不甚感激!
下面是程序代码





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using System.Windows.Forms.DataVisualization.Charting;

namespace chart绑定多个数据源1
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  declare 
  //string sf = FAC_SERIAL;
  OracleConnection thisConnection = new OracleConnection("Data Source=192.168.0.134:1521/dps;User ID=device;Password=device;Unicode=True;");
  //string selectquery="SELECT "
  DataSet thisDataSet = new DataSet();
  OracleDataAdapter facAdapter = new OracleDataAdapter(
  "SELECT @X:=FAC_ID,FAC_SERIAL FROM EMS_FAC_INFO WHERE FAC_ID<210000100 AND FAC_ID>210000034 order by FAC_ID", thisConnection);//用WHERE实现数据筛选
  OracleDataAdapter trAdapter = new OracleDataAdapter(
  "SELECT * FROM EMS_TR_DEVICE order by FAC_ID", thisConnection);
  OracleDataAdapter brkAdapter = new OracleDataAdapter(
  "SELECT * FROM accmng.liureportfault", thisConnection);

  facAdapter.Fill(thisDataSet, "EMS_FAC_INFO");
  trAdapter.Fill(thisDataSet, "TR_DEVICE");
  brkAdapter.Fill(thisDataSet, "accmng.liureportfault");
   
  DataView dv = new DataView(thisDataSet.Tables[0]);
  dv.RowFilter = "FAC_ID<210000047";//设置要显示的字段范围
  DataView dt = new DataView(thisDataSet.Tables[1]);
  dt.RowFilter = "FAC_ID<210000047";
   
  DataView dv1=new DataView(thisDataSet.Tables[2]);
   
  Series Series1 = new Series();
  Series1.ChartType = SeriesChartType.Line;
  chart1.Series.Add(Series1);
  Series Series2 = new Series();
  Series2.ChartType = SeriesChartType.Line;
  chart1.Series.Add(Series2);

  chart1.Series["Series1"].Points.DataBindXY(dv,"FAC_ID",dv,"FAC_SERIAL");
  chart1.Series["Series2"].Points.DataBindXY(dt, "FAC_ID", dt, "FIXLOSS");
  chart1.Dock = DockStyle.Fill;
  chart1.Titles.Add("多个数据源的绑定,数据的筛选、排序");
  Series1.IsVisibleInLegend = false;
   
  }

  }
}

------解决方案--------------------
你放到sql存储过程里我能告诉你...
------解决方案--------------------
列名做变量要动态执行
SQL code
exec('select '+@col1+','+@col2+' from tb')

------解决方案--------------------
sql=‘select '+ @f1+','+@f2 +'from db’
------解决方案--------------------
@sql=‘select '+ @f1+','+@f2 +'from db’
------解决方案--------------------