怎么获取数据库里的链接,将其链接到网页上,在网页上点击时还可以链接过去(数据库是sql2000)
比如说数据库里有表名:Games
列名:DownLoad
值:ftp://172.45.25.12
--------------------------------------
网页上: 下载
———————————————————
现在就是要点击网页上的“下载”时能链接到数据库里的“ftp://172.45.25.12”,我知道在网页上要用
<a href="">下载</a>
但不知道这href=""怎么写才可以将数据库里的表Games里的DownLoad下的值获取到<a href="">下载</a>的""里,我不知道可不可以这样,如果这样不可以,要怎么样可以实现我想要的功能呢。
(我知道可以用<a href="ftp://172.45.25.12">下载</a>实现,但是我要的是动态的,如果这样写,太麻烦了,要一个页面一个页面的写,不现实,我要的是动态获取Games里的DownLoad的值)
谢谢了啊! --------------------编程问答-------------------- public String url = "";
Page_Load里面写
读取数据库
if(dr.Read())
{
url = dr["DownLoad"].ToString();
}
aspx里
<a href="<%=url%>">下载</a> --------------------编程问答-------------------- 如果是多条数据显示,直接采用GridView等数据控件绑定即可 --------------------编程问答-------------------- 界面的链接用数据绑定 --------------------编程问答--------------------
+1 --------------------编程问答-------------------- 那个if条件里的dr是什么啊,要怎么定义啊 --------------------编程问答-------------------- 数据库操作你是怎样写的 --------------------编程问答-------------------- dr是datareader定义:DataReader dr=comm.ExcuteDataReader();
一楼的建议不错 --------------------编程问答-------------------- 这个是代码页
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_GameUpdate : System.Web.UI.Page
{
public String url = "";
public int gameid;
public static string bid;
public static string tname;
//连接数据库
SqlConnection con = new SqlConnection(Base.strConn);
protected void Page_Load(object sender, EventArgs e)
{
try
{
gameid = int.Parse(Request["bid"]);
}
catch (Exception)
{
gameid = 0;
}
if (!IsPostBack)
{
if (Session["AdminID"] != null)
{
/*读取用户名和密码信息*/
//创建用户对象,判断当前用户身份和信息是否正确
cs_Admin sysa = new cs_Admin();
sysa.AdminID = Session["AdminID"].ToString();
sysa.Password = Session["Password"].ToString();
if (!sysa.IsAdmin(sysa.AdminID, sysa.Password))
{
Response.Write("<Script>alert('没有管理权限!');history.back(-1);</Script>");
return;
}
}
else
Response.Redirect("Login.aspx");
cs_Games game = new cs_Games();
game.Id = gameid;
game.GetGameInfo();
//连接机种
string strIndex = Request.Form["gametypeList"];
string SqlString = "SELECT * FROM Type";
con.Open();
// 连接指定表
SqlDataAdapter Myadapter = new SqlDataAdapter(SqlString, con);
DataSet ds = new DataSet();
Myadapter.Fill(ds, SqlString); // 装入数据
gametypeList.DataSource = ds.Tables[0]; //数据源设置
gametypeList.DataTextField = "GameTypeName";
gametypeList.DataValueField = "TyID";
gametypeList.DataBind();
gametypeList.SelectedValue = game.TyID.ToString();
//数据绑定
con.Close();
SqlString = "SELECT * FROM GameType where TyID=" + int.Parse(game.TyID.ToString());
Myadapter = new SqlDataAdapter(SqlString, con);
ds = new DataSet();
Myadapter.Fill(ds, SqlString); // 装入数据
typeList.DataSource = ds.Tables[0]; //数据源设置
typeList.DataTextField = "TypeName";
typeList.DataValueField = "TypeId";
typeList.DataBind(); //数据绑定
con.Close();
typeList.SelectedValue = game.TypeId.ToString();
#region 装载当前图书的信息
/* cs_Games game = new cs_Games();
game.Id = gameid;
game.GetGameInfo();
gametypeList.SelectedValue = game.TyID.ToString();
typeList.SelectedValue = game.TypeId.ToString();*/
GameName.Text = game.GameName;
GameSize.Text = game.GameSize;
List.SelectedItem.Text = game.GameType;
runningenvList.SelectedItem.Text = game.RunningEnv;
accreditwayList.SelectedItem.Text = game.AccreditWay;
try
{
Calendar1.SelectedDate = DateTime.Parse(game.PublishDate);
}
catch (Exception)
{
Calendar1.SelectedDate = DateTime.Now;
}
txtContent.Text = game.Content;
upimage.Text = game.ImageFile;
DownLoadAddr.Text = game.DownloadAddr;
#endregion
}
}
protected void gametypeList_TextChanged(object sender, EventArgs e)
{
string SqlString = "SELECT * FROM GameType where TyID=" + int.Parse(gametypeList.SelectedValue);
SqlDataAdapter Myadapter = new SqlDataAdapter(SqlString, con);
DataSet ds = new DataSet();
Myadapter.Fill(ds, SqlString); // 装入数据
typeList.DataSource = ds.Tables[0]; //数据源设置
typeList.DataTextField = "TypeName";
typeList.DataValueField = "TypeId";
typeList.DataBind(); //数据绑定
con.Close();
}
protected void Btn_sure_Click(object sender, EventArgs e)
{
//判断是否输入类型名称
if (GameName.Text.Trim() == "")
{
Response.Write("<Script>alert('请输入游戏名称');</Script>");
return;
}
//判断是否更改分类名称
if (GameName.Text.Trim() != tname)
{
//判断分类名称是否重复
cs_Games objType = new cs_Games();
try
{
objType.TyID = int.Parse(gametypeList.SelectedValue);
}
catch
{
objType.TyID = 1;
}
try
{
objType.TypeId = int.Parse(typeList.SelectedValue);
}
catch
{
objType.TypeId = 1;
}
objType.Id = int.Parse(Request["bid"]);
objType.GameName = GameName.Text.Trim();
if (objType.Exists())
{
Response.Write("<Script>alert('该机种在该分类下的游戏已经存在!');</Script>");
GameName.Focus();
}
else
{
objType.GameName = GameName.Text.Trim();
objType.GameSize = GameSize.Text.Trim();
objType.GameType = List.SelectedItem.Text.Trim();
objType.RunningEnv = runningenvList.SelectedItem.Text.Trim();
objType.AccreditWay = accreditwayList.SelectedItem.Text.Trim();
objType.PublishDate = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
objType.Content = txtContent.Text.Trim();
objType.ImageFile = upimage.Text.Trim();
objType.DownloadAddr = DownLoadAddr.Text.Trim();
//更新分类名称
objType.Update();
Response.Redirect("GameList.aspx");
}
}
}
protected void Btn_back_Click(object sender, EventArgs e)
{
}
protected void typeList_TextChanged(object sender, EventArgs e)
{
}
}
--------------------编程问答-------------------- 这个是网页(这个页面我是用模板加载的)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GameUpdate.aspx.cs" Inherits="admin_GameUpdate" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>游戏修改</title>
</head>
<body>
<center>
<form id="myform" runat="server">
<div id="page" style="width: 900px">
<div id="hand" style="width: 900px">
<img alt="" src="../imgs/head.jpg" />
</div>
<div id="middle" style="width: 900px; padding:0px">
<div id="midleft" style="width: 180px; margin:0px; float:left"><!--中间左边-->
<table style="float:left;" border="1">
<tr>
<td><img alt="" src="../imgs/left0.jpg" /></td>
</tr>
<tr>
<td style=" width:150px">
<a href="../admin/TypeList.aspx">机种管理</a></td>
</tr>
<tr>
<td><img alt="" src="../imgs/left1.jpg" /></td>
</tr>
<tr>
<td style=" width:150px">
<a href="../admin/GameTypeList.aspx">分类管理</a></td>
</tr>
<tr>
<td style=" width:150px">
<a href="../admin/GameList.aspx">游戏管理</a></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><img alt="" src="../imgs/left2.jpg" /></td>
</tr>
<tr>
<td style=" width:150px; ">
<a href="../admin/UserList.aspx">会员管理</a></td>
</tr><tr>
<td style=" width:150px; padding-left:10px">网站管理员</td>
</tr>
<tr>
<td style=" width:150px">修改密码</td>
</tr>
<tr>
<td style=" width:150px">退出</td>
</tr>
</table>
</div><!--中间左边-->
<div id="midright" style="width: 705px; margin:0px; float:right"><!--中间右边-->
<strong><span style="font-size: 16pt; color: #0000cc">修改游戏 </span></strong>
<table border="1" style="width: 585px">
<tr>
<td style="text-align: center; width: 79px;">游戏分类</td>
<td style="width: 155px; text-align: left"><asp:DropDownList AutoPostBack="true" ID="gametypeList" runat="server" OnTextChanged="gametypeList_TextChanged">
</asp:DropDownList>
<asp:DropDownList AutoPostBack="true" ID="typeList" runat="server" OnTextChanged="typeList_TextChanged">
</asp:DropDownList></td>
<td>游戏名称</td>
<td style="text-align: left; width: 115px;"><asp:TextBox ID="GameName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="height: 28px; width: 79px;">游戏大小</td>
<td style="height: 28px; width: 155px; text-align: left;"><asp:TextBox ID="GameSize" runat="server" Width="109px"></asp:TextBox></td>
<td style="height: 28px">资源类别</td>
<td style="height: 28px; text-align: left; width: 115px;">
<asp:DropDownList ID="List" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem >家机模拟</asp:ListItem>
<asp:ListItem >PC</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td style="width: 79px">运行环境</td>
<td style="width: 155px; text-align: left">
<asp:DropDownList ID="runningenvList" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem >Win7,Vista,XP,2003</asp:ListItem>
<asp:ListItem>Win7</asp:ListItem>
<asp:ListItem>Vista</asp:ListItem>
<asp:ListItem>XP</asp:ListItem>
<asp:ListItem>2003</asp:ListItem>
</asp:DropDownList></td>
<td>授权方式</td>
<td style="text-align: left; width: 115px;">
<asp:DropDownList ID="accreditwayList" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>共享</asp:ListItem>
<asp:ListItem>独家</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td style="width: 79px">更新时间</td>
<td style="width: 155px">
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
</td>
<td> 游戏介绍</td>
<td style="width: 115px"><asp:TextBox ID="txtContent" runat="server" Height="184px" TextMode="MultiLine" Width="163px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="4" style="height: 98px; text-align: left;"><iframe frameborder="0" height="80" width="600" scrolling="no" src="AddImgs.aspx" id="IFRAME1" onclick="return IFRAME1_onclick()" style="width: 504px; height: 133px;" ></iframe>
<br />
文件名称:
<asp:TextBox ID="upimage" runat="server" Visible="True"></asp:TextBox>
</td>
</tr>
<tr>
<td style="height: 28px; width: 79px;">
下载地址</td>
<td style="height: 28px; text-align: left" colspan="3">
<asp:TextBox ID="DownLoadAddr" runat="server" Width="405px"></asp:TextBox></td>
</tr>
</table>
<asp:Button ID="Btn_sure" runat="server" OnClick="Btn_sure_Click" Text="确定" Width="55px" />
<asp:Button ID="Btn_back" runat="server" Text="返回" Width="57px" OnClick="Btn_back_Click" /><br />
</div><!--中间右边-->
</div>
</div>
</form>
</center>
</body>
</html>
--------------------编程问答-------------------- 这个是Base.cs页
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Base 的摘要说明
/// </summary>
public class Base
{
public Base()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region "Fields of base calss"
/// <summary>
/// connecting to Database '连接数据库
/// </summary>
public static string strConn = ConfigurationManager.ConnectionStrings["GamewebDBConnectionString"].ConnectionString;
/// <summary>
/// SQL command
/// </summary>
protected static string strSQL;
#endregion
#region
/// <summary>
/// executing SQL commands ‘执行commands语句
/// </summary>
/// <param name="strSQL">string</param>
/// <returns>return int</returns>
protected static int SqlExecuteNonQuery(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn); //设置数据库连接对象
SqlCommand myCmd = new SqlCommand(strSQL, myCn); //设置Command对象
try
{
myCn.Open(); //打开数据库连接
myCmd.ExecuteNonQuery(); //执行SQL语句
return 0;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close(); //关闭数据库连接
}
}
/// <summary>
/// 返回一个数据集
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(DataSet)</returns>
protected static DataSet SqlDataSet(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
try
{
myCn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSQL, myCn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCn.Close();
}
}
protected static int SqlDataSet1(string strSQL)
{
int i;
SqlConnection myCn = new SqlConnection(strConn); //设置数据库连接对象
SqlCommand myCmd = new SqlCommand(strSQL, myCn);
myCn.Open();
i = int.Parse(myCmd.ExecuteScalar().ToString());
myCn.Close();
return i;
}
#endregion
} --------------------编程问答--------------------
那个dr 是DbDataReader,你用SQL server的话就是SqlDataReader
简单的说就是 你需要定义个一个变量,它的访问级别至少是protected 的
可以定义成string 类型,因为你定义成其他类型还是会调用ToString()方法
if(dr.Read()) 那段就是数据库操作,你只需要把数据库中的值赋值给刚刚定义的变量就可以了
前台用 <%= %> 输出变量
补充:.NET技术 , C#