Hi
I have below code & i want to save it in Sql Database
protected void MyOwnVideos()
{
StringBuilder htmlTable = new StringBuilder();
var client = new RestClient("googleapis.com/youtube/v3");
var request = new RestRequest("search", Method.GET);
request.AddParameter("part", "snippet");
request.AddParameter("type", "video");
request.AddParameter("channelId", "UC_nNoaVw"); // Replace with your actual channelId
request.AddParameter("key", "AIz8..."); // Replace with your actual API key
IRestResponse response = client.Execute(request);
// Check if the response contains data
if (response.Data == null || response.Data.items == null || response.Data.items.Count == 0)
{
Console.WriteLine("No Data Found. Please check your channelId and API key.");
return;
}
// Build the HTML table
htmlTable.Append("");
htmlTable.Append("Video Id Video Title Description Published ");
htmlTable.Append("");
foreach (var data in response.Data.items.OrderBy(x => x.snippet.publishedAt))
{
htmlTable.Append("");
htmlTable.Append($"{data.id} ");
htmlTable.Append($"{data.snippet.title} ");
htmlTable.Append($"{data.snippet.description} ");
htmlTable.Append($"{data.snippet.publishedAt} ");
htmlTable.Append(" ");
}
htmlTable.Append("");
htmlTable.Append("
");
// Output the HTML table
Console.WriteLine(htmlTable.ToString());
}
Thanks
Jignesh KumarPosted Mar 9, 2025, 10:57 AM
Just try to remove public keyword,
And check your full page code if you are missing any closing } bracket
Ramco RamcoPosted Mar 9, 2025, 9:47 AM
Hi
I have defined in Web.Config . I am getting error } expected when i write Cnn line.
Thanks
Jignesh KumarPosted Mar 9, 2025, 7:49 AM
Hello Ramco,
If you do not have SQL table to store data then need to create table with below properties,
You can store using below code,
Daniel WrightPosted Mar 9, 2025, 6:07 AM
To save the data retrieved from the YouTube API in the provided C# code snippet into a SQL database, you would typically follow these steps:
1. Set up your SQL Database: Make sure you have a SQL database set up where you can store the data. You would typically define a table structure that matches the data you want to save, in this case, the video information.
2. Establish a Connection: You need to establish a connection to your SQL database from your C# application. You can use ADO.NET with SQLClient to achieve this.
3. Prepare SQL Insert Statements: Construct SQL INSERT statements based on the data you want to save. For each video retrieved from the API, you would create an INSERT statement to store it in the database.
4. Execute the SQL Statements: Once you have your INSERT statements prepared, you can execute them using ADO.NET commands.
Here's a simplified example to illustrate the process of saving data to a SQL database in C#:
In this example:
- Replace `"Your_SQL_Connection_String"` with your actual SQL Server connection string.
- Ensure you handle exceptions and errors appropriately, and consider using parameterized queries for better security.
By executing the `SaveVideosToDatabase` method, you can save the video information from the YouTube API into your SQL database. Remember to adjust the SQL table structure and queries to match the data you want to store accurately. Let me know if you need further clarification or assistance!