可以把这段代码写进一个类文件中,代码如下:using System;namespace InfoSearch
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
const string _connectStr=" data source = localhost ; initial catalog = Northwind ; user id = sa " ; //连接数据库的参数
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public void ConnectDB(string _connectStr)
{
System.Data.SqlClient.SqlConnection sqlConnection1 ;
//string _connectStr = " data source = localhost ; initial catalog = Northwind ; user id = sa " ;
sqlConnection1  = new System.Data.SqlClient.SqlConnection ( _connectStr ) ;
sqlConnection1.Open () ;
}
}
}   
调用每次可根据环境重新设置打开连接的参数_onnectStr
或把它写进一个常量中也可以

解决方案 »

  1.   

    写一个数据库链接或操作的类,对外提供属性:链接字符串,SQL语句
    对数据操作写成公共方法,这样其他模块就可以调用了
      

  2.   

    我也不清楚,是不是用一个命名空间就可以了呢?
    比如这样:namespace System.Data.Data_open
    {
    System.Data.SqlClient.SqlConnection sqlConnection1 ;
    string _connectStr = " data source = localhost ; initial catalog = Northwind ; user id = sa " ;
    sqlConnection1  = new System.Data.SqlClient.SqlConnection ( _connectStr ) ;
    sqlConnection1.Open ( ) ;
    }
      

  3.   

    先做一个从System.Windows.Forms.Form继承的类命名为WinForm
    加入这个公共函数getConn
    然后你的窗口在从WinForm继承,每次取连接时即可调用getConn。WinForm 代码如下:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace netApplication
    {
    /// <summary>
    /// WinForm 的摘要说明。
    /// </summary>
    public class WinForm : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public WinForm()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "WinForm";
    }
    #endregion public System.Data.SqlClient.SqlConnection GetConn(){
    System.Data.SqlClient.SqlConnection sqlConnection1 ;
    string _connectStr = " data source = localhost ; initial catalog = Northwind ; user id = sa " ;
    sqlConnection1  = new System.Data.SqlClient.SqlConnection ( _connectStr ) ;
    sqlConnection1.Open ( ) ;
    return sqlConnection1;
    }
    }
    }
      

  4.   

    To learntmncyc(觅)
    我要如何在新的WinForm窗体中包涵类呢,还有我要怎么调用这个类的信息呢,多谢你了!
      

  5.   

    To ikis(FireFox)
    如果做成继承窗体也是不错的选择,但是如果我已经把我的程序做成了安装文件的话,安装在客户的机子上的话,数据库的地址肯定和开发时数据库的地址不一样的,那我又要如何改数据库的连接信息呢,多谢你了
      

  6.   

    不要试图在全面向对象的设计中加入结构化设计的思想,如果一定要达到类似全局变量或全局函数功能,用STATIC
      

  7.   

    To feels30000(船长)
    能给个这样的例子么,多谢你了
      

  8.   

    把上述类文件加入你的工程中
    然后在你的程序中调用使用以下语句:
    string strConn=" data source = localhost ; initial catalog = Northwind ; user id = sa ";
    InfoSearch.Class1 MyClass=new InfoSearch.Class1();
    MyClass.ConnectDB(strConn);这样做就可以了
      

  9.   

    还在调的地方加上
    string strConn=" data source = localhost ; initial catalog = Northwind ; user id = sa ";么,那就好象没有达到我的要求呀,这样做还是在每个窗体中加入数据库的连接的呀
      

  10.   

    To learntmncyc(觅) and  flyygf() 你们有没有这方面具体的源代码呢,能给我看看么,多谢了,我的信箱是[email protected]
      

  11.   

    app.config
    然后在程序中设置控件的属性为动态属性或者设置对象的属性为资源文件中的信息。具体方法可以参考vs.net帮助中关于xml资源文件的说明