自己简单写了一点,其他的模块可以使用这个类里面的方法。
using System;
using System.Data;
using System.Data.SqlClient;namespace library.common
{
/// <summary>
/// JudgeData是整个图书馆评估系统的数据对象,它提供对评估数据库的访问和操作。
/// </summary>
public class JudgeData
{
//JudgeData数据对象的私有属性DSN.
//private string DSN = "data source=LIUZQ;initial catalog=Library;persist security info=False;user id=sa;Password=1;workstation id=LIUZQ;packet size=4096";
private string DSN = "Webconfig"; /// <summary>
/// 返回值为string类型,DSN是JudgeData数据对象与数据库连接时所需的Connection字符串,可写可读的属性存取器。
/// </summary>
public string Dsn
{
get{return DSN;}
set{DSN = value;}
}
/// <summary>
/// JudgeData数据对象的构造函数
/// </summary>
public JudgeData()
{
DSN = System.Configuration.ConfigurationSettings.AppSettings["DSN"].ToString();
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 从数据库中根据传入的Sql语句填充一个数据集合。
/// </summary>
/// <param name="Sql">需要的参数Sql是字符串类型,是一条SQL语句</param>
/// <returns>返回值为DataSet类型</returns>
public DataSet showDataSet(String Sql)
{
DataSet dataSet1 = new DataSet();
    SqlDataAdapter dataAdapter1;
SqlConnection conn1 = new SqlConnection(DSN);

dataAdapter1= new SqlDataAdapter(Sql,conn1);
dataAdapter1.Fill(dataSet1,"table1"); return dataSet1;
}
/// <summary>
/// 从数据库中根据传入的Sql语句填充一个数据集合,返回的是该集合中表的数据视图。
/// </summary>
/// <param name="Sql">需要的参数Sql是字符串类型,是一条SQL语句</param>
/// <returns>返回值为DataView类型</returns>
public DataView showDataView(string Sql)
{
DataSet dataSet1 = new DataSet();
SqlDataAdapter dataAdapter1;
SqlConnection conn1 = new SqlConnection(DSN); dataAdapter1= new SqlDataAdapter(Sql,conn1);
dataAdapter1.Fill(dataSet1,"table1"); return dataSet1.Tables["table1"].DefaultView;
}
/// <summary>
/// 从数据库中根据传入的Sql语句填充一个数据集合,返回的是该集合中表的第一行的数据。
/// </summary>
/// <param name="Sql">需要的参数Sql是字符串类型,是一条SQL语句</param>
/// <returns>返回值为DataRow类型</returns>
public DataRow showDataRow(string Sql)
{
DataRow myDatarow;
DataSet dataSet1 = new DataSet();
SqlDataAdapter dataAdapter1;
SqlConnection conn1 = new SqlConnection(DSN);

dataAdapter1= new SqlDataAdapter(Sql,conn1);
dataAdapter1.Fill(dataSet1,"table1"); try
{
myDatarow = dataSet1.Tables["table1"].Rows[0];
}
catch
{
myDatarow = null;
}
return myDatarow;
}
/// <summary>
/// 从数据库中根据传入的Sql语句填充一个数据集合,返回的是该集合中表的所有行的数据。
/// </summary>
/// <param name="Sql">需要的参数Sql是字符串类型,是一条SQL语句</param>
/// <returns>返回值为DataRowCollection类型</returns>
public DataRowCollection showDataRows(string Sql)
{
DataSet dataSet1 = new DataSet();
SqlDataAdapter dataAdapter1;
SqlConnection conn1 = new SqlConnection(DSN); dataAdapter1= new SqlDataAdapter(Sql,conn1);
dataAdapter1.Fill(dataSet1,"table1"); return dataSet1.Tables["table1"].Rows;
}
/// <summary>
/// 从数据库中根据传入的Sql语句填充一个数据集合,返回的是该集合中表集合中的第一个表。
/// </summary>
/// <param name="Sql">需要的参数Sql是字符串类型,是一条SQL语句</param>
/// <returns>返回值为DataTable类型</returns>
public DataTable showDataTable(string Sql)
{
DataSet dataSet1 = new DataSet();
SqlDataAdapter dataAdapter1;
SqlConnection conn1 = new SqlConnection(DSN); dataAdapter1= new SqlDataAdapter(Sql,conn1);
dataAdapter1.Fill(dataSet1,"table1"); return dataSet1.Tables["table1"];
}
/// <summary>
/// 根据传入的StrSql语句,执行此命令
/// </summary>
/// <param name="StrSql">传入sql语句</param>
public void ExecuSql(string StrSql)
{
SqlConnection conn1;
DataSet dataSet1;
SqlDataAdapter dataAdapter1;
conn1 = new SqlConnection(DSN);
dataSet1 = new DataSet() ;
dataAdapter1 = new SqlDataAdapter(StrSql,conn1);
dataAdapter1.Fill(dataSet1,"table1");
}
/// <summary>
/// 根据传入的StrSql语句,执行此命令
/// </summary>
/// <param name="StrSql">传入sql语句</param>
public void UpData(string StrSql)
{
SqlConnection conn1;
SqlCommand MyCommand;
MyCommand =new SqlCommand();
conn1 =new SqlConnection(DSN);
conn1.Open();
MyCommand.Connection = conn1;
MyCommand.CommandText=StrSql;
MyCommand.ExecuteNonQuery ();
} }
}

解决方案 »

  1.   

    using System;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;namespace Synx.Modules.Data
    {
    /// <summary>
    /// DbObject is the class from which all classes in the Data Services
    /// Tier inherit. The core functionality of establishing a connection
    /// with the database and executing simple stored procedures is also
    /// provided by this base class.
    /// </summary>
    public abstract class DbObject :System.MarshalByRefObject
    {
    protected SqlConnection Connection;
    private string connectionString; /// <summary>
    /// A parameterized constructor, it allows us to take a connection
    /// string as a constructor argument, automatically instantiating
    /// a new connection.
    /// </summary>
    public DbObject()
    {
    ModuleSettings connectionSetting = ModuleConfig.GetSettings();
    //connectionString = connectionSetting.ConnectionString;
    connectionString = "data source=202.115.22.159;initial catalog=Synx;password=jiaowojuanmao;persist security info=True;user id=sa;workstation id=UESTC-SYNX;packet size=4096";
    Connection = new SqlConnection( connectionString );
    } /// <summary>
    /// Protected property that exposes the connection string
    /// to inheriting classes. Read-Only.
    /// </summary>
    protected string ConnectionString
    {
    get 
    {
    return connectionString;
    }
    } /// <summary>
    /// Private routine allowed only by this base class, it automates the task
    /// of building a SqlCommand object designed to obtain a return value from
    /// the stored procedure.
    /// </summary>
    /// <param name="storedProcName">Name of the stored procedure in the DB, eg. sp_DoTask</param>
    /// <param name="parameters">Array of IDataParameter objects containing parameters to the stored proc</param>
    /// <returns>Newly instantiated SqlCommand instance</returns>
    private SqlCommand BuildIntCommand(string storedProcName, IDataParameter[] parameters)
    {
    SqlCommand command = BuildQueryCommand( storedProcName, parameters ); command.Parameters.Add( new SqlParameter ( "ReturnValue",
    SqlDbType.Int,
    4, /* Size */
    ParameterDirection.ReturnValue,
    false, /* is nullable */
    0, /* byte precision */
    0, /* byte scale */
    string.Empty,
    DataRowVersion.Default,
    null )); return command;
    }
    /// <summary>
    /// Builds a SqlCommand designed to return a SqlDataReader, and not
    /// an actual integer value.
    /// </summary>
    /// <param name="storedProcName">Name of the stored procedure</param>
    /// <param name="parameters">Array of IDataParameter objects</param>
    /// <returns></returns>
    private SqlCommand BuildQueryCommand(string storedProcName, IDataParameter[] parameters)
    {
    SqlCommand command = new SqlCommand( storedProcName, Connection );
    command.CommandType = CommandType.StoredProcedure; foreach (SqlParameter parameter in parameters)
    {
    command.Parameters.Add( parameter );
    } return command; } /// <summary>
    /// Runs a stored procedure, can only be called by those classes deriving
    /// from this base. It returns an integer indicating the return value of the
    /// stored procedure, and also returns the value of the RowsAffected aspect
    /// of the stored procedure that is returned by the ExecuteNonQuery method.
    /// </summary>
    /// <param name="storedProcName">Name of the stored procedure</param>
    /// <param name="parameters">Array of IDataParameter objects</param>
    /// <param name="rowsAffected">Number of rows affected by the stored procedure.</param>
    /// <returns>An integer indicating return value of the stored procedure</returns>
    protected int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
    {
    int result = 0; Connection.Open();
    SqlCommand command = BuildIntCommand( storedProcName, parameters );
    rowsAffected = command.ExecuteNonQuery();
    result = (int)command.Parameters["ReturnValue"].Value;

    Connection.Close();
    return result;
    } /// <summary>
    /// Will run a stored procedure, can only be called by those classes deriving
    /// from this base. It returns a SqlDataReader containing the result of the stored
    /// procedure.
    /// </summary>
    /// <param name="storedProcName">Name of the stored procedure</param>
    /// <param name="parameters">Array of parameters to be passed to the procedure</param>
    /// <returns>A newly instantiated SqlDataReader object</returns>
    protected SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )
    {
    SqlDataReader returnReader; Connection.Open();
    SqlCommand command = BuildQueryCommand( storedProcName, parameters );
    command.CommandType = CommandType.StoredProcedure; returnReader = command.ExecuteReader();
    //Connection.Close();
    return returnReader;
    } /// <summary>
    /// Creates a DataSet by running the stored procedure and placing the results
    /// of the query/proc into the given tablename.
    /// </summary>
    /// <param name="storedProcName"></param>
    /// <param name="parameters"></param>
    /// <param name="tableName"></param>
    /// <returns></returns>
    protected DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )
    {
    DataSet dataSet = new DataSet();
    Connection.Open();
    SqlDataAdapter sqlDA = new SqlDataAdapter();
    try
    {
    sqlDA.SelectCommand = BuildQueryCommand( storedProcName, parameters );
    sqlDA.Fill( dataSet, tableName );
    }
    catch(Exception e)
    {
    MessageBox.Show(e.Message);
    }

    Connection.Close(); return dataSet;
    } /// <summary>
    /// Takes an -existing- dataset and fills the given table name with the results
    /// of the stored procedure.
    /// </summary>
    /// <param name="storedProcName"></param>
    /// <param name="parameters"></param>
    /// <param name="dataSet"></param>
    /// <param name="tableName"></param>
    /// <returns></returns>
    protected void RunProcedure(string storedProcName, IDataParameter[] parameters, DataSet dataSet, string tableName )
    {
    Connection.Open();
    SqlDataAdapter sqlDA = new SqlDataAdapter();
    sqlDA.SelectCommand = BuildIntCommand( storedProcName, parameters );
    sqlDA.Fill( dataSet, tableName );
    Connection.Close();
    }
    }
    }
      

  2.   

    Microsoft Application Blocks for .NET
      

  3.   

    Microsoft Application Blocks for .NET?有下载吗