简体   繁体   English

在SQL Server 2012中保存流数据

[英]Saving Streaming Data in SQL Server 2012

I am trying to save some data into SQL Server from a C# console application using twitter streaming api. 我正在尝试使用Twitter流API将一些数据从C#控制台应用程序保存到SQL Server中。 I have tried to save the data in normal saving but it didn't worked. 我试图将数据保存为普通保存,但没有用。 Can anyone please suggest which way should I use to store streaming data? 谁能建议我应使用哪种方式存储流数据?

Here is my C# code. 这是我的C#代码。 Thank you in advance 先感谢您

    static void Main(string[] args)
    {
        TwitterCredentials.SetCredentials("xxxx,xxxx,xxxx,xxxx");
        Stream_FilteredStreamExample();
    }

    private static void Stream_FilteredStreamExample()
    {
        SqlConnection conn = new SqlConnection(@"Data Source=xxxx;Initial Catalog=Surveillance;Integrated Security=True");
        conn.Open();

        var stream = Stream.CreateFilteredStream();
        stream.AddTrack("ebola");

        stream.MatchingTweetAndLocationReceived += (sender, args) =>
        {
            if (!args.Tweet.IsRetweet)
            {
                var tweet = args.Tweet;
                if(args.Tweet.Coordinates!=null)
                {
                    SqlCommand myCommand = new SqlCommand("INSERT INTO TwitterDatabase(Tweet,Latitude,Longitude) values('"+tweet.Text+"','"+tweet.Coordinates.Latitude+"','"+tweet.Coordinates.Longitude+"')",conn);
                    Console.WriteLine("tweets:{0}", tweet.Text);
                    Console.WriteLine("(Coardinates{0}, {1})", tweet.Coordinates.Latitude, tweet.Coordinates.Longitude);
                 }
            }
        };

        stream.StartStreamMatchingAnyCondition();
        conn.Close();
    }

In Database i have datatype 在数据库中,我有数据类型

Tweet = varchar(150)

Latitude = float

Longitude = float

Your connection is closed when you try to save a tweet. 当您尝试保存推文时,您的连接已关闭。 Open the connection and than save the tweet in your event and execute the command: 打开连接,然后在您的事件中保存推文并执行命令:

private static void Stream_FilteredStreamExample()
{
    var stream = Stream.CreateFilteredStream();
    stream.AddTrack("ebola");
    stream.MatchingTweetAndLocationReceived += (sender, args) =>
    {
        if (!args.Tweet.IsRetweet)
        {
            using (SqlConnection conn = new SqlConnection(@"Data Source=xxxx;Initial Catalog=Surveillance;Integrated Security=True"))
            {
            conn.Open();
            var tweet = args.Tweet;
            if(args.Tweet.Coordinates!=null)
            {
                using (SqlCommand myCommand = new SqlCommand("INSERT INTO TwitterDatabase(Tweet,Latitude,Longitude) values '"+tweet.Text+"','"+tweet.Coordinates.Latitude+"','"+tweet.Coordinates.Longitude+"')",conn))
                {
                    myCommand.ExecuteNonQuery();
                }
             }
             }
        }
    };
  stream.StartStreamMatchingAnyCondition();
}

You realy have to look at parameterized queries. 您确实必须查看参数化查询。 You are now exposed to sql injecting. 现在,您可以使用sql注入了。

The data i am trying to store is an structured data so for storing into database the below code worked for me. 我要存储的数据是结构化数据,因此以下代码对我有用,以便将其存储到数据库中。

stream.MatchingTweetAndLocationReceived += (sender, args) =>
        {
            if (!args.Tweet.IsRetweet)
            {
                using ( SqlConnection conn = new SqlConnection(@"Data Source=WIN-PAL1Q8DR163\AVINASH;Initial Catalog=Surveillance;Integrated Security=True"))
                {
                conn.Open();
                var tweet = args.Tweet;
                if (args.Tweet.Coordinates != null)
                {
                    try
                    {
                        using (SqlCommand myCommand = new SqlCommand("INSERT INTO TwitterDatabase(Tweet,Latitude,Longitude) VALUES(@Tweeets, @LatCoordinate, @LongCoordinate)", conn))
                        {
                            myCommand.Parameters.Add(new SqlParameter("Tweeets", tweet.Text));
                            myCommand.Parameters.Add(new SqlParameter("LatCoordinate", tweet.Coordinates.Latitude));
                            myCommand.Parameters.Add(new SqlParameter("LongCoordinate", tweet.Coordinates.Longitude));
                            myCommand.ExecuteNonQuery();
                        }
                    }catch{
                        Console.WriteLine("Could not insert.");
                    }
                }

            }
    }

        };
    stream.StartStreamMatchingAnyCondition();

}

 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM