Hi
I have below code . I want Videos to be read from Sql Database
C#
static async Task Main(string[] args)
{
List youtubevideoslinks = new List() { "youtube.com/watch?v=-wtIMTCHWuI",
"youtube.com/watch?v=-wtIMTCHWuI", "youtube.com/watch?v=-wtIMTCHWuI",
"youtube.com/watch?v=lalOy8Mbfdc", "youtube.com/watch?v=lalOy8Mbfdc",
"youtube.com/watch?v=lalOy8Mbfdc"};
foreach (var item in youtubevideoslinks)
{
RunVideo(item);
}
}Thanks

Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 25, 2023, 6:49 AM EST
Hello Ramco,
To get videos from database you can create a method as below:
public static List GetYouTubeVideoLinksFromDatabase(string connectionString) { List youtubevideoslinks = new List(); string query = "SELECT video_link FROM videos"; //Replace table name and column name here using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { string videoLink = reader["video_link"].ToString(); youtubevideoslinks.Add(videoLink); } reader.Close(); } return youtubevideoslinks; }Then you can use it in your code
static async Task Main(string[] args) { string connectionString = "Your SQL connection string"; //Replace you connection string. List youtubevideoslinks = GetYouTubeVideoLinksFromDatabase(connectionString); foreach (var item in youtubevideoslinks) { RunVideo(item); } }Thanks
Naimish Makwana
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 25, 2023, 10:21 AM EST
Hi Ramco,
You can modify the code as follows:
1. Create a SQL connection and query the database to retrieve the video links. Here's an example SQL query:
SELECT VideoLink FROM VideosTable
2. Execute the query and retrieve the video links using a SqlDataReader.
3. Replace the hardcoded youtubevideoslinks list with the retrieved video links.
Here's the modified code:
static async Task Main(string[] args) { List youtubevideoslinks = new List(); // Create SQL connection using (SqlConnection connection = new SqlConnection(connectionString)) { // Open connection await connection.OpenAsync(); // Create SQL command using (SqlCommand command = new SqlCommand("SELECT VideoLink FROM VideosTable", connection)) { // Execute SQL command and retrieve data using SqlDataReader using (SqlDataReader reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { string videoLink = reader.GetString(0); youtubevideoslinks.Add(videoLink); } } } } // Run videos foreach (var item in youtubevideoslinks) { RunVideo(item); } }Replace the connectionString variable with the appropriate connection string for your SQL database. Also, ensure that the VideosTable and VideoLink column names match the table and column names in your database.
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 25, 2023, 9:00 AM EST
Hi Naimish
Process has exited so the requested information is not available on below line though it runs. Secondly after first video it runs some other video. I want my videos should run automatically,
static async Task Main(string[] args) { var datasource = @"JA\MSSQLSERVER_2019";//your server var database = "Videos"; //your database name var username = "sa"; //username of server to connect var password = "sa"; //password string connectionString = @"Data Source=" + datasource + ";Initial Catalog=" + database + ";Persist Security Info=True;User ID=" + username + ";Password=" + password; List youtubevideoslinks = GetYouTubeVideoLinksFromDatabase(connectionString); foreach (var item in youtubevideoslinks) { RunVideo(item); } } public static List GetYouTubeVideoLinksFromDatabase(string connectionString) { List youtubevideoslinks = new List(); string query = "SELECT videoid FROM videos"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { string videoLink = reader["videoid"].ToString(); youtubevideoslinks.Add(videoLink); } reader.Close(); } return youtubevideoslinks; } public static void RunVideo(string videoId) { string url = $"youtube.com/watch?v={videoId}"; var process = new Process(); var _process = new ProcessStartInfo(@"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", url); process.StartInfo = _process; process.Start(); Thread.Sleep(2000); process.CloseMainWindow(); process.Close(); }Thanks