想要通过使用本地变量值连接列名来创建列名来从数据库中获取记录 [英] want to fetch a record from Database by making column name by concatenate column name with local variable value

查看:67
本文介绍了想要通过使用本地变量值连接列名来创建列名来从数据库中获取记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在考勤表中,他们是列D1,D2,...... D31。我正在尝试通过Concatenate D& D来获取一列。局部变量j1。其中j1是月份的最后一天(如D30或D31或D28或D29)如果j1 = 30则D+ j1 +视为D30并将其值分配给变量att但它分配30而不是D30的值怎么能我通过添加带有列名的局部变量(如D+ j1 +)来获取实际值。如果j1 = 30,查询应该返回D30的值并将其存储在att变量上。





 string j1 = Convert.ToString(days1); 
att =;
ad = new SqlDataAdapter(选择D+ j1 +来自出勤,其中Emp_Code ='+ TextBox1.Text +',reg ='+ regn1 +'和Type ='+ Label112.Text + ',con);

if(dt6.Rows.Count> 0)
{
att = Convert.ToString(dt6.Rows [0] [0]);
}

解决方案

试试这个...



 string j1 = Convert.ToString(days1); 
att =;
ad = new SqlDataAdapter(选择D+ j1 +来自出勤,其中Emp_Code ='+ TextBox1.Text +'和reg ='+ regn1 +'和[Type] ='+ Label112 .Text +',con);

DataTable dt6 = new DataTable();

ad.Fill(dt6);

if(dt6.Rows.Count> 0)
{
att = Convert.ToString(dt6.Rows [0] [0]);
}


下面的代码将帮助您





  string  j1 = Convert.ToString(days1); 
att = ;
ad = new SqlDataAdapter( 选择D + j1 + 来自出席其中​​ Emp_Code = ' + TextBox1.Text +'和reg = ' + regn1 +'和[Type] = ' + Label112。文本+' ,con);

DataTable dt6 = new DataTable ();

ad.Fill(dt6);

if(dt6.Rows.Count> 0)
{
att = dt6。行[0] [0] .ToString()+
' s;
}


In attendance table thier are columns D1,D2,....D31.below i'm trying to fetch a column by Concatenate D& local variable j1 .here j1 is the last day of month(likeD30 or D31 or D28 or D29) if j1=30 then D"+j1+" consider as D30 and assign it's value to variable att but it assigns 30 not D30's value how can i fetch actual value by adding local variable with Column name (like D"+j1+")..hereif j1=30 the query should return D30's value and store it on att variable ..


string j1 = Convert.ToString(days1);
        att = "";
        ad = new SqlDataAdapter("select D"+j1+"  from Attendance where Emp_Code='" + TextBox1.Text + "',reg='" + regn1 + "'and Type='" + Label112.Text + "'", con);
        
        if (dt6.Rows.Count > 0)
        {
            att = Convert.ToString(dt6.Rows[0][0]);
        }

解决方案

Try this...

string j1 = Convert.ToString(days1);
att = "";
ad = new SqlDataAdapter("select D" + j1 + " from Attendance where Emp_Code='" + TextBox1.Text + "' and  reg='" + regn1 + "' and [Type]='" + Label112.Text + "'", con);

DataTable dt6 = new DataTable();

ad.Fill(dt6);

if (dt6.Rows.Count > 0)
{
 att = Convert.ToString(dt6.Rows[0][0]);
}


below code will help You


string j1 = Convert.ToString(days1);
att = "";
ad = new SqlDataAdapter("select D + j1 + " from Attendance where Emp_Code='" + TextBox1.Text + "' and  reg='" + regn1 + "' and [Type]='" + Label112.Text + "'", con);

DataTable dt6 = new DataTable();

ad.Fill(dt6);

if (dt6.Rows.Count > 0)
{
 att = dt6.Rows[0][0].ToString()+"'s";
}


这篇关于想要通过使用本地变量值连接列名来创建列名来从数据库中获取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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