斗战神新手:.net 2003连接数据库的问题(用ODBC接口)

来源:百度文库 编辑:高考问答 时间:2024/05/03 02:53:42
现有一个表(LoveDB),表的位置是:主机>yonghu(数据库)>LoveDB(表).
有一个网页sql.aspx.
操作系统:windows2003,平台:vs.net2003,sql2000

请问用ODBC接口怎么把一个表的内容显示网页上.
请写出简单函数和需要的其他操作.如在数据源里添加DSN.
总之你的方法能把数据显示出来,分就是你滴.
速度的优先,质量最重要.
需要把表在服务管理器里拖到窗体里么?
拖到sql.aspx里,还是拖到相对应的Global.asax中?

我在函数中这样定义的:
database=yonghu
odbccommand("select top 5 * from LoveDB")
不知道有问题不?

你只是想把数据取出来,根本就不用DNS数据源啊!那样显得更麻烦!
直接用页面定义一个DataGird!
然后声明一个数据库连接string conn="server=localhost; userid=sa; pwd=; DataBase=yonghu"
server是指服务器; userid是指你SQL访问的用户名,pwd是指密码,DataBase是指需要访问的数据库;

如下:
void Page_Load(Object sender, EventArgs e) //加载
{
if (! IsPostBack ) //防止事件回发
{
// set the initial page in the DataGrid to zero
// (not actually required as this is the default)
dgrdTitles.CurrentPageIndex = 0;
BindDataGrid();
}
}

void BindDataGrid (){
SqlConnection conn;
SqlCommand cmdSelect;

conn= new SqlConnection( @"server=localhost; userid=sa; pwd=; DataBase=yonghu" );//数据库连接
cmdSelect = new SqlCommand( "Select * From LoveDB", conn);//里面的就是查询语句
conn.Open();//打开连接
dgrdTitles.DataSource = cmdSelect.ExecuteReader();
dgrdTitles.DataBind(); //绑定DataGird
conn.Close(); //关闭连接
}

//dgrdTitles是指前台DataGrid的ID名字!