如何使用数据库中的月份和年份在csv中显示数据 [英] How do I get a data to be displayed in a csv using month and year from a datbaseatabase

查看:108
本文介绍了如何使用数据库中的月份和年份在csv中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用(SqlDataAdapter sda = new SqlDataAdapter())

{

cmd.Connection = con;

sda.SelectCommand = cmd;

使用(DataTable dt = new DataTable())

{

sda.Fill(dt);



我尝试过:



using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);

What I have tried:

double Month = Convert.ToDouble(DropDownList1.SelectedItem.Value);
     double Year = Convert.ToDouble(DropDownList2.SelectedItem.Value);
     string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
     using (SqlConnection con = new SqlConnection(constr))
     {
         using (SqlCommand cmd = new SqlCommand("SELECT a.FirstName +' '+ a.MiddleName +' '+a.LastName as ApplicantName,a.ApplicationNo,b.[ApplicationDate] FROM [Applicant]as a,Application as b where a.ApplicationNo= b.ApplicationNo and MONTH(b.ApplicationDate) = @Month and YEAR(b.ApplicationDate) = @Year "))
         //using (SqlCommand cmd = new SqlCommand("SELECT a.FirstName +' '+ a.MiddleName +' '+a.LastName as ApplicantName,a.ApplicationNo,b.[ApplicationDate] FROM [Applicant]as a,Application as b where a.ApplicationNo= b.ApplicationNo and YEAR(b.ApplicationDate) = @Year "))
         {
             SqlParameter param = new SqlParameter();
             param.ParameterName = "@Month";
             param.Value = Month;

             cmd.Parameters.Add(param);
             //SqlParameter param = new SqlParameter();
              param.ParameterName = "@Year";
              param.Value = Year;
             cmd.Parameters.Add(param);

推荐答案

好吧,马上,你需要创建单独的SqlParameter每个参数的对象。否则,您将传递月份和年份的相同值。在修复之前,您可能无法获得您感兴趣的数据(@Month参数与@Year参数相同)。
Well, right off the bat, you need to create separate SqlParameter objects for each parameter. Otherwise, you're passing the same value for the month and year. You probably won't be able to get the data you're interested in until you fix that (the @Month parameter will be the same as the @Year parameter).


这篇关于如何使用数据库中的月份和年份在csv中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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