Who can teach me how to make Class to store database location When want to use just call the location.
Everytime I will write again the connection. When database location is change, I will change the database location one by one. This is time consuming and difficult to maintain program.
I'm using C# Microsoft visual studio 2008 Windows Form Application
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1.PDC
{
public partial class AddProduct : Form
{
OleDbCommand sCommand;
OleDbDataAdapter sAdapter;
OleDbCommandBuilder sBuilder;
DataSet sDs;
DataTable sTable;
string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=\\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";
public AddProduct()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
OleDbConnection conAuthor;
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source= \\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";
conAuthor = new OleDbConnection(conString);
OleDbCommand cmdInsert, cmdCheckID;
OleDbDataReader dtrCheckID;
conAuthor.Open();
string strChechID = "select * from [Product] where Model='" + txtModel.Text + "'and FGrade='" + txtFGrade.Text + "' and Thk='" + txtThk.Text + "'and FSize='" + txtSize.Text + "'";
conString = "Insert Into [Product]([Model],[FGrade], [Thk], [FSize]) Values(?,?,?,?)";
cmdCheckID = new OleDbCommand(strChechID, conAuthor);
cmdInsert = new OleDbCommand(conString, conAuthor);
dtrCheckID = cmdCheckID.ExecuteReader();
if (String.IsNullOrEmpty(txtModel.Text))
{
MessageBox.Show("Model field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
if (String.IsNullOrEmpty(txtFGrade.Text))
{
MessageBox.Show("Foam Grade field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
if (String.IsNullOrEmpty(txtThk.Text))
{
MessageBox.Show("Thick field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
if (String.IsNullOrEmpty(txtSize.Text))
{
MessageBox.Show("Size field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
try
{
if (dtrCheckID.HasRows)
{
MessageBox.Show("Duplicate Data!!!!!!");
dtrCheckID.Close();
}
else
{
dtrCheckID.Close();
cmdInsert.Parameters.AddWithValue("@Model", txtFGrade.Text);
cmdInsert.Parameters.AddWithValue("@FGrade", txtFGrade.Text);
cmdInsert.Parameters.AddWithValue("@Thk", txtThk.Text);
cmdInsert.Parameters.AddWithValue("@FSize", txtSize.Text);
cmdInsert.ExecuteNonQuery();
conAuthor.Close();
txtModel.Text = "";
txtFGrade.Text = "";
txtThk.Text = "";
txtSize.Text = "";
timer1.Enabled = true;
}
}
catch (Exception)
{
//MessageBox.Show("ex.ToString()");
}
}
private void AddProduct_Load(object sender, EventArgs e)
{
string sql = "SELECT * FROM Product ORDER BY Model";
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "Product");
sTable = sDs.Tables["Product"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["Product"];
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
private void timer1_Tick(object sender, EventArgs e)
{
string sql = "SELECT * FROM Product ORDER BY Model";
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "Product");
sTable = sDs.Tables["Product"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["Product"];
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
timer1.Enabled = false;
}
private void delete_btn_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
sAdapter.Update(sTable);
}
}
private void new_btn_Click(object sender, EventArgs e)
{
dataGridView1.ReadOnly = false;
save_btn.Enabled = true;
new_btn.Enabled = false;
delete_btn.Enabled = false;
btnRefresh.Enabled = false;
btnAdd.Enabled = false;
}
private void save_btn_Click(object sender, EventArgs e)
{
sAdapter.Update(sTable);
dataGridView1.ReadOnly = true;
save_btn.Enabled = false;
new_btn.Enabled = true;
delete_btn.Enabled = true;
btnAdd.Enabled = true;
btnRefresh.Enabled = true;
}
private void btnRefresh_Click(object sender, EventArgs e)
{
string sql = "SELECT * FROM Product ORDER BY Model";
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "Product");
sTable = sDs.Tables["Product"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["Product"];
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
txtFGrade.Text = "";
txtThk.Text = "";
txtSize.Text = "";
}
}
}