/// <summary>
        /// 创建SqlConnection
        /// </summary>
        /// <returns></returns>
        public static SqlConnection getSqlCon()
        {
            //string sqlcon = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
            SqlConnection con = new SqlConnection(DataConnectManager.return_ConnectString());

            con.Open();
            return(con);
        }
Example #2
0
        /// <summary>
        /// 建立连接字符串
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <param name="basename"></param>
        public static bool ConnectDataBase(string fuwuqi, string uid, string pwd, string basename)
        {
            bool        isConnect = false;
            string      path      = Application.StartupPath + "\\App.config";
            XmlDocument doc       = new XmlDocument();

            doc.Load(path);
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            //nodes[0].RemoveAll();
            //doc.Save(path);
            string attrvalue = "server=" + fuwuqi + ";database=" + basename + ";uid=" + uid + ";pwd=" + pwd + ";";
            //XmlAttribute attr_name = doc.CreateAttribute("name");
            //attr_name.Value = "sqlcon";
            //nodes[0].Attributes.Append(attr_name);
            //XmlAttribute attr_connect = doc.CreateAttribute("connectionString");
            //attr_connect.Value = attrvalue;
            //nodes[0].Attributes.Append(attr_connect);
            XmlElement ele = (XmlElement)nodes[0];

            ele.SetAttribute("connectionString", attrvalue);
            doc.Save(path);
            SqlConnection con = null;

            try
            {
                con = new SqlConnection(DataConnectManager.return_ConnectString());
                con.Open();
                isConnect = true;
                return(isConnect);
            }
            catch
            {
                return(isConnect);
            }
            finally
            {
                con.Close();
            }
        }