0% found this document useful (0 votes)
13 views1 page

Class Database

The document defines a Database class that manages SQL database connections and operations. It includes methods to execute SQL queries and non-query commands while handling exceptions. The class uses a SqlDataAdapter and SqlConnection to interact with a specified SQL Server database.

Uploaded by

buiphucnhan20gg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Class Database

The document defines a Database class that manages SQL database connections and operations. It includes methods to execute SQL queries and non-query commands while handling exceptions. The class uses a SqlDataAdapter and SqlConnection to interact with a specified SQL Server database.

Uploaded by

buiphucnhan20gg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

class Database

{
SqlDataAdapter da;
DataSet ds;
SqlConnection sqlConn;
public Database()
{
string strCnn = "Data Source=JOHNNYBUIII; Database=SV; " + "user
id=sa;password=1;MultipleActiveResultSets=True;";
sqlConn = new SqlConnection(strCnn);
}

public DataTable Execute(string sqlStr)


{
try
{
da = new SqlDataAdapter(sqlStr, sqlConn);
ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
catch (Exception ex)
{
throw new Exception("Error executing query: " + ex.Message);
}
}
public void ExecuteNonQuery(string sqlStr)
{
try
{
if (sqlConn.State != ConnectionState.Open)
sqlConn.Open();

SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);


cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception("Error executing non-query: " + ex.Message);
}
finally
{
if (sqlConn.State == ConnectionState.Open)
sqlConn.Close();
}
}
}

You might also like