using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace EXAMPLE SYSTEM NAME
{
public class DatabaseAccess
{
public static SqlConnection conn;
private static SqlConnection ConnOpen()
{
if(conn == null)
{
conn = new SqlConnection(CONNECTION STRING);
}
if(conn.State!=System.Data.ConnectionState.Open)
{
conn.Open();
}
return conn;
}
public static bool Insert(string query)
{
try
{
int noofrows = 0;
SqlCommand cmb = new SqlCommand(query, ConnOpen());
noofrows = cmb.ExecuteNonQuery();
if(noofrows > 0)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
public static bool Update(string query)
{
try
{
int noofrows = 0;
SqlCommand cmb = new SqlCommand(query, ConnOpen());
noofrows = cmb.ExecuteNonQuery();
if (noofrows > 0)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
public static bool Delete(string query)
{
try
{
int noofrows = 0;
SqlCommand cmb = new SqlCommand(query, ConnOpen());
noofrows = cmb.ExecuteNonQuery();
if (noofrows > 0)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
public static DataTable Retrieve(string query)
{
try
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(query, ConnOpen());
da.Fill(dt);
return dt;
}
catch
{
return null;
}
}
}
}
------------------------
Note:
1. namespace EXAMPLE NAME SYSTEM
This name will generate automatically by C# for example:
namespace Example Management System
2. conn = new SqlConnection(CONNECTION STRING);
What you have to:
- Connect to SQL Server (2000, 2014, ...)
- on the connection dialog (contect to Server)
>>> Server Type: Database Engine
>>>Server Name:
DESKTOP-KLS1BHH\SQLEXPRESS or
.\SQLEXPRESS
this is an example server name)
>>> Authentication: Windows Authentication
- conn = new SqlConnection(CONNECTION STRING);
>>> Replace CONNECTION STRING with
@"Data Source=.\SQLEXPRESS;Initial Catalog= EXAMPLE SYSTEM NAME ;Integrated Security=True
No comments:
Post a Comment
Add your comment here.