从数据库中获取值 [英] Fetch the values from the DB

查看:90
本文介绍了从数据库中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从DB中获取值列表并在下拉列表中显示它们,如何在不传递任何内容的情况下获取。



用什么代替sqlparams ??

I have to fetch list of values from DB and display them in a dropdown, how can i fetch without passing anything.

what to use in place of sqlparams??

public List<string>  GetTimeZones()
{
    using (SqlDataReader dr = SqlHelper.ExecuteReader(DatabaseConnection.Connection,CommandType.Text, "sp_EP_GetTimeZones"))
    {
        if (dr.Read())
        { 
           
        
        }   
    }
}







ALTER procedure [dbo].[sp_EP_GetTimeZones] 
AS
BEGIN
  Select Zonetime from dbo.EP_TimeZones
  END

推荐答案

我想你正在寻找这个,希望它能帮到你



public DataTable GetAllEmployees()

{

DataTable dt = new DataTable();

SqlConnection con = new SqlConnection(conString);



尝试

{

SqlCommand cmd = new SqlCommand(StoredprocedureName,con);

cmd.CommandType = CommandType.StoredProcedure;



con.Open();

SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(dt) ;

}

}



最后根据您的要求将DataTable转换为List。
I guess you are looking for this,hope it will help you

public DataTable GetAllEmployees()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(conString);

try
{
SqlCommand cmd = new SqlCommand("StoredprocedureName", con);
cmd.CommandType = CommandType.StoredProcedure;

con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
}

At last Convert DataTable to List as per your requirements.


您可以返回 DataTable 并直接绑定它。让我更新您的代码。

You can return a DataTable and bind that directly. Let me update your code.
public DataTable GetTimeZones()
{
    DataTable dtTimeZones = new DataTable();

    SqlCommand cmd = new SqlCommand("sp_EP_GetTimeZones;", con);
    cmd.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    adapter.Fill(dtTimeZones);
    
    return dtTimeZones;
}



绑定时,你可以这样做......


While binding, you can do like...

ddlTimeZones.DataSource = GetTimeZones();
ddlTimeZones.DataTextField = "Zonetime";
ddlTimeZones.DataValueField = "Zonetime";
ddlTimeZones.DataBind();



我为 Zonetime >值和 ID 。如果您有任何其他列值,则只需在过程中获取并在此处更新。


I have assigned Zonetime for to both Value and ID. If you have any other column value, then just fetch that in procedure and update here.


这篇关于从数据库中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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