如何遍历SQL表以设置全局变量 [英] How do I loop through SQL table to set global variables

查看:70
本文介绍了如何遍历SQL表以设置全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一张桌子tblFlags有3列UniqID,FlagName,FlagValue



此表用于存储应用程序设置,即DataPathBackup



我想要做的是循环此表以在启动时设置公共静态字符串



即公共静态字符串FlagName=FlagValue

这应该返回:public static string DataPathBackup = z:\ AppBackup \





我怎样才能实现这个目标?



我有什么尝试过:



我设置了一些默认值

ie

public static String DataPathBackup = @Z:\ AppBackup \;



然后我打电话给FillFlags()



来自下面;

j返回正确的FlagName,k返回正确的FlagValue





所以我希望有类似

公共静态字符串j=k



这将覆盖我的默认设置,如果在此表中找到它

Hi All

I have a table tblFlags with 3 columns UniqID, FlagName,FlagValue

This table is used for storing app settings ie DataPathBackup

What i would like to do is loop through this table to set public static strings upon startup

ie public static string "FlagName" = "FlagValue"
this should return : public static string DataPathBackup = z:\AppBackup\


How would i be able to achieve this?

What I have tried:

I have set some default values
ie
public static String DataPathBackup= @"Z:\AppBackup\";

then i call FillFlags()

From below;
j returns the correct FlagName and k returns the correct FlagValue


so i would like to have something like
public static string "j" = "k"

this will then overide my default setting if it was found in this table

public void FillFlags()
{
    string connectionString = null;
    SqlConnection cnn,cnn2;
    connectionString = CString;
    cnn = new SqlConnection(connectionString);
    cnn2 = new SqlConnection(connectionString);
    SqlDataReader dr,dr2;

        SqlCommand comm = new SqlCommand("SELECT [FlagName],[FlagValue] from tblFlags");

    comm.Connection = cnn;
        cnn.Open();
        dr = comm.ExecuteReader();

        while (dr.Read())
        {
            string j = dr["FlagName"].ToString();
           
            SqlCommand comm1 = new SqlCommand("SELECT [FlagValue] from tblFlags WHERE FlagName='"+ j +"'");
            comm1.Connection = cnn2;
            cnn2.Open();
            dr2 = comm1.ExecuteReader();
            MessageBox.Show(j);
            while (dr2.Read())
            {
                
                string k=dr2["FlagValue"].ToString();
                //j = k;
               
                MessageBox.Show(k);
            }
            dr2.Close();
            dr2.Dispose();
            cnn2.Close();
        }
        dr.Close();
        dr.Dispose();

        cnn.Close();
}

推荐答案

尽管可能会这样做,但这并不是一个好主意。我怀疑使用反射可以做到你想做的事情:

Although it's probably possible to do this, that doesn't make it a good idea. I suspect it's possible to do exactly what you want using reflection:
public static string MyVariable;
...
MyVariable = "Old value";
SetVariableByName("MyVariable", "New value for MyVariable");
Console.WriteLine(MyVariable);



并获得


And get

"New value for MyVariable"

打印。 (但是你必须编写这个方法,它会很复杂)



但它不好,慢,容易出错。

相反,请考虑使用词典:

printed. (But you'd have to write the method, and it would be complicated)

But it's not nice, slow, and prone to errors.
Instead, consider using a Dictionary:

public static Dictionary<string, object> MySettings = new Dictionary<string, object>();



并使用您从中检索的名称和值您的数据库设置条目:


And use the name and value you retrieve from your DB to set entries in that:

MySettings["MyVariable"] = "New value for MyVariable";

理解正在发生的事情要容易得多,而且更安全!

It's a lot easier to understand what is going on, and a lot safer!


这篇关于如何遍历SQL表以设置全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆