写了一个数据库系统,已经搞好了,就用c#写的 是vs2008的链接字符串呢是 sa登陆:     
"Data Source=.;Initial Catalog=数据库;User ID=sa,pwd=;"; windows登陆
Data Source=.;Initial Catalog=数据库;Integrated Security=True
这个样子的,没问题了然后呢 我这样想的,就是用户使用的时候呢,第一步:选择是sa登陆还是windows登陆第二步:假入是sa 就填写自己的数据库名和用户名和密码 ;假如是windows 就填写数据库名字即可第三步:做好上述判断以后,就连上数据库了,以后面的所有操作都不要再连接数据库了
-----------------------------------------------------------------------------------------------所以想请高手给一个教程啥的(当然上面的代码我都会写了)1、这几个东西是不是要保存到一个什么全局的变量里面去,如果是,请问保存到哪里?后面的窗体如何调用它的这个链接字符串代码?(这里的一些小代码请提供下)2、或者是不是要保存到一个什么ini的配置文件里去?这个我更加不明白了,请高手给一个相关教程,给点代码也好,实在感谢!

解决方案 »

  1.   

    直接配置web.config/// <summary>
            /// 修改web.config的<connectionStrings>章节
            /// </summary>
            /// <returns></returns>
            private bool WriteConWebConfig()
            {
                System.IO.FileInfo FileInfo = new System.IO.FileInfo(Setupdir + "/web.config");
                if (!FileInfo.Exists)
                {
                    throw new InstallException("Missing config file :" + Setupdir + "/web.config");
                }
                System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
                xmlDocument.Load(FileInfo.FullName);
                bool FoundIt = false;
                foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
                {
                    if (Node.Name == "add")
                    {
                        if (Node.Attributes.GetNamedItem("name").Value == "StandardLMS_ConnectionString")
                        {
                            if (AdminName != "" && AdminPwd != "")
                            {
                                Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Persist Security Info=False;Data Source={0};database={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", ServerName, DBName, AdminName, AdminPwd);
                            }
                            else
                            {
                                Node.Attributes.GetNamedItem("connectionString").Value = "Server=" + ServerName + ";Database=" + DBName + ";Integrated Security=true";
                            }
                            FoundIt = true;
                        }
                    }
                }
                if (!FoundIt)
                {
                    throw new InstallException("Error when writing the config web.config");
                }
                xmlDocument.Save(FileInfo.FullName);
                return FoundIt;
            }
      

  2.   

    哥啊 我不是asp.net啊 我就是c#啊2楼的哥
      

  3.   

    那就自定义一个XML文件 来存储
      

  4.   

    全局静态变量:public static string connstring="";调用:类名.connstring
      

  5.   

    app.config
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      config.AppSettings.Settings.Remove(key);
      config.AppSettings.Settings.Add(key, value);
      config.Save(ConfigurationSaveMode.Modified);
      ConfigurationManager.RefreshSection("appSettings");  
    或XML方法
    string configFileName = Application.ExecutablePath + ".config";  
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();  
    doc.Load(configFileName);  
    string configString = @"[@name='appSetting1']/value";  
    System.Xml.XmlNode configNode = doc.SelectSingleNode(configString);  
    if (configNode != null)  
    {  
        
    }