如何在c#中的select查询中获取字符串值 [英] how to get the string value in select query in c#

查看:830
本文介绍了如何在c#中的select查询中获取字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组字符串值,我想使用c#中的select查询中的值。怎么弄它



string dbdate = string.Join(,,dat);



string Sql = @select * from prg_detail where service_id ='+ comboBox1.SelectedValue +'and date in(@param);

conn.Open();

SqlCommand cmd = new SqlCommand(Sql,conn);

SqlDataAdapter dbad = new SqlDataAdapter(cmd);

cmd.Parameters.AddWithValue(@ param,dbdate );





DataTable dt3 = new DataTable();

dbad.Fill(dt3);



但它不起作用

解决方案

你不能这样做。

有一个SQL子句可以提供帮助:

  SELECT  *  FROM  MyTable  WHERE  Id  IN  1  2  3  4 

将返回列表中包含任何一个id值的行,您可以将其用于日期:但不能使用参数值。

您可以说:

  string  Sql =  @  SELECT * FROM prg_detail WHERE service_id =' + comboBox1.SelectedValue +  '和日期IN( + dbdate +  ; 

但是......您需要确保每个日期都正确格式化(最好是yyyy-MM-dd)并用引号分隔。

一种方法是:

  string  dbdate =  ' +  string  .Join( ',',dat)+  ; 

如果您的日期格式正确。


i have set of string values i want use the values in the select query in c#. how to get it

string dbdate=string.Join(",", dat);

string Sql = @"select * from prg_detail where service_id = '" + comboBox1.SelectedValue + "' and date in (@param)";
conn.Open();
SqlCommand cmd = new SqlCommand(Sql, conn);
SqlDataAdapter dbad = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@param", dbdate);


DataTable dt3=new DataTable();
dbad.Fill(dt3);

but its not working

解决方案

You can't do it like that.
There is a SQL clause that will help:

SELECT * FROM MyTable WHERE Id IN (1, 2, 3, 4)

Will returns rows with any one of the id values in the list, and you can use this for dates: but you can't use parameter values.
You could say:

string Sql = @"SELECT * FROM prg_detail WHERE service_id = '" + comboBox1.SelectedValue + "' AND date IN (" + dbdate + ")";

But...you would need to make sure that each of your dates is properly formated (preferably yyyy-MM-dd) and delimited by quotes.
One way to do that is:

string dbdate="'" + string.Join("','", dat) + "'";

If your dates come pre formatted correctly.


这篇关于如何在c#中的select查询中获取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆