本文介绍了如何从/向表读取和写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我16岁,我的英语不是很好。对不起!



此代码可以在富文本框中获取一些链接,然后保存txt文件中链接的内容!



i希望我的程序从sql server数据库中的表读取链接,并保存另一个表中链接的内容。记录中每个链接的内容。



你可以帮助我吗?!



每个链接我的桌子有一个id。像这样:

id adress

10200 www.test.com/test1.html

10201 www.test.com/test2.html



这是一个链接的内容:123,234,567,1,44,55,66,77



i需要第一个和第二个号码保存在一个表格中。







我的代码是:



hi i am 16 years old and my english isnt very well . excuse me !

this code can get some link in rich textbox and then save the contents of links in txt files !

i want my program read links from a table in sql server database and also save contents of links in another table . contents of every link in a record .

can u please help me ?!

every link in my table have an id . like this :
id adress
10200 www.test.com/test1.html
10201 www.test.com/test2.html

and this is content of a link : 123,234,567,1,44,55,66,77

i need first and second number to save in a table .



my code is :

public void StartThreads()
        {

            List source = new List();
            source.AddRange(this.rich1.Lines);
            Parallel.ForEach(source, new Action(this.ProcessUrl));

        }
        private void ProcessUrl(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Random random = new Random();
            using (Stream stream = System.IO.File.OpenWrite(Globals.c++ + ".txt"))

            {
                using (Stream stream2 = response.GetResponseStream())
                {
                    stream2.CopyTo(stream);
                }
            }
        }

        private void rich1_TextChanged(object sender, EventArgs e)
        {
            this.lbl1.Text = this.rich1.Lines.Count().ToString();
        }
        public static class Globals
        {
            public static int c = 1;
        }

推荐答案

string link = "123,234,567,1,44,55,66,77"



你可以使用正则表达式。有关详细信息,请参阅此链接: []



在这种情况下,这很容易正则表达式。


you can use regular expressions. See this link for more info: Regular Expressions Tutorial[^]

In this case it is a pretty easy regex.

// Add this at the top of your file
using System.Text.RegularExpressions;

int no1 = 0;
int no2 = 0;
// This expression will take the first two numbers in the string
Regex rx = new Regex(@"(?<no1>\d+)\s*,\s*(?<no2>\d+)");
Match m = rx.Match(link);
if (m.Success)
{
    no1 = int.Parse(m.Groups["no1"].Value);
    no2 = int.Parse(m.Groups["no2"].Value);
    // Now you can add these values in your data tables
}
else
{
    // Well, what do you want to do here.
}





你可以找到很多来源用于从数据库读取和写入数据。只需使用您最喜爱的搜索引擎。

以下是一些示例:



[]



[]



[]



[]



You can find many sources for reading and writing data to and from a database. Just use your favorite search engine.
Here are some examples:

Beginners guide to accessing SQL Server through C#[^]

W3 Schools - SQL Tutorial[^]

MSDN - SqlDataAdapter Class[^]

Dot Net Pearls - C# SqlDataAdapter[^]


这篇关于如何从/向表读取和写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:56