我第一次写c#.net之类的程序..
 我想在新建的类中写个数据库连接和其他的公共连接 然后在每个页面都能来访问这个数据库连接和其他的公共连接 请问怎么来写??

解决方案 »

  1.   

    public static void ConnectDB()
    {}
      

  2.   

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace Test
    {
        static class ProDatabase
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmlogin());
               
            }
            public static void ConnectDB()
               {
                   SqlConnection conn = new SqlConnection("Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=.");
                   conn.Open();
               }
        }
    }是不是这样写哦
      

  3.   

    SqlConnection 要放在ConnectDB外边定义,否则外边访问不到
      

  4.   

    可是在winform开发中好象没有app.config中哦//
      

  5.   

    public static SqlConnection CreateConnection()
    {
       return new SqlConnection("server=.;database=Database;uid=sa;pwd=;");
    }
      

  6.   

    在winform裡新建一個add.config文件
    在add.config的XML裡加上
    <appSettings>
        <add key="strConnection" value="" />
    </appSettings>
      
    value後面是你要連的數據庫
      
    再在需要調用的頁面這樣寫
    SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["strConnection"].Trim());
      

  7.   

    還有一種寫在 class裡面也行
      

  8.   

    “System.Configuration.ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
    我按照上面兄弟说的写可是报一上面的错误~~~~~请再指教!!谢谢
      

  9.   

    “System.Configuration.ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
    我按照上面兄弟说的写可是报一上面的错误~~~~~请再指教!!谢谢
      

  10.   

    怎么會出錯咧`?
      我的例子是
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
        <add key="strConnection" value="workstation id=WEIXIONG;packet size=4096;integrated security=SSPI;data source=WEIXIONG;persist security info=False;initial catalog=GoodWay" />
    </appSettings>
    </configuration>
      

  11.   

    “System.Configuration.ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
    我按照上面兄弟说的写可是报一上面的错误~~~~~请再指教!!谢谢
    =============
    这只是个旧的用法而已, 只会有个Warning, 但不会有错的
      

  12.   

    去.net C#中我發的一個帖子``上面的例子視而不見了的````
    急急~~一個C#中select語句更新數據表的問題~~大蟲幫幫 這一張
      

  13.   

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
        <add key="strConnection" value="workstation id=WEIXIONG;packet size=4096;integrated security=SSPI;data source=WEIXIONG;persist security info=False;initial catalog=GoodWay" />
    </appSettings>
    </configuration>====================            先在Reference里面添加System.Configuration, 就会有System.Configuration.ConfigurationManager的引用了            System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
                nvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
                string connectionString = nvc["strConnection"];