Update and insert in one button with Command Parametrs C# - c#

I want to do update and insert in one button.
i want it to check if there's data then it will just update it but if not it will add it.
I don't know what i'm missing in this code
cnx.Open();
SqlCommand cmd = cnx.CreateCommand();
SqlDataReader dr;
cmd.CommandText = "Select * from TPayement where ID='" + txt_ID.Text + "'";
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
cmd.CommandText = "update TPayement (ID, Nom, QUA, Salaire, NombreJour, HeurSupplimentaire, SalaireHeur, Somme) values (#ID, #Nom, #QUA, #Salaire, #NombreJour, #HeurSupplimentaire, #SalaireHeur, #Somme) where ID='" + txt_ID.Text + "'";
cmd.Parameters.Add("#ID", SqlDbType.Int);
cmd.Parameters.Add("#Nom", SqlDbType.VarChar, 50);
cmd.Parameters.Add("#QUA", SqlDbType.VarChar, 50);
cmd.Parameters.Add("#Salaire", SqlDbType.Float);
cmd.Parameters.Add("#NombreJour", SqlDbType.Float);
cmd.Parameters.Add("#HeurSupplimentaire", SqlDbType.Float);
cmd.Parameters.Add("#SalaireHeur", SqlDbType.Float);
cmd.Parameters.Add("#Somme", SqlDbType.Float);
cmd.Parameters["#ID"].Value = txt_ID.Text;
cmd.Parameters["#Nom"].Value = txt_Nom.Text;
cmd.Parameters["#QUA"].Value = txt_QUA.Text;
cmd.Parameters["#Salaire"].Value = txt_Salaire.Text;
cmd.Parameters["#NombreJour"].Value = txt_NBRJ.Text;
cmd.Parameters["#HeurSupplimentaire"].Value = txt_HSUP.Text;
cmd.Parameters["#SalaireHeur"].Value = txt_SalireHeur.Text;
cmd.Parameters["#Somme"].Value = txt_Somme.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Se payement est enregistrer");
}
else
{
cmd.CommandText = "insert into TPayement (ID, Nom, QUA, Salaire, NombreJour, HeurSupplimentaire, SalaireHeur, Somme) values (#ID, #Nom, #QUA, #Salaire, #NombreJour, #HeurSupplimentaire, #SalaireHeur, #Somme)";
cmd.Parameters.Add("#ID", SqlDbType.Int);
cmd.Parameters.Add("#Nom", SqlDbType.VarChar, 50);
cmd.Parameters.Add("#QUA", SqlDbType.VarChar, 50);
cmd.Parameters.Add("#Salaire", SqlDbType.Float);
cmd.Parameters.Add("#NombreJour", SqlDbType.Float);
cmd.Parameters.Add("#HeurSupplimentaire", SqlDbType.Float);
cmd.Parameters.Add("#SalaireHeur", SqlDbType.Float);
cmd.Parameters.Add("#Somme", SqlDbType.Float);
cmd.Parameters["#ID"].Value = txt_ID.Text;
cmd.Parameters["#Nom"].Value = txt_Nom.Text;
cmd.Parameters["#QUA"].Value = txt_QUA.Text;
cmd.Parameters["#Salaire"].Value = txt_Salaire.Text;
cmd.Parameters["#NombreJour"].Value = txt_NBRJ.Text;
cmd.Parameters["#HeurSupplimentaire"].Value = txt_HSUP.Text;
cmd.Parameters["#SalaireHeur"].Value = txt_SalireHeur.Text;
cmd.Parameters["#Somme"].Value = txt_Somme.Text;
dr.Close();
cmd.ExecuteNonQuery();
MessageBox.Show("Se payement est enregistrer");
}
dr.Close();
cnx.Close();

The problem is that you open a DataReader with the Command object cmd.
Then, before you close the DataReader, you try to re-purpose the same cmd object and Execute it.
You have to close the DataReader before you can re-use the cmd object for an insert or update.

Related

How to update a excel row using C# ADO.NET?

I am using Oledb to connect with an excel sheet. My connection is working fine. Insert and retrieve data are also working fine. But when I try to update a record in excel sheet with where clause then my code executes successfully without any error but excel row does not update.
My code is:
strQry = #"Update [Guests$] set [FirstName]=#FirstName,[LastName]=#LastName,[Address]=#Address,
[EmailId]=#EmailId,[TelNo]=#TelNo,[MobileNo]=#MobileNo,[FaxNo]=#FaxNo where [GuestId]=#GuestId";
using (OleDbConnection con = new OleDbConnection(clsConnection.conStr))
{
using (OleDbCommand cmd = new OleDbCommand(strQry,con))
{
cmd.Parameters.Add("#GuestId", OleDbType.Integer).Value = intId;
cmd.Parameters.Add("#FirstName", OleDbType.VarChar, 15).Value = txtFirstName.Text;
cmd.Parameters.Add("#LastName", OleDbType.VarChar, 15).Value = txtLastName.Text;
cmd.Parameters.Add("#Address", OleDbType.VarChar, 200).Value = txtAddress.Text;
cmd.Parameters.Add("#EmailId", OleDbType.VarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("#TelNo", OleDbType.VarChar, 20).Value = txtTelNo.Text;
cmd.Parameters.Add("#MobileNo", OleDbType.VarChar, 15).Value = txtMobileNo.Text;
cmd.Parameters.Add("#FaxNo", OleDbType.VarChar, 20).Value = txtFaxNo.Text;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
MessageBox.Show(strSuccessMsg);
BinddgvGuests();
ClearControls();
}
}
But when I remove the where clause then all the records are updated.
Is there something wrong with my where clause?
After searching a lot, finally I encounter my mistake. Actually parameters should be in same sequence as in the query. So I change my parameters to:
cmd.Parameters.Add("#FirstName", OleDbType.VarChar, 15).Value = txtFirstName.Text;
cmd.Parameters.Add("#LastName", OleDbType.VarChar, 15).Value = txtLastName.Text;
cmd.Parameters.Add("#Address", OleDbType.VarChar, 200).Value = txtAddress.Text;
cmd.Parameters.Add("#EmailId", OleDbType.VarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("#TelNo", OleDbType.VarChar, 20).Value = txtTelNo.Text;
cmd.Parameters.Add("#MobileNo", OleDbType.VarChar, 15).Value = txtMobileNo.Text;
cmd.Parameters.Add("#FaxNo", OleDbType.VarChar, 20).Value = txtFaxNo.Text;
cmd.Parameters.Add("#GuestId", OleDbType.Integer).Value = intId;
Now this is working fine for me.

Issues with the insert function to my database program

Good day all! I'm having a minor issue with the insert to my program. See, the code has no errors but I'm having an OleDb exception when trying to insert. The other parts of my project work fine but there is a tiny issue here that I can't seem to find
public void Insert()
{
//myDb = new OleDbConnection(conn + dbFile);
myDb.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Employee", myDb);
//
OleDbCommand cmd = new OleDbCommand("INSERT INTO Employee(Username, Password, email, phone) VALUES ('" + insUn + "','" + insPass + "','" + insNm + "','" + insNmr + "')", myDb);
adapter.InsertCommand = cmd;
adapter.InsertCommand.ExecuteNonQuery();
DataSet ds = new DataSet();
adapter.Fill(ds, "Employee");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Employee";
myDb.Close();
}
The other functions such as the search and delete work but I can't find the problem here
These are the exceptions:
try
{
if (textBox2.Text != "")
{
insUn = textBox2.Text;
insNmr = textBox4.Text;
insPass = textBox3.Text;
insNm = textBox5.Text;
}
Insert();
}
catch (OleDbException ex)
{
MessageBox.Show("Error, please try again", "Exception", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
}
catch (FormatException ex)
{
MessageBox.Show("One or more fields have not been entered. Please check and re-enter", "Missing fields", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
enter code here
Abdellah's answer will work, but be aware for SQL Injection attacks when building your query string. You should build it like this:
OleDbCommand cmd = new OleDbCommand("INSERT INTO Employee(Username, Password, email, phone) VALUES (#p1, #p2, #p3, #p4)", myDb);
int maxSize = 50;
cmd.Paramters.Add("#p1", SqlDbType.VarChar, maxSize).Value = insUn;
cmd.Parameters.Add("#p2", SqlDbType.VarChar, maxSize).Value = insPass;
cmd.Parameters.Add("#p3", SqlDbType.VarChar, maxSize).Value = insNm;
cmd.Parameters.Add("#p4", SqlDbType.VarChar, maxSize).Value = insNmr;
I advice you to use the Parameter to avoid SQL injections , and put the brackets [] in query for [Password] because it's a keyword like below :
public void Insert()
{
//myDb = new OleDbConnection(conn + dbFile);
myDb.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO Employee(Username, [Password], email, phone) VALUES (#Username, #Password, #email, #phone)", myDb);
cmd.Parameters.AddWithValue("#Username", insUn);
cmd.Parameters.AddWithValue("#Password", insPass);
cmd.Parameters.AddWithValue("#email", insNm);
cmd.Parameters.AddWithValue("#phone", insNmr);
cmd.ExecuteNonQuery();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Employee", myDb);
DataSet ds = new DataSet();
adapter.Fill(ds, "Employee");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Employee";
myDb.Close();
}

Send new exception if a SELECT value returned is NULL or does not exist in the Table

So i am using a stored procedure to select a registry from a DB, the only thing the stored procedure does is SELECT ... The thing is that i use that stored procedure to fill a DataSet wich i return in my WebService. The problem comes when i want to send an exception instead of the DataSet, since the stored procedure checks on the DB and returns an empty row the DataSet fills with nothing and does not send me to an Exception ... Now, i save all my exceptions in a log table in the same DB ... My question is, can i go to the Exception block if the SELECT values are empty??
Here is my Code
[WebMethod(Description = "Private", EnableSession = false)]
public DataSet M812(string p_transaction)
{
string extran, enclosure, eDate;
try
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "dbo.sp_M812";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = p_transaction;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Entradas");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
return ds;
}
catch(SqlException Ex)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "dbo.sp_reqdataerrorlog";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd.Parameters.Add("#vo_enclosure", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd.Parameters.Add("#vo_trans", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
enclosure = "" + cmd.Parameters["#vo_enclosure"].Value;
extran = "" + cmd.Parameters["#vo_trans"].Value;
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con2;
cmd2.CommandText = "dbo.sp_errorlog";
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd2.Parameters.Add("#p_enclosure", SqlDbType.NChar, 6).Value = enclosure;
cmd2.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = extran;
cmd2.Parameters.Add("#p_method", SqlDbType.NChar, 6).Value = "812";
cmd2.Parameters.Add("#p_message", SqlDbType.NVarChar, 250).Value = "SQL Error: " + Ex.Message;
cmd2.Parameters.Add("#vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd2.Parameters.Add("#vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
con2.Open();
cmd2.ExecuteNonQuery();
con2.Close();
cmd2.Connection.Close();
eDate = "" + cmd2.Parameters["#vo_errorDate"].Value;
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = con3;
cmd3.CommandText = "dbo.sp_selecterrorlog";
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = p_transaction;
cmd3.Parameters.Add("#p_date", SqlDbType.DateTime).Value = eDate;
SqlDataAdapter da = new SqlDataAdapter(cmd3);
DataSet ds = new DataSet();
da.Fill(ds, "ErrorLog");
con3.Open();
cmd3.ExecuteNonQuery();
con3.Close();
cmd3.Connection.Close();
return ds;
}
catch (Exception Ex)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "dbo.sp_reqdataerrorlog";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd.Parameters.Add("#vo_enclosure", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd.Parameters.Add("#vo_trans", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
enclosure = "" + cmd.Parameters["#vo_enclosure"].Value;
extran = "" + cmd.Parameters["#vo_trans"].Value;
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con2;
cmd2.CommandText = "dbo.sp_errorlog";
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd2.Parameters.Add("#p_enclosure", SqlDbType.NChar, 6).Value = enclosure;
cmd2.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = extran;
cmd2.Parameters.Add("#p_method", SqlDbType.NChar, 6).Value = "812";
cmd2.Parameters.Add("#p_message", SqlDbType.NVarChar, 250).Value = "WEB Error: " + Ex.Message;
cmd2.Parameters.Add("#vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd2.Parameters.Add("#vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
con2.Open();
cmd2.ExecuteNonQuery();
con2.Close();
cmd2.Connection.Close();
eDate = "" + cmd2.Parameters["#vo_errorDate"].Value;
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = con3;
cmd3.CommandText = "dbo.sp_selecterrorlog";
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = p_transaction;
cmd3.Parameters.Add("#p_date", SqlDbType.DateTime).Value = eDate;
SqlDataAdapter da = new SqlDataAdapter(cmd3);
DataSet ds = new DataSet();
da.Fill(ds, "ErrorLog");
con3.Open();
cmd3.ExecuteNonQuery();
con3.Close();
cmd3.Connection.Close();
return ds;
}
}
Here is how you can check if the DataSet has any rows in any table:
bool hasRows = ds.Tables.Cast<DataTable>().Any(table => table.Rows.Count != 0);
Now you have a choice as to how to get the error data returned to the caller:
Pull out the error building logic into a separate method that your exception catch handler block and your if condition can both call, like this:
private void BuildAndReturnErrorDataSet()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "dbo.sp_reqdataerrorlog";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd.Parameters.Add("#vo_enclosure", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd.Parameters.Add("#vo_trans", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
enclosure = "" + cmd.Parameters["#vo_enclosure"].Value;
extran = "" + cmd.Parameters["#vo_trans"].Value;
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con2;
cmd2.CommandText = "dbo.sp_errorlog";
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd2.Parameters.Add("#p_enclosure", SqlDbType.NChar, 6).Value = enclosure;
cmd2.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = extran;
cmd2.Parameters.Add("#p_method", SqlDbType.NChar, 6).Value = "812";
cmd2.Parameters.Add("#p_message", SqlDbType.NVarChar, 250).Value = "WEB Error: " + Ex.Message;
cmd2.Parameters.Add("#vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd2.Parameters.Add("#vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
con2.Open();
cmd2.ExecuteNonQuery();
con2.Close();
cmd2.Connection.Close();
eDate = "" + cmd2.Parameters["#vo_errorDate"].Value;
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = con3;
cmd3.CommandText = "dbo.sp_selecterrorlog";
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = p_transaction;
cmd3.Parameters.Add("#p_date", SqlDbType.DateTime).Value = eDate;
SqlDataAdapter da = new SqlDataAdapter(cmd3);
DataSet ds = new DataSet();
da.Fill(ds, "ErrorLog");
con3.Open();
cmd3.ExecuteNonQuery();
con3.Close();
cmd3.Connection.Close();
return ds;
}
if(!hasRows)
{
// Call error data set building logic
BuildAndReturnErrorDataSet();
}
catch (Exception Ex)
{
// Call error data set building logic
BuildAndReturnErrorDataSet();
}
Raise exception if hasRows is false - not recommended as this is using exception handling to cause program control flow, but it technically will work.

Failed to convert parameter value from a DataRowView to a Int32

I'm inserting records and one of my object is combobox. The combox is connected to the table. When i'm inserting this error appear:
Failed to convert parameter value from a DataRowView to a Int32
My code:
cn.Open();
SqlCommand Insert = new SqlCommand();
Insert.Connection = cn;
Insert.CommandType = CommandType.Text;
Insert.CommandText = "INSERT INTO Ticket VALUES (CustomerID, Date, Store, Amount, NoStub) ";
Insert.Parameters.Add("CustomerID", SqlDbType.Int).Value = cboName.SelectedValue;
Insert.Parameters.Add("Date", SqlDbType.DateTime).Value = dtpDate.Value.Date.ToString();
Insert.Parameters.Add("Store", SqlDbType.NVarChar).Value = txtStore.Text;
Insert.Parameters.Add("Amount", SqlDbType.Decimal).Value = txtAmount.Text;
Insert.Parameters.Add("NoStub", SqlDbType.Decimal).Value = txtStub.Text;
Insert.ExecuteNonQuery();
cn.Close();
Use this sample code:
command.Parameters.Add("#CustomerID", SqlDbType.Int).Value = Convert.ToInt32(storeCode);
or use
int.parse for cboName.
Change your code to the following and let the Server resolve the data type
cn.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = cn;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "INSERT INTO Ticket(CustomerID, Date, Store, Amount, NoStub)
VALUES (#CustomerID, #Date, #Store, #Amount, #NoStub) ";
sqlCmd.Parameters.AddWithValue("#CustomerID", cboName.SelectedValue);
sqlCmd.Parameters.AddWithValue("#Date", dtpDate.Value.Date.ToString());
sqlCmd.Parameters.AddWithValue("#Store", txtStore.Text);
sqlCmd.Parameters.AddWithValue("#Amount", txtAmount.Text);
sqlCmd.Parameters.AddWithValue("#NoStub", txtStub.Text);
sqlCmd.ExecuteNonQuery();
cn.Close();

Creating a Class for SqlConnection in ASP.NET and then use in your application?

I'm having to select, update, delete, etc. basically everywhere on my web application and every time I'm having to write something like this:
con.Open();
cmd = new SqlCommand("update items set item_cost = #cost, item_retail_value = #retail, item_v_style = #v_style, item_v_color = #v_color, item_description = #description, " +
"item_date_modify = #date, item_time_modify = #time, item_user_modify = #user where item_style = #style and item_color = #color and item_sec_dimenssion = #sec", con);
cmd.Parameters.Add("#style", SqlDbType.VarChar, 15).Value = styl;
cmd.Parameters.Add("#color", SqlDbType.VarChar, 3).Value = colr;
cmd.Parameters.Add("#sec", SqlDbType.VarChar, 8).Value = sdim;
cmd.Parameters.Add("#size", SqlDbType.VarChar, 3).Value = size;
cmd.Parameters.Add("#cost", SqlDbType.VarChar, 8).Value = sprice;
cmd.Parameters.Add("#retail", SqlDbType.VarChar, 8).Value = sretail;
cmd.Parameters.Add("#uom", SqlDbType.VarChar, 3).Value = uom;
cmd.Parameters.Add("#sku", SqlDbType.VarChar, 10).Value = sku;
cmd.Parameters.Add("#barcode", SqlDbType.VarChar, 20).Value = barcode;
cmd.Parameters.Add("#v_style", SqlDbType.VarChar, 100).Value = v_style;
cmd.Parameters.Add("#v_color", SqlDbType.VarChar, 20).Value = v_color;
cmd.Parameters.Add("#description", SqlDbType.VarChar, 40).Value = description;
cmd.Parameters.Add("#date", SqlDbType.VarChar, 20).Value = date;
cmd.Parameters.Add("#time", SqlDbType.VarChar, 20).Value = time;
cmd.Parameters.Add("#user", SqlDbType.VarChar, 20).Value = user;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
Is there a way to create a Class to create SqlConnection with a SqlCommand for "Select", "Update", "Delete", etc. and just provide the table, the fields, parameters and the criteria so I don't have to write all this code every time.
Any help will really appreciate it.
Linq2Sql
Entity Framework
nHibernate
Dapper
BLToolkit
create a class with Singleton and then every time you want to do anything with SqlConnection
you can use the same object.

Categories