设置全局变量问题
大家好,我是C#初学者:
Program.cs:
namespace SunPDA
{
public class SunGlobal
{
public static string strUser { get; set; }
public static string strAct { get; set; }
public static string strConn {get{return "Data Source=192.168.1.241
;Initial Catalog=SunDB;User Id=sa;Password=sa";}}
public static SunConnDB MyConnDB = new SunConnDB(); //方法一,
}
public class SunConnDB
{
public SqlConnection MyConn;
public SqlCommand MyCmd;
//public static SunConnDB MyConnDB = new SunConnDB(); //方法二:两句注释代码
public SunConnDB()
{
MyConn = new SqlConnection(SunGlobal.strConn);
MyCmd = new SqlCommand();
MyCmd.Connection = MyConn;
//MyConnDB = this;
}
}
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[MTAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
-------------------------
我的想法是:
strUser、strAct:在软件登录成功后,把当前用户与权限数据分别存放到这两全局属性中。
strConn:连接数据库字符串,软件其它地方要连数据库,就访问此属性。
类SunConnDB:主要是把SqlConntion、SqlCommand两控件弄个全局对象,这样在其它窗口中,就少创建此两控件对象。
现经测试,方法一、方法二,程序运行都没问题,(本人采用方法一)
请问,我这样写代码是否有不合理的地方,或理解错误?
另外:
在BCB中,SQL语句写法是:String strSql="select * from TPADBA where DBA001=" + QuotedStr(Edit1->Text);
在C#中,SqlCommand.CommandText ="select * from TPADBA where DBA001='" + textBox1.Text.Replace("'", "''") + "'";
请问,除了这种写法外,还有没有更方便的函数来实现啊?