| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 586 人关注过本帖
标题:[问题解决了]我为啥绑定不了数据??
只看楼主 加入收藏
wangchen223
Rank: 1
等 级:新手上路
帖 子:149
专家分:0
注 册:2006-7-1
收藏
 问题点数:0 回复次数:6 
[问题解决了]我为啥绑定不了数据??

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace WebApplication2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.ListBox ListBox1;
protected System.Web.UI.WebControls.TextBox TextBox1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
OleDbDataReader myDataReader;
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += "Data Source=";
strConnection += MapPath("Northwind.mdb");
OleDbConnection objConnection=new OleDbConnection(strConnection);
objConnection.Open();
OleDbCommand cmd = new OleDbCommand("insert into Northwind values(1)",objConnection);
SqlDataAdapter da=new SqlDataAdapter("select * from a",objConnection);
cmd.ExecuteNonQuery();
DataSet ds=new DataSet();
da.Fill(ds,"abcd");
ListBox1.DataSource=ds.Tables["abcd"];
ListBox1.DataBind();
objConnection.Close();
}
}
}

图片附件: 游客没有浏览图片的权限,请 登录注册

[此贴子已经被作者于2007-3-27 18:30:50编辑过]

搜索更多相关主题的帖子: using System 绑定 问题解决 Web 
2007-03-25 15:15
wangchen223
Rank: 1
等 级:新手上路
帖 子:149
专家分:0
注 册:2006-7-1
收藏
得分:0 
我想添加一个记录,然后就把数据绑定到ListBox1上,然后数据库是access的

2007-03-25 15:17
沙僧
Rank: 1
等 级:新手上路
帖 子:160
专家分:0
注 册:2006-7-23
收藏
得分:0 
你都没有在Page_Load里调用,怎么绑定呢?

我刚做的论坛:bbs.,欢迎大家光临!交友QQ:45094574.
2007-03-25 16:51
axa1986
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-7-4
收藏
得分:0 
你用SqlDataAdapter就不对了应用 OleDbDataAdapter
2007-03-26 00:45
axa1986
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-7-4
收藏
得分:0 

ListBox只能绑一列
用DataSet我不会 可以
protected void Button1_Click(object sender, EventArgs e)
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
strConnection += Server.MapPath("Northwind.mdb");
OleDbConnection objConnection = new OleDbConnection(strConnection);
objConnection.Open();
OleDbCommand OleDbCommand1 = new OleDbCommand();
OleDbCommand1.CommandText = "Select 列名 from 表名";
OleDbCommand1.Connection = objConnection;
ListBox1.DataSource = OleDbCommand1.ExecuteReader();
ListBox1.DataTextField = "列名";
ListBox1.DataBind();

}

2007-03-26 01:23
axa1986
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-7-4
收藏
得分:0 

DataSet行了
protected void Button1_Click(object sender, EventArgs e)
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
strConnection += Server.MapPath("Northwind.mdb");
OleDbConnection objConnection = new OleDbConnection(strConnection);
objConnection.Open();
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("select * from 表名", objConnection);
da.Fill(ds, "score");
ListBox1.DataSource = ds.Tables["表名"];
ListBox1.DataTextField = "列名";//关键
ListBox1.DataBind();
objConnection.Close();

}

2007-03-26 01:29
wangchen223
Rank: 1
等 级:新手上路
帖 子:149
专家分:0
注 册:2006-7-1
收藏
得分:0 
谢谢了.

2007-03-27 18:29
快速回复:[问题解决了]我为啥绑定不了数据??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015344 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved