从sql / Using中拉出来作为字符串 [英] Pull from sql / Using as string

查看:63
本文介绍了从sql / Using中拉出来作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地的sql DB。我试图拉表格表HomePage并返回并用作字符串。这是一个简单的webBrowser作为学习项目。我觉得我很接近但不确定。

这个片段是Home button_click事件。



I have a local sql DB. I am trying to pull form table HomePage and return and use as a string. This is for a simple webBrowser as a learning project. I think I am close but just not sure.
This snippet is the Home button_click event.

private void button5_Click(object sender, EventArgs e)
{
    List<String> HomePage = new List<String>();

    //SqlConnection cn = new SqlConnection(global::SimpleWebBrowser.Properties.Settings.Default.Database1ConnectionString);

    using (SqlConnection connection = new SqlConnection(global::SimpleWebBrowser.Properties.Settings.Default.Database1ConnectionString))
    {
        string query = "SELECT Tab FROM HomePage";
        using (SqlCommand command = new SqlCommand(query, connection))
        {
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    HomePage.Add(reader.GetString(0));
                }

            }

        }

    }

    var temp = HomePage.Select(q => q.FirstOrDefault()).ToArray();

    webBrowser1.Navigate(temp);


}





我刚开始学习c#约5小时前所以请告诉我。



I just started learning c# about 5 hrs ago so be gental please.

推荐答案

这是一个奇怪的方法 - 如果你只对第一项感兴趣,为什么要全部阅读?

Just读一个,然后使用它。



你需要打开连接才能使用DataReader!



另外,WebBrowser控件的Navigate方法没有重载,我知道这需要一个字符串数组 - 所以为什么要尝试传递它?



为了让将来更加明显,请尝试忘记 var 存在 - 当您明确说出您期望的变量类型时,读取代码会更加清晰。如果你没有其他原因,如果你试图将一个字符串数组分配给一个字符串变量,编译器会给你一个错误!



我可能会开始的方式这是这样的:

That's an odd approach - if you are only interested in the first item, why read them all?
Just read one, and use that.

And you do need to open the connection in order to use a DataReader!

Plus, there is no overload of the WebBrowser control's Navigate method that I know of that takes an array of strings - so why are you trying to a pass it one?

To make it more obvious in future, try to forget that var exists - it's a lot clearer to read code when you explicitly say what variable type you are expecting. If for no other reason than the compiler will give you an error if you try to assign an array of strings to a string variable!

The way I would probably start with this is like this:
    string strConnect = global::SimpleWebBrowser.Properties.Settings.Default.Database1ConnectionString;
    using (SqlConnection connection = new SqlConnection(strConnect))
    {
        connection.Open();
        string query = "SELECT Tab FROM HomePage";
        using (SqlCommand command = new SqlCommand(query, connection))
        {
            using (SqlDataReader reader = command.ExecuteReader())
            {
                if  (reader.Read())
                {
                    webBrowser1.Navigate((string) reader["Tab"]);
                }
            }
        }
    }
}





拼写失败:强为字符串[/ edit]



[edit]Spelling failure: "strong" for "string"[/edit]


感谢您清除了很多。这样做更有意义。我这样试试,我明白了。 http://pastebin.com/ysdKCTtg [ ^ ]



以下是我将代码更改为(与我相信的相同)



Thank you for clearing a lot of that up. Makes much more sense that way. I tryed this that way and I get this. http://pastebin.com/ysdKCTtg[^]

Here is what I changed my code to (same as yours i believe)

<blockquote class="quote"><div class="op">Quote:</div>        private void button5_Click(object sender, EventArgs e)
        {
            List<string> HomePage = new List<string>();

            //SqlConnection cn = new SqlConnection(global::SimpleWebBrowser.Properties.Settings.Default.Database1ConnectionString);

            string strConnect = global::SimpleWebBrowser.Properties.Settings.Default.Database1ConnectionString;
            using (SqlConnection connection = new SqlConnection(strConnect))
            {
                connection.Open();
                string query = "SELECT HomePage FROM HomePage"; //Change Select tab to HomePage as this is correct row
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            webBrowser1.Navigate((string) reader["HomePage"]); //Fixes Row name error here also.
                        }

                    }

                }

            }</string></string></blockquote>









我可以看到我只是在那个绿色的旁边,再次感谢你。现在一切正常。 + oyu很棒。





I can see were I was just out side the green on that one, Thank you again. It is all working now. +Awesome for oyu.


这篇关于从sql / Using中拉出来作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆