刚学习C#,不明白的问题有不少,在这里想向各位高手请教一下:在一个系统中,如果要用到多个窗体,必须在每个窗体中都单独地建立连接数据库吗,有办法公用一个连接,在各个窗体中都可以用吗?

解决方案 »

  1.   

    朋友我刚学习.NET,有些用法不明白,可不可以请您讲得稍细一点
      

  2.   


    private sealed class sqlheaper()
    {
    #region ExecuteNonQuery public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
    {
    // Pass through the call providing null for the set of SqlParameters
    return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null);
    } public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    {
    if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" ); // Create & open a SqlConnection, and dispose of it after we are done
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    connection.Open(); // Call the overload that takes a connection in place of the connection string
    return ExecuteNonQuery(connection, commandType, commandText, commandParameters);
    }
    }
    public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
    {
    if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );
    if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( "spName" ); // If we receive parameter values, we need to figure out where they go
    if ((parameterValues != null) && (parameterValues.Length > 0)) 
    {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName); // Assign the provided values to these parameters based on parameter order
    AssignParameterValues(commandParameters, parameterValues); // Call the overload that takes an array of SqlParameters
    return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
    }
    else 
    {
    // Otherwise we can just call the SP without params
    return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
    }
    } public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)
    {
    // Pass through the call providing null for the set of SqlParameters
    return ExecuteNonQuery(connection, commandType, commandText, (SqlParameter[])null);
    }
    public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    {
    if( connection == null ) throw new ArgumentNullException( "connection" ); // Create a command and prepare it for execution
    SqlCommand cmd = new SqlCommand();
    bool mustCloseConnection = false;
    PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );
        
    // Finally, execute the command
    int retval = cmd.ExecuteNonQuery();
        
    // Detach the SqlParameters from the command object, so they can be used again
    cmd.Parameters.Clear();
    if( mustCloseConnection )
    connection.Close();
    return retval;
    }
    public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues)
    {
    if( connection == null ) throw new ArgumentNullException( "connection" );
    if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( "spName" ); // If we receive parameter values, we need to figure out where they go
    if ((parameterValues != null) && (parameterValues.Length > 0)) 
    {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName); // Assign the provided values to these parameters based on parameter order
    AssignParameterValues(commandParameters, parameterValues); // Call the overload that takes an array of SqlParameters
    return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
    }
    else 
    {
    // Otherwise we can just call the SP without params
    return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
    }
    } public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)
    {
    // Pass through the call providing null for the set of SqlParameters
    return ExecuteNonQuery(transaction, commandType, commandText, (SqlParameter[])null);
    } public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    {
    if( transaction == null ) throw new ArgumentNullException( "transaction" );
    if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" ); // Create a command and prepare it for execution
    SqlCommand cmd = new SqlCommand();
    bool mustCloseConnection = false;
    PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );
        
    // Finally, execute the command
    int retval = cmd.ExecuteNonQuery();
        
    // Detach the SqlParameters from the command object, so they can be used again
    cmd.Parameters.Clear();
    return retval;
    } public static int ExecuteNonQuery(SqlTransaction transaction, string spName, params object[] parameterValues)
    {
    if( transaction == null ) throw new ArgumentNullException( "transaction" );
    if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );
    if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( "spName" ); // If we receive parameter values, we need to figure out where they go
    if ((parameterValues != null) && (parameterValues.Length > 0)) 
    {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); // Assign the provided values to these parameters based on parameter order
    AssignParameterValues(commandParameters, parameterValues); // Call the overload that takes an array of SqlParameters
    return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
    }
    else 
    {
    // Otherwise we can just call the SP without params
    return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
    }
    } #endregion ExecuteNonQuery
    }
      

  3.   

    如果是WINFORM环境你完全可以使整个程序只使用一个后台数据库连接。无论是长连或是短连都可以。
    作一个公共类,接受请求返回结果。其实没什么复杂的。但是不建议吧数据库访问“直接”作成静态类。应该是有初始化有释放(实现IDISPOSE,实现DISPOSE方法)而且最好能可靠的释放,是个好风格。  然后在外边再作一层类,可以是静态的类或者方法。这样会有良好的使用习惯。要是WEB的化,使用一个数据库连接就比较困难而且有危险了。还是各司其职的好,其他理论同上。
      

  4.   

    可以单独写一个数据库连接的类,每个form需要链接数据库时可以定义一个实例就可以了。
      

  5.   

    写一个DBConnection.cs的类.把连接写在里面.别的地方用的话就来这调.
    例如:public static ConnectionStr="...连接字符串";
    调用:  SqlConnection myConnection = new SqlConnection(DBConnection.ConnectionStr);