【问题标题】:c# select url from database to web controlc# 从数据库中选择 url 到 web 控件
【发布时间】:2010-12-27 13:56:34
【问题描述】:

我正在尝试将 url 从数据库放入 webcontrol。
我创建了 4 个选项卡控件,每个选项卡控件由 webcontrol 组成;我想使用从数据库中获取的网址:
第一个 url 放到 tab1(浏览器在 tab) 第二个 url 放到 tab2 ...

我该怎么做?

private void Form1_Load(object sender, EventArgs e)
    {
        string MyConString = "SERVER=192.168.0.78;" +
             "DATABASE=webboard;" +
             "UID=aimja;" +
             "PASSWORD=aimjawork;" +
             "charset=utf8;";
        MySqlConnection connection = new MySqlConnection(MyConString);
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader Reader;
        command.CommandText = "SELECT  url FROM `listweb` WHERE `url` IS NOT NULL AND ( `webbordkind` = 'เว็บท้องถิ่น' ) and `nourl`= 'n' order by province, amphore limit 4 ";
        connection.Open();
        Reader = command.ExecuteReader();


        string thisrow = "";
        string sumthisrow = "";
        while (Reader.Read())
        {
            thisrow = "";
            for (int i = 0; i < Reader.FieldCount; i++)
                thisrow +=  Reader.GetValue(i).ToString();

          //  System.IO.File.AppendAllText(@"C:\file.txt", thisrow + " " + Environment.NewLine);
            sumthisrow = sumthisrow + thisrow;



        }
        connection.Close();

    }

【问题讨论】:

  • this.webBrowser2.Dock = System.Windows.Forms.DockStyle.Fill; this.webBrowser2.Location = new System.Drawing.Point(3, 3); this.webBrowser2.MinimumSize = new System.Drawing.Size(20, 20); this.webBrowser2.Name = "webBrowser2"; this.webBrowser2.Size = new System.Drawing.Size(410, 420); this.webBrowser2.TabIndex = 0; this.webBrowser2.Url = new System.Uri("msn.com", System.UriKind.Absolute); //

标签: c# database url tabcontrol web-controls


【解决方案1】:

尝试使用:

// Navigates with given WebBrowser to the given URL if it is valid.
private static void Navigate(WebBrowser webBrowser1, String address)
{
    if (String.IsNullOrEmpty(address)) return;
    if (address.Equals("about:blank")) return;
    if (!address.StartsWith("http://") &&
        !address.StartsWith("https://"))
    {
        address = "http://" + address;
    }
    try
    {
        webBrowser1.Navigate(new Uri(address));
    }
    catch (System.UriFormatException)
    {
        return;
    }
}

在您的循环之前,使用您的 4 WebBrowser 创建一个数组。 然后在循环中,创建一个名为 count 的 int,并使用类似:Navigate(WebBrowserList[count],thisrow);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    相关资源
    最近更新 更多