【问题标题】:How can i create a customizable SQL function in Visual Studio如何在 Visual Studio 中创建可自定义的 SQL 函数
【发布时间】:2018-01-21 17:15:36
【问题描述】:

是否可以在 Visual Studio 中创建可扩展的 SQL 查询?

private void button1_Click(object sender, EventArgs e)
{
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "Select geneID from Table3 where geneID in(" + filterdata + ")";

    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    dataGridView1.DataSource = dt;

    con.Close();
}

这可以扩展为使用任何可能的条件从任何可能的表中选择任何可能的参数。我认为它看起来像下面这样:

从 [Table 1, Table2...] 中选择 [Variable 1,Variable 2...] where [Condition1, Condition 2...]

在这种情况下,将使用多个复选框来选择变量、表格和条件。我想将任何可能的搜索合并到一键单击中。

【问题讨论】:

  • 查找动态 SQL

标签: c# sql visual-studio checkbox


【解决方案1】:

使用 sql 连接 ..在您的评论文本中,例如 select a.row,b.rowtwo from tableone a inner join tabletwo b on a.row = b.row where a.row = your values

【讨论】:

    【解决方案2】:

    可以使用String.Format方法

    根据指定的格式将对象的值转换为字符串,并将它们插入到另一个字符串中。

    如果您不熟悉 String.Format 方法,请参阅Getting started with the String.Format method 部分以快速了解。

    所以你可以这样使用

    cmd.CommandText = String.Format("Select {0} from {1} where {2}", columns, tables, conditions)
    

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 2010-10-19
      • 1970-01-01
      • 2012-04-27
      相关资源
      最近更新 更多