none
Adding values that doesn't exist in object RRS feed

  • Question

  • Hi Experts,

    Am still on track to learn C#. Could you please help with below problem. 

    I have configuration app.config that contains comma-separated values which are basically server_names

    Example 

    Config_Values --> Server_1, Server_2

    I will be using quartz library to execute every 30 second to read a table from a DB which contains data in below format is in DB using Dapper or Ado.net.

    Data Set from DB

    Query_ID	Query	Database_Name
    1	 SELECT * FROM XYZ	Master
    2	 SELECT * FROM dbo.ABC	Master
    

    Problem 1 : For every server read from the config file I need to map them together.

    Now, I need to load the return data set into an object with below format "Problem"

    Query_ID	Query	Database_Name	Server_Name
    1	 SELECT * FROM XYZ	Master	Server_1
    2	 SELECT * FROM dbo.ABC	Master	Server_1
    1	 SELECT * FROM XYZ	Master	Server_2
    2	 SELECT * FROM dbo.ABC	Master	Server_2

    Now as am doing query to DB every 30 seconds there could be possibility new Query_ID is available to be loaded in the object

    Problem 2 : Add only those Query_ID + Server_Name combination to the object which are new so final result something like below

    Read data from DB again (I will be able to handle it using Quartz)

    Query_ID Query Database_Name

    Query_ID	Query	Database_Name
    1	SELECT * FROM XYZ	Master
    2	SELECT * FROM dbo.ABC	Master
    3	SELECT * FROM dbo.Emp	AdventureWorks



    Problem load the existing object with only new Query_ID + Server_Name combination

    Query_ID	Query	Database_Name	Server_Name
    1	SELECT * FROM XYZ	Master	Server_1
    2	SELECT * FROM dbo.ABC	Master	Server_1
    1	SELECT * FROM XYZ	Master	Server_2
    2	SELECT * FROM dbo.ABC	Master	Server_2
    3	SELECT * FROM dbo.Emp	AdventureWorks	Server_1
    3	SELECT * FROM dbo.Emp	AdventureWorks	Server_2
    



    Kindly help

    Thanks

    Priya



    • Edited by Priya Bange Sunday, March 1, 2020 5:50 AM .
    Sunday, March 1, 2020 5:46 AM

Answers

  • 1- add a tag with key and value pair to your web.config file
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>   
         
        <system.web>
          <compilation defaultLanguage="c#" debug="true" />
        </system.web>
        
        <appSettings>
          <add key="ConnectionInfo" value="server1,server2,server3" />
    	.
    	.
        </appSettings>
     
    </configuration>
    
    
    add the reference for configuration to your app follow the link
    https://www.aspsnippets.com/Articles/Read-Get-AppSettings-Key-Value-from-WebConfig-file-in-ASPNet-using-C-and-VBNet.aspx
    
    
    
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.Configuration;
    
    public partial class _Default : Page
    {
        public string Getservers    {
            get
            {
                // Returns the title string.
                return WebConfigurationManager.AppSettings["ConnectionInfo"];
            }
        }
    
         protected void Page_Load(object sender, EventArgs e)
        {
            // Access the properties (and thus Web.config) like this.
            string Text = this.Getservers
    	string[] serverarray = Text.split(",");
    	for(int I =0;i< serverarray.length;i++)
    	{
             var ser = serverarray[I]; //here can get all the serverrs
    	}
        }
    }
    
    
    

    • Marked as answer by Priya Bange Wednesday, March 4, 2020 6:43 AM
    Monday, March 2, 2020 12:26 PM

All replies

  • Why do you need these in a object?  Why can't you just do one "for" loop for the items in the Config_Values list, and then for each line in your database, do the query?

    And have you really thought about what you're asking?  Do you really want to do all of those SQL queries every 30 seconds?  SQL queries often take time.  You're going to burden all of those servers and the network between them.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Sunday, March 1, 2020 7:46 AM
  • Hello,

    Can you describe the underlying logic for reading data every thirty seconds without mentioning anything related to code, in other words, what is the business requirements.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 1, 2020 4:01 PM
    Moderator

  • Now as am doing query to DB every 30 seconds there could be possibility new Query_ID is available to be loaded in the object

    Problem 2 : Add only those Query_ID + Server_Name combination to the object which are new so final result something like below

    Read data from DB again (I will be able to handle it using Quartz)

    Query_ID Query Database_Name

    Query_ID	Query	Database_Name
    1	SELECT * FROM XYZ	Master
    2	SELECT * FROM dbo.ABC	Master
    3	SELECT * FROM dbo.Emp	AdventureWorks

    Hi Priya,

    Thank you for posting here.

    I don't quite understand these contents, could you please describe them again?

    I don't know where to start until I understand what you mean.

    Looking forward to your reply.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 2, 2020 7:03 AM
  • 1- add a tag with key and value pair to your web.config file
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>   
         
        <system.web>
          <compilation defaultLanguage="c#" debug="true" />
        </system.web>
        
        <appSettings>
          <add key="ConnectionInfo" value="server1,server2,server3" />
    	.
    	.
        </appSettings>
     
    </configuration>
    
    
    add the reference for configuration to your app follow the link
    https://www.aspsnippets.com/Articles/Read-Get-AppSettings-Key-Value-from-WebConfig-file-in-ASPNet-using-C-and-VBNet.aspx
    
    
    
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.Configuration;
    
    public partial class _Default : Page
    {
        public string Getservers    {
            get
            {
                // Returns the title string.
                return WebConfigurationManager.AppSettings["ConnectionInfo"];
            }
        }
    
         protected void Page_Load(object sender, EventArgs e)
        {
            // Access the properties (and thus Web.config) like this.
            string Text = this.Getservers
    	string[] serverarray = Text.split(",");
    	for(int I =0;i< serverarray.length;i++)
    	{
             var ser = serverarray[I]; //here can get all the serverrs
    	}
        }
    }
    
    
    

    • Marked as answer by Priya Bange Wednesday, March 4, 2020 6:43 AM
    Monday, March 2, 2020 12:26 PM

  • Now as am doing query to DB every 30 seconds there could be possibility new Query_ID is available to be loaded in the object

    Problem 2 : Add only those Query_ID + Server_Name combination to the object which are new so final result something like below

    Read data from DB again (I will be able to handle it using Quartz)

    Query_ID Query Database_Name

    Query_ID	Query	Database_Name
    1	SELECT * FROM XYZ	Master
    2	SELECT * FROM dbo.ABC	Master
    3	SELECT * FROM dbo.Emp	AdventureWorks

    Hi Priya,

    Thank you for posting here.

    I don't quite understand these contents, could you please describe them again?

    I don't know where to start until I understand what you mean.

    Looking forward to your reply.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Dear All,

    Sorry for the confusing posts , let me list out step by step

    1. Query a SQL Server Instance every 30 seconds . Am able to do this using Quartz so this is covered.

    2. Load the retrieved data into a collection. This is also done using dapper and stored procedure.

    3. Now am stuck

    1. In the configuration I have 1 to many SQL Server instances listed like below 

    "Server_Instance": [ "Server_1", "Server_2" ]

    These Servers need to be read from the configuration and for every result set that's read from SQL Server. I need to load them into another collection.

    Query_ID	Query	Database_Name	Server_Name
    1	SELECT * FROM XYZ	Master	Server_1
    2	SELECT * FROM dbo.ABC	Master	Server_1
    1	SELECT * FROM XYZ	Master	Server_2
    2	SELECT * FROM dbo.ABC	Master	Server_2
    3	SELECT * FROM dbo.Emp	AdventureWorks	Server_1
    3	SELECT * FROM dbo.Emp	AdventureWorks	Server_2

    Next time when the result is again read after 30 second if there is any new Query_ID in the DB table just add that only to existing memory collection.

    Thanks

    Priya

    Monday, March 2, 2020 3:32 PM
  • Hi Prige,

    I understand what you mean.

    You said "stuck", did something go wrong? Or does the program stop at a certain step?

    If the former, please provide exception information.

    If it is the latter, you can use breakpoints to find out that part of the code and provide it to us for testing.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 3, 2020 9:35 AM