1

我正在尝试使用 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);
        }
4

1 回答 1

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);
}
于 2013-10-23T17:34:08.433 回答