Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# I need to know how to insert a value into a databse using the classes i currently have.. the value will be coming from

C# I need to know how to insert a value into a databse using the classes i currently have.. the value will be coming from a text box.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.OleDb; using System.IO; using System.Reflection;

namespace MyFinalGroupProject { public class clsDataAccess { ///

/// Connection string to the database. /// private string sConnectionString;

///

/// Constructor that sets the connection string to the database /// public clsDataAccess() { sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source= " + Directory.GetCurrentDirectory() + "\\Invoice.mdb"; }

///

/// This method takes an SQL statment that is passed in and executes it. The resulting values /// are returned in a DataSet. The number of rows returned from the query will be put into /// the reference parameter iRetVal. /// /// The SQL statement to be executed. /// Reference parameter that returns the number of selected rows. /// Returns a DataSet that contains the data from the SQL statement. public DataSet ExecuteSQLStatement(string sSQL, ref int iRetVal) { try { //Create a new DataSet DataSet ds = new DataSet();

using (OleDbConnection conn = new OleDbConnection(sConnectionString)) { using (OleDbDataAdapter adapter = new OleDbDataAdapter()) {

//Open the connection to the database conn.Open();

//Add the information for the SelectCommand using the SQL statement and the connection object adapter.SelectCommand = new OleDbCommand(sSQL, conn); adapter.SelectCommand.CommandTimeout = 0;

//Fill up the DataSet with data adapter.Fill(ds); } }

//Set the number of values returned iRetVal = ds.Tables[0].Rows.Count;

//return the DataSet return ds; } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }

///

/// This method takes an SQL statment that is passed in and executes it. The resulting single /// value is returned. /// /// The SQL statement to be executed. /// Returns a string from the scalar SQL statement. public string ExecuteScalarSQL(string sSQL) { try { //Holds the return value object obj;

using (OleDbConnection conn = new OleDbConnection(sConnectionString)) { using (OleDbDataAdapter adapter = new OleDbDataAdapter()) {

//Open the connection to the database conn.Open();

//Add the information for the SelectCommand using the SQL statement and the connection object adapter.SelectCommand = new OleDbCommand(sSQL, conn); adapter.SelectCommand.CommandTimeout = 0;

//Execute the scalar SQL statement obj = adapter.SelectCommand.ExecuteScalar(); } }

//See if the object is null if (obj == null) { //Return a blank return ""; } else { //Return the value return obj.ToString(); } } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }

///

/// This method takes an SQL statment that is a non query and executes it. /// /// The SQL statement to be executed. /// Returns the number of rows affected by the SQL statement. public int ExecuteNonQuery(string sSQL) { try { //Number of rows affected int iNumRows;

using (OleDbConnection conn = new OleDbConnection(sConnectionString)) { //Open the connection to the database conn.Open();

//Add the information for the SelectCommand using the SQL statement and the connection object OleDbCommand cmd = new OleDbCommand(sSQL, conn); cmd.CommandTimeout = 0;

//Execute the non query SQL statement iNumRows = cmd.ExecuteNonQuery(); }

//return the number of rows affected return iNumRows; } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } } } }

.............................

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Data;

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Data;

namespace MyFinalGroupProject { public class clsitem_Manager////////////////////////////////////////////this class managers flights from the database { //clsDataAccess db;

clsDataAccess db = new clsDataAccess(); DataSet ds = new DataSet(); BindingList lstItems = new BindingList(); public int numberOfRecords;

public BindingList GetAllItemsFromDB()////////////////////////////////////////////This method will get all flights from the db, put each flight into a clsFlight object, add that object to the list, then return that list to the UI { //lstFlights = ////////This is a list of flights that represents all flights from the database

////////////////Steps for you to complete string sSQL = clsSQL.GetAllItems_SQL();/////////////////////////Get SQL to get all flights

int iRet = 0;

//Execute the sSQL statement using the clsDataAccess class to get the flights into a dataset ds = db.ExecuteSQLStatement(sSQL, ref iRet);

foreach (DataRow dr in ds.Tables[0].Rows) { lstItems.Add(new clsItem { ItemCode = dr[0].ToString(), ItemDesc = dr[1].ToString(), Cost = dr[2].ToString() }); numberOfRecords++; } return lstItems;

}

}

public class clsItem { public string ItemCode { get; set; } public string ItemDesc { get; set; } public string Cost { get; set; }

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Creating A Database In Filemaker Pro Visual QuickProject Guide

Authors: Steven A. Schwartz

1st Edition

0321321219, 978-0321321213

More Books

Students also viewed these Databases questions

Question

2. List the advantages of listening well

Answered: 1 week ago

Question

=+j Explain IHRMs role in global HR research.

Answered: 1 week ago

Question

=+j Describe an effective crisis management program.

Answered: 1 week ago