0% found this document useful (0 votes)
2 views

Using System

The document defines a C# class named DataConn for managing database connections using SQL Server. It includes methods for opening and closing connections, executing commands, and retrieving data as a DataSet or SqlDataReader. Error handling is implemented to display messages in case of database-related issues.

Uploaded by

dinh toan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Using System

The document defines a C# class named DataConn for managing database connections using SQL Server. It includes methods for opening and closing connections, executing commands, and retrieving data as a DataSet or SqlDataReader. Error handling is implemented to display messages in case of database-related issues.

Uploaded by

dinh toan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System.

Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Shop_Manager;

internal class DataConn


{
private static string source;

private static SqlConnection con;

private static SqlCommand cmd;

private static SqlDataAdapter da;

private static DataSet ds;

static DataConn()
{
source = "Data Source=DESKTOP-RUVC0OI;Initial Catalog=qlbh;Integrated
Security=True";
con = new SqlConnection(source);
try
{
con.Open();
}
catch (SqlException)
{
MessageBox.Show("Lỗi cơ sở dữ liệu! Hãy xem trợ giúp!");
}
}

public static void DongKetNoi()


{
cmd.Dispose();
try
{
con.Close();
}
catch (SqlException)
{
MessageBox.Show("Lỗi cơ sở dữ liệu! Hãy xem trợ giúp!");
}
finally
{
con.Dispose();
}
}

public static void ThucHienCmd(string select)


{
cmd = new SqlCommand(select, con);
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException se)
{
MessageBox.Show("Lỗi cơ sở dữ liệu! Hãy nhấn F1!");
MessageBox.Show(se.Message ?? "");
}
}

public static DataSet GrdSource(string select)


{
da = new SqlDataAdapter(select, con);
ds = new DataSet();
da.Fill(ds);
return ds;
}

public static SqlDataReader ThucHienReader(string select)


{
cmd = new SqlCommand(select, con);
try
{
return cmd.ExecuteReader();
}
catch (SqlException)
{
return null;
}
}
}

You might also like