none
How to create a function that takes table name as parameter ? RRS feed

  • Question

  • my code is like this,

    if(MyDB.Table_1.Any(x=>x.fieldName == "microsoft"))
    {
      //update
    }
    else
    {
      //create new
    }

    it only works on Table_1, now i have Table_2, ...

    can i make a function out of it, it can table name as parameter ?

    Thursday, August 8, 2013 10:17 PM

Answers

  • i take Northwind for example:

     var constr = @"Data Source=.;Initial Catalog=Northwind;Integrated Security=True"; 
      var MyDB = new DataContext(constr) ; 
      
     string[] allTables=MyDB.ExecuteQuery<string>("select name from sysobjects where xtype='u'").ToArray();
     foreach(string tbName in allTables) 
     { 
       //do something
     } 

    here is my blog: http://blog.csdn.net/q107770540/article/details/6127137




    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    • Edited by Tim Tang Friday, August 9, 2013 1:47 AM e
    • Marked as answer by ningji Tuesday, August 13, 2013 1:49 AM
    Friday, August 9, 2013 1:46 AM
  • Hello ningji,

    Thank you for posting in MSDN Forums.

    From your description, I notice the issue you are experiencing is that you want to get some table name as parameter.

    According to Tim Tang, if you want to a few tables, I recommend that you can write sql like as following:

    select * from sysobjects where xtype = 'u' and name in (‘table1’,'table2','table3’…)

    table1,2,3 are tables you want.

    Or you can do a filter in the loop like as following:

    foreach(string tbName in allTables) 
    { 
    if(!IsTabelNameWant(tbName))
    {
    continue;
    }
    …
    }

    I look forward to hearing from you.

    Best Regards.


    Fred Bao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ningji Tuesday, August 13, 2013 1:49 AM
    Monday, August 12, 2013 8:46 AM
    Moderator

All replies

  • i take Northwind for example:

     var constr = @"Data Source=.;Initial Catalog=Northwind;Integrated Security=True"; 
      var MyDB = new DataContext(constr) ; 
      
     string[] allTables=MyDB.ExecuteQuery<string>("select name from sysobjects where xtype='u'").ToArray();
     foreach(string tbName in allTables) 
     { 
       //do something
     } 

    here is my blog: http://blog.csdn.net/q107770540/article/details/6127137




    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    • Edited by Tim Tang Friday, August 9, 2013 1:47 AM e
    • Marked as answer by ningji Tuesday, August 13, 2013 1:49 AM
    Friday, August 9, 2013 1:46 AM
  • Hi Tim,

    Thanks for reply 1st. In my case, i just want to operate on a few tables, not all tables.

    Is there a way to do this ?

    Thanks !

    Sunday, August 11, 2013 3:23 AM
  • Hello ningji,

    Thank you for posting in MSDN Forums.

    From your description, I notice the issue you are experiencing is that you want to get some table name as parameter.

    According to Tim Tang, if you want to a few tables, I recommend that you can write sql like as following:

    select * from sysobjects where xtype = 'u' and name in (‘table1’,'table2','table3’…)

    table1,2,3 are tables you want.

    Or you can do a filter in the loop like as following:

    foreach(string tbName in allTables) 
    { 
    if(!IsTabelNameWant(tbName))
    {
    continue;
    }
    …
    }

    I look forward to hearing from you.

    Best Regards.


    Fred Bao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ningji Tuesday, August 13, 2013 1:49 AM
    Monday, August 12, 2013 8:46 AM
    Moderator
  • Hello ningji,

    Thank you for posting in MSDN Forums.

    From your description, I notice the issue you are experiencing is that you want to get some table name as parameter.

    According to Tim Tang, if you want to a few tables, I recommend that you can write sql like as following:

    select * from sysobjects where xtype = 'u' and name in (‘table1’,'table2','table3’…)

    table1,2,3 are tables you want.

    Or you can do a filter in the loop like as following:

    foreach(string tbName in allTables) 
    { 
    if(!IsTabelNameWant(tbName))
    {
    continue;
    }
    …
    }

    I look forward to hearing from you.

    Best Regards.


    Fred Bao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Fred,

    your SQL way works, :)

    i'm asking more, can i do it using linq ? started with Linq, and want to continue with it.

    Searched online a little bit, seems no one has a good answer with linq.

    Tuesday, August 13, 2013 1:45 AM
  • Hello ningji,

    Thank you for posting in MSDN Forums.

    From your description, I notice the issue you are experiencing is that you want to get some table name as parameter.

    According to Tim Tang, if you want to a few tables, I recommend that you can write sql like as following:

    select * from sysobjects where xtype = 'u' and name in (‘table1’,'table2','table3’…)

    table1,2,3 are tables you want.

    Or you can do a filter in the loop like as following:

    foreach(string tbName in allTables) 
    { 
    if(!IsTabelNameWant(tbName))
    {
    continue;
    }
    …
    }

    I look forward to hearing from you.

    Best Regards.


    Fred Bao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Fred,

    i misunderstood you, yes your way on top of Tim's should work, let me give it a try.

    Thanks ! 

    Tuesday, August 13, 2013 1:49 AM