给我数据列表填充示例代码 [英] give me datalist populate example with the coding
问题描述
亲爱的先生
我想用编码填充数据列表我该怎么办
和我在ms-access中的数据库
我只想显示特定列中的前3条记录,您能帮我吗
我的数据库表必须具有列Id,cat_type且表名称是catagory
我想显示cat_type列中的前3条记录
我该怎么做help
Dear Sir
i want to populate datalist with the coding how can i do
and my database in ms-access
i want to show only 3 top record from the specific column can you help me
my database table have to column Id,cat_type and table name is catagory
i want to show top 3 record from cat_type column
how can i do pls help
推荐答案
在web.config中设置连接字符串
set connection string in web.config
<connectionStrings>
<add name="demoConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\demo.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>
像这样在cs文件顶部导入包
import package in top of the cs file like this
using System.Data.OleDb;
在代码文件中这样写连接字符串
in code file write connection string like this
private string dbConnectionString = ConfigurationManager.ConnectionStrings["demoConnectionString"].ConnectionString;
编写一种从数据库中检索数据的方法
write a method for retrive data from database
public DataSet showProductDetails(int pid)
{
string sqlQry = "Select * from Products where ProductID=" + pid + "";
OleDbConnection dataConnection = new OleDbConnection(dbConnectionString);
OleDbCommand command = new OleDbCommand(sqlQry, dataConnection);
dataConnection.Open();
command.CommandType = CommandType.Text;
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command);
DataSet dsProductDetails = new DataSet();
dataAdapter.Fill(dsProductDetails);
return dsProductDetails;
}
最后在pageload事件中设置datalist的数据源,或者如果要在按钮事件中加载,请尝试此
finally set the datasource of datalist in pageload event or if you want to load in button event try this
int category = Convert.ToInt16(Request.QueryString["cid"]);
dsProductList = productList.ShowProductByCategoryID(category);
dtProductList = dsProductList.Tables[0];
DataList1.DataSource = dtProductList;
DataList1.DataBind();
for (int i = 0; i < dtProductList.Rows.Count; i++)
{
ImageButton imgButton =(ImageButton) DataList1.Items[i].FindControl("ImageButton1");
Label lblName =(Label) DataList1.Items[i].FindControl("lblItemName");
Label lblPrice =(Label) DataList1.Items[i].FindControl("lblPrice");
imgButton.ImageUrl = string.Format("~/Images/" + dtProductList.Rows[i]["ImageUrl"].ToString() + "");
imgButton.PostBackUrl = string.Format("~/UI/ProductDetails.aspx?ProductID={0}", dtProductList.Rows[i]["ProductID"].ToString());
lblName.Text = dtProductList.Rows[i]["Name"].ToString();
lblPrice.Text = dtProductList.Rows[i]["Price"].ToString();
}
它是在数据列表中查看产品详细信息的示例示例,希望您能了解如何在数据列表中显示数据.我也建议你搜索谷歌更好的解决方案.如果您有任何疑问或信息,请告诉我.
谢谢
its a sample example of view product details in datalist hope you will get idea that how to display data in data list. also i suggest you to search google for better solution. if you have any qry or information let me know.
Thanks
这篇关于给我数据列表填充示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!