class DataAccess
{
Private static string ConnectString="Data Source=localhost; InitialCatalog=the Book; Integrated Security=true ";
///& lt; summary>
///according to the name of the table to get the data sets of tables
///& lt;/summary>
///& lt; Param name="table" & gt;
///& lt; Returns>
Public static DataTable GetDataSetByTableName (string table)
{
Using (SqlConnection con=new SqlConnection (ConnectString))
{
String SQL="select * from" + "" + table;
Try
{
SqlDataAdapter adapter=new SqlDataAdapter(sql, con);
The DataSet ds=new DataSet ();
Adapter. The Fill (ds, "table");
Return the ds. Tables [0];
}
The catch (SqlException ex)
{
//exception handling
Throw new Exception (ex. Message);
}
}
}
///& lt; summary>
///according to the SQL statement to get the data object
///& lt;/summary>
///& lt; Param name="SQL" & lt;/param>
///& lt; Returns>
Public static DataTable GetDataSetBySql (string SQL)
{
Using (SqlConnection con=new SqlConnection (ConnectString))
//create a database connection object
{
SqlDataAdapter adapter=new SqlDataAdapter (SQL, con);//create the adapter object
The DataSet ds=new DataSet ();//create data collection object
Try
{
Adapter. The Fill (ds);//fill data set
Return the ds. Tables [0];//return data set
}
The catch (SqlException ex)
{
Throw new Exception (ex. Message);
}
}
}
///& lt; summary>
///according to object id value for the DataReader
///& lt;/summary>
///& lt; Param name="id" & lt;/param>
///& lt; Returns>
Public static SqlDataReader GetDataReaderByID (int id)
{
Using (SqlConnection con=new SqlConnection (ConnectString))
{
String SQL="slecet * from bookinfo where bookid=" + id;//Sql statement
Try
{
SqlCommand comm=new SqlCommand (SQL, con);//create the Command object
Con. The Open ();//open the link
SqlDataReader reader=comm. ExecuteReader ();//create the DataReader object
Reader. The Read ();//read the data
Return the reader;//return the DataReader
}
The catch (SqlException ex)
{
Throw new Exception (ex. Message);
}
}
}
///& lt; summary>
///update the data
///& lt;/summary>
///& lt; Param name="SQL" & gt;
///& lt; Returns>
Public static bool UpdataDataTable (string SQL)
{
Using (SqlConnection con=new SqlConnection (ConnectString))
{
Try
{
Con. The Open ();//open the link
SqlCommand comm=new SqlCommand (SQL, con);//create Commons object
If (comm. ExecuteNonQuery () & gt; 0)//update
{
return true;
}
The else
{
return false;
}
}
The catch (SqlException ex)
{
Throw new Exception (ex. Message);
}
}
}
///& lt; summary>
///update the database according to the data set and SQL statements
///& lt;/summary>
///& lt; Param name="ds" & lt;/param>
///& lt; Param name="SQL" & lt;/param>
Public static void UpdateDataSet (DataSet ds, string SQL)
{
Using (SqlConnection con=new SqlConnection (ConnectString))
{
Try
{
SqlDataAdapter adapter=new SqlDataAdapter(sql, con);//create an adapter
SqlCommandBuilder builder=new SqlCommandBuilder (adapter);//according to the adapter automatically generated form
Adapter. Update (ds, "table");
}
The catch (SqlException ex)
{
Throw new Exception (ex. Message);
}
}
}
}
CodePudding user response:
Without a space here?"... ; Initial Catalog =the Book; . "