利用資料集實現對資料庫的操作

來源:互聯網
上載者:User
資料|資料庫 /// <summary>
/// 說明: 本類主要實現對資料庫的操作(查詢|SP)
/// 建立者: 黃宗銀
/// 建立時間: 2004-12-4
/// </summary>
public class DB
{
/// <summary>
/// 從資料庫查詢資料
/// </summary>
/// <param name="Columns">查詢列名</param>
/// <param name="Target">查詢目標</param>
/// <param name="PKColumn">主鍵列名</param>
/// <param name="PKValue">主索引值</param>
/// <param name="Q">是否加引號</param>
/// <param name="strEx">異常資訊</param>
/// <returns>返回查詢結果</returns>
public static DataView GetDBData( string Columns, string Target, string PKColumn, string PKValue, bool Q, ref string strEx )
{
string strCondition = "[" + PKColumn + "] = ";
if( Q )
{
strCondition += "'" + PKValue + "'";
}
else
{
strCondition += PKValue;
}

DataSet ds = GetDBData( Columns, Target, strCondition, ref strEx );

if( strEx != string.Empty )
{
return null;
}

return ds.Tables[0].DefaultView;
}

/// <summary>
/// 從資料庫查詢資料
/// </summary>
/// <param name="Columns">查詢列名</param>
/// <param name="Target">查詢目標</param>
/// <param name="Condition">查詢條件</param>
/// <param name="strEx">異常資訊</param>
/// <returns>查詢結果</returns>
public static DataSet GetDBData( string Columns, string Target, string Condition, ref string strEx )
{
strEx = string.Empty;
string strConnect = ConfigurationSettings.AppSettings["ConnectString"];
SqlConnection sqlConnection = new SqlConnection( strConnect );
try
{
string strQuery = "SELECT " + Columns + "FROM [" + Target + "]";

if( Condition.Trim() != string.Empty )
{
strQuery += "WHERE [" + Condition;
}

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter( strQuery, sqlConnection );

DataSet ds = new DataSet();
sqlDataAdapter.Fill( ds );

sqlConnection.Close();
return ds;
}
catch( SqlException ex )
{
sqlConnection.Close();
strEx = ex.Message;
return null;
}
}

/// <summary>
/// 執行某個預存程序
/// </summary>
/// <param name="SP">預存程序名</param>
/// <param name="xsd">資料集對象</param>
/// <param name="strEx">異常資訊</param>
/// <returns>執行結果</returns>
public static void ExecSP( string SP, DataSet xsd, ref string strEx )
{
string strConnect = ConfigurationSettings.AppSettings["ConnectString"];
SqlConnection sqlConnection = new SqlConnection( strConnect );

try
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter( SP, sqlConnection );
sqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

for( int i = 0; i < xsd.Tables["IN"].Columns.Count; i++ )
{
string strColumnName = xsd.Tables["IN"].Columns[i].ColumnName;
sqlDataAdapter.SelectCommand.Parameters.Add( "@" + strColumnName, (xsd.Tables["IN"].Rows[0])[i] );
}

for( int j = 0; j < xsd.Tables["OUT"].Columns.Count; j++ )
{
string strColumnName = xsd.Tables["OUT"].Columns[j].ColumnName;
sqlDataAdapter.SelectCommand.Parameters.Add( "@" + strColumnName, xsd.Tables["OUT"].Columns[j].DataType );
sqlDataAdapter.SelectCommand.Parameters["@" + strColumnName].Direction = ParameterDirection.Output;
}

sqlDataAdapter.Fill( xsd, "OUT" );
}
catch( SqlException ex )
{
strEx = ex.Message;
}
finally
{
sqlConnection.Close();
}
}
}




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。