1

Using MS Visual Studio 2012 Express, Using C#.NET 4.0.

Hi guys, This I believe is a simple one.

I've been rushing a program at the request of my boss, But now it turns out that I have a lot more free time on it.

So I'm going through the code trying to make it more compact, cleaner etc.

I have this one function.....

public void RunMonth()
{
    //** top 10 listings*********************//            
    SetTxtBox1(DateTime.Now.ToString() + " ==== END OF MONTH REPORTS starting.........\r\n");
    SetTxtBox1(" **************************************************************************************\r\n");
    try
    {
        VRMtableDESC = querys.TopVRM("DESC");
        SetTxtBox1(DateTime.Now.ToString() + " ==== VRM Top has been loaded\r\n");
    }
    catch
    {
        SetTxtBox1(DateTime.Now.ToString() + " ==== VRM Top has FAILED\r\n");
    }
}

"VRMtableDESC" is a datatable, one of around 18 datatables. for each datatable this try statement is run.

Now as you can tell this currently is repeated 18 times for every table. instead i would like to loop into the tables instead and fill them with there data.

is there a way to do this? i would need some sort of collection for the datatables and the query.function calls too as far as I'm aware.

So far i have hit a blank so im hoping there is something im missing.

Thanks in Advance

::::UPDATE:::: Ok so I have this set up so far....

 foreach (DataTable tbl in MyDataSet.Tables)
 {
     tbl.TableName.Equals(querys.TopVRM(""));
 }

the equals method needs to be dynamic in its method selection too.... any ideas?

::UPDATE(31/10/12)::

Hi again, Im getting an error, the common error " object not sent to instance of object"

it apears on the merge in the foreach loop.

I have debugged my query call and looks fine.

 var tables = new SortedDictionary<string, Func<DataTable>>()
        {
            {"VRMtableDESC",  () => querys.TopVRM("DESC")},
            {"VRMtableASC",   () => querys.BotVRM("ASC")},
            {"SpectableDESC", () => querys.TopSpec("DESC")},
            {"SpectableASC",  () => querys.botSpec("ASC")},
            {"ParttableDESC", () => querys.TopPart("DESC")},
            {"ParttableASC", () => querys.BotPart("ASC")},
            {"MantableDESC",  () => querys.TopManual("DESC")},
            {"MantableASC",   () => querys.BotMan("ASC")}, //why is this first to run?????
            {"UsersLockedTbl",() => querys.UserLocked()},
            {"NewUsersTbl",   () => querys.NewUsers()},

            {"VRMtotaltblTOP",   () => querys.VRMtotalTOP("")},
            {"PARTtotaltblTOP",  () => querys.PARTtotalTOP("")},
            {"SPECtotaltblTOP",  () => querys.SPECtotalTOP("")},
            {"MANtotaltblTOP",   () => querys.MANtotalTOP("")},
            {"VRMtotaltblBOT",   () => querys.VRMtotalBOT("")},
            {"PARTtotaltblBOT",  () => querys.PARTtotalBOT("")},
            {"SPECtotaltblBOT",  () => querys.SPECtotalBOT("")},
            {"MANtotaltblBOT",   () => querys.MANtotalBOT("")},
        };

        foreach(var kvp in tables)
        {
            try
            {
                MyDataSet.Tables[kvp.Key].Merge(kvp.Value());//error occurs here
                SetTxtBox1(String.Format("{0} ==== {1} has been Loaded\r\n", DateTime.Now.ToString(), kvp.Key));
            }catch(Exception e)
            {
                SetTxtBox1(String.Format("{0} ==== {1} has FAILED\r\n", DateTime.Now.ToString(), kvp.Key));
                MessageBox.Show("error:::" + e);
            }
        }
        AutoRunApp();  

any help would be great.

UPDATE::

heres my query call from the "bunchofquerys" class

 public DataTable BotMan(string order)
    {

            SqlConnection conn = new SqlConnection(myConnString32);
            SqlCommand vrm = new SqlCommand();
            //BindingSource bindme = new BindingSource();
            SqlDataAdapter adapt1 = new SqlDataAdapter(vrm);
           // DataSet dataSet1 = new DataSet();
            DataTable table1 = new DataTable();
            try
            {
            vrm.Connection = conn;
            vrm.CommandType = CommandType.StoredProcedure;
            if (order.Equals("DESC"))
            {
                vrm.CommandText = "dbo.TopManual";
            }
            vrm.CommandText = "dbo.BotManual";
            vrm.CommandTimeout = 360;

            vrm.Parameters.AddWithValue("@OrderBy", order);
            adapt1.Fill(table1);
            return table1;
        }
        catch (Exception e)
        {
            MessageBox.Show("eeror::zomg:::  " + e);

        }
        return table1;
    }
4

1 回答 1

2

你说你已经有一个DataSet. 填写所有表格的一种简单方法是使用Merge.

在此示例中,我在表名(中的表名DataSet)和相应的查询函数(假设它们返回DataTable具有相似模式的 a,并且您DataSet的名称为 named ds)之间创建了一个映射。

Key用于从 中查询适当DataTableDataSet,然后该方法Merge用于“填充” DataTable

var tables = new SortedDictionary<string, Func<DataTable>>() //or use List of Tuples... 
{
    {"VRM_TOP",    () => querys.TopVRM("DESC")},
    {"FooBar_TOP", () => querys.FooBar("DESC")},
    {"Something",  () => querys.Something("ASC")},
};

foreach(var kvp in tables)
    ds.Tables[kvp.Key].Merge(kvp.Value());

你可以反过来做,遍历你的每个表,DataSet并通过表的TableName.

现在可以轻松地将您的try/catch块添加到foreach循环中。

foreach(var kvp in tables)
{
    try
    {
        ds.Tables[kvp.Key].Merge(kvp.Value());
        SetTxtBox1(String.Format("{0} ==== {1} has ben loaded\r\n", DateTime.Now, kvp.Key));
    }
    ...
}
于 2012-10-30T13:25:49.057 回答