patterncsharpMinor
Querying school databases
Viewed 0 times
databasesschoolquerying
Problem
This is the application I created for my personal use. I need some quality testing of this application. If you see some errors or any lengthy coding (it's already lengthy), and it needs to be shortened please tell me. I would love to learn new things; as I'm a newbie, I have made lots of mistakes here.
`using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Configuration;
namespace Table_creator
{
public partial class CreateTable : Form
{
private BindingSource bindingSource1 = new BindingSource();
public CreateTable()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string qry = "CREATE TABLE [" + txtCreateTable.Text + "] (StudentName NVARCHAR(50),ContactNo nchar(11),FathersNo nchar(11), MothersNo nchar(11), Email nvarchar(100))";
SqlCommand cmd = new SqlCommand(qry, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Success! Updated");
ListTables();
GetData();
}
catch (System.Exception ex)
{
MessageBox.Show("Failed");
txtCreateTable.Focus();
}
finally
{
con.Close();
}
}
private void ListTables()
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Op
`using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Configuration;
namespace Table_creator
{
public partial class CreateTable : Form
{
private BindingSource bindingSource1 = new BindingSource();
public CreateTable()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string qry = "CREATE TABLE [" + txtCreateTable.Text + "] (StudentName NVARCHAR(50),ContactNo nchar(11),FathersNo nchar(11), MothersNo nchar(11), Email nvarchar(100))";
SqlCommand cmd = new SqlCommand(qry, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Success! Updated");
ListTables();
GetData();
}
catch (System.Exception ex)
{
MessageBox.Show("Failed");
txtCreateTable.Focus();
}
finally
{
con.Close();
}
}
private void ListTables()
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Op
Solution
First Principal of coding is DRY (do not repeat yourself ). so if you are repeating your code , encapsulate in one method or class. and call that method using differnt paramerter as per need.
Second, Opening and disposing a connection is equally important.
I have changed only one operation.
Second, Opening and disposing a connection is equally important.
I have changed only one operation.
public class DataHelper
{
private const string ConnectionString = "PutYouConnectionString";
public static DataTable ExecuteDataSet(string sql,params SqlParameter[] parameters)
{
using (var connection = new SqlConnection(ConnectionString))
{
using (var command = new SqlCommand(sql))
{
command.Connection = connection;
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
var dbAdapter = new SqlDataAdapter(command);
var dataRecords = new DataTable();
dbAdapter.Fill(dataRecords);
return dataRecords;
}
}
}
public static void ExecuteNonQuery(string sql,params SqlParameter[] parameters)
{
using (var connection = new SqlConnection(ConnectionString))
{
using (var command = new SqlCommand(sql))
{
command.Connection = connection;
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
command.ExecuteNonQuery();
}
}
}
}
public class DataService
{
public void CreateTable(string tableName)
{
var sql = "CREATE TABLE [" + tableName +
"] (StudentName NVARCHAR(50),ContactNo nchar(11),FathersNo nchar(11), MothersNo nchar(11), Email nvarchar(100))";
DataHelper.ExecuteNonQuery(sql);
}
}Code Snippets
public class DataHelper
{
private const string ConnectionString = "PutYouConnectionString";
public static DataTable ExecuteDataSet(string sql,params SqlParameter[] parameters)
{
using (var connection = new SqlConnection(ConnectionString))
{
using (var command = new SqlCommand(sql))
{
command.Connection = connection;
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
var dbAdapter = new SqlDataAdapter(command);
var dataRecords = new DataTable();
dbAdapter.Fill(dataRecords);
return dataRecords;
}
}
}
public static void ExecuteNonQuery(string sql,params SqlParameter[] parameters)
{
using (var connection = new SqlConnection(ConnectionString))
{
using (var command = new SqlCommand(sql))
{
command.Connection = connection;
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
command.ExecuteNonQuery();
}
}
}
}
public class DataService
{
public void CreateTable(string tableName)
{
var sql = "CREATE TABLE [" + tableName +
"] (StudentName NVARCHAR(50),ContactNo nchar(11),FathersNo nchar(11), MothersNo nchar(11), Email nvarchar(100))";
DataHelper.ExecuteNonQuery(sql);
}
}Context
StackExchange Code Review Q#59770, answer score: 7
Revisions (0)
No revisions yet.