我想從使用twitter流api的C#控制檯應用程序中將一些數據保存到SQL Server中。我試圖以正常儲存保存數據,但沒有奏效。任何人都可以請建議我應該使用哪種方式來存儲流數據?在SQL Server 2012中保存流數據
這是我的C#代碼。謝謝你在前進
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();
}
在數據庫中,我有數據類型
Tweet = varchar(150)
Latitude = float
Longitude = float
[SQL注入警報(http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你應該** **不串連在一起您的SQL語句 - 使用**參數化查詢**來代替以避免SQL注入 – 2014-12-07 10:43:47