我正在尝试使用LInklabel打开我的访问数据库上的超链接。但是,这是第一次使用linklabel。任何建议都会很棒!
con.Open();
str = "Select * from loc where link ='" + facility+ "'";
cmd = new OleDbCommand(str, con);
dr = cmd.ExecuteReader();
if (dr.Read())
{
linkLabel1.Text = dr.GetString(17);
}
答案 0 :(得分:1)
以下代码适用于我:
private void Form1_Load(object sender, EventArgs e)
{
using (var con = new OleDbConnection())
{
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\Database1.accdb;";
con.Open();
using (var cmd = new OleDbCommand())
{
cmd.Connection = con;
cmd.CommandText = "SELECT FirstName, website FROM Clients WHERE ID = 1";
OleDbDataReader rdr = cmd.ExecuteReader();
rdr.Read();
String fName = rdr["FirstName"].ToString();
String url = rdr["website"].ToString();
if (url.Substring(0,1).Equals("#"))
{
// remove leading and trailing hash marks from URL
// as retrieved from a Hyperlink field in Access
url = url.Substring(1, url.Length - 2);
}
linkLabel1.Text = String.Format("Link to {0}'s website", fName);
linkLabel1.Links.Add(0, linkLabel1.Text.Length, url);
}
con.Close();
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string target = e.Link.LinkData as string;
System.Diagnostics.Process.Start(target);
}