Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        private void btn_Link_Click(object sender, EventArgs e)
        {
            bool isconnect = DataConnectManager.ConnectDataBase(txt_fuwuqi.Text, txt_uid.Text, txt_pwd.Text, cmb_database.SelectedItem.ToString());

            if (isconnect == true)
            {
                MessageBox.Show("数据库连接成功!");
            }
            else
            {
                MessageBox.Show("数据库连接失败!");
            }
        }
Ejemplo n.º 3
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();
            }
        }