Command Object’s ExecuteNonQuery Method ISYS 512.

Slides:



Advertisements
Similar presentations
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Advertisements

Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
Authenticating Users in an ASP.NET Application. Web Site Administration Tool From VS 2008, click Website/ ASP.Net Configuration to open Web Site Administration.
ASP.NET and ADO.NET. ASP.NET Server Controls Intrinsic Controls: These controls correspond to their HTML counterparts. –Ex. Textbox, listbox, button,
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to Structured Query Language, SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows.
ASP.Net AJAX. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net, etc.
Web Site Security ISYS 512/812. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows:
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
Website Security ISYS 512. Cookies Data in Cookies System.Web Which web site set the cookie Expiration date –DateTime data type –TimeSpan data type One.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
ASP and HTML. Anchor Tag testAnchorTag Demo: ASPNet/ASPNETProdListSelf.ASPX.
Introduction to Structured Query Language SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;
How to use MySql With C# Ross Sherman 1. Using MySql MySql is free. You could download it from:
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
UNIT 9.2 Learning Objectives A Real world Application of Databases – The Money Ball Example The Money Ball Example Data Warehouses – The Role of Data Warehouses.
Neal Stublen Populating a Database  SQLExpress should be installed with Visual Studio  The book provides a.sql file for populating.
UNIT 9.2: Learning Objectives Agile Development – Bruce Feiler on Agile Programming Database access from code – Database Cycle Review – SQL Command Types.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Working with the Application Object. Session VS Application Object The Session object helps to preserve data on a per user basis. What if we want to initialize.
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
ASP.Net and HTML. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
ASP.NET Binding and an Introduction to Database Queries Please use speaker notes for additional information!
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.
Coding ADO.NET Objects: Connection, Command, DataReader.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 The SqlCommand Object ADO.NET - Lesson 03  Training time: 15 minutes  Author:
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
Coding ADO.NET Objects: Connection, Command, DataReader.
C# MySQL onnect-C-to-MySQL 1.
ASP.NET Programming with C# and SQL Server First Edition
Coding ADO.NET Objects: Connection, Command, DataReader
ADO.NET Framework.
Unit 9.2 Database access from code Database Cycle Review
SQL commands from C# and ASP.net
ASP.NET and ADO.NET.
Coding ADO.NET Objects: Connection, Command, DataReader
Database Handling Class and Service
ASP.NET and ADO.NET.
VB Classes ISYS 512.
Working with Session and Application Objects
Introduction to ADO.NET
Shopping Cart Demo.
Command Object’s ExecuteNonQuery Method
Presentation transcript:

Command Object’s ExecuteNonQuery Method ISYS 512

Using ExecuteNonQuery Method To run SQL: – Insert – Delete – Update The ExecuteNonQuery method also returns a value indicating the number of records affected by the SQL statement.

Use ExecuteNonQuery to Insert A New Record Create unbound text boxes to enter new customer record. string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); string strSQLInsert; strSQLInsert = "Insert into Customer values ('"; strSQLInsert += textBox1.Text + "','" + textBox2.Text + "','"; strSQLInsert += textBox3.Text + "','" + textBox4.Text + "')"; SqlCommand objCommInsert = new SqlCommand(strSQLInsert, objConn); objConn.Open(); MessageBox.Show(strSQLInsert.ToString()); try { objCommInsert.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } objConn.Close();

Use ExecuteNonQuery to Delete A Record 1.Create a listbox with CIDs 2.Delete the selected record and remove the CID from the listbox. string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "delete from customer where cid = '" + listBox1.SelectedItem + "'"; SqlCommand objComm = new SqlCommand(strSQL, objConn); try { int affectedRecords; objConn.Open(); affectedRecords= objComm.ExecuteNonQuery(); MessageBox.Show(affectedRecords.ToString() + "records deleted"); listBox1.Items.RemoveAt(listBox1.SelectedIndex); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } objConn.Close();

Use ExecuteNonQuery to Update A New Record Create a project that do the following tasks: – Use a DataReader to retrieve customer IDs and populate a listbox. – Select a new rating from radio buttons for the selected customer. – Update customer’s rating using the ExecuteNonQuery method of a Command object.

create CID listbox: private void Form4_Load(object sender, EventArgs e) { try { string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); objConn.Open(); string strSQL = "select * from customer;"; SqlCommand objComm = new SqlCommand(strSQL, objConn); SqlDataReader objDataReader; objDataReader = objComm.ExecuteReader(); while (objDataReader.Read() == true) { listBox1.Items.Add(objDataReader["cid"]); // listBox1.Items.Add(objDataReader.GetString(0)); } objConn.Close(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); }

Update customer rating: private void button1_Click(object sender, EventArgs e) { string newRating; if (radioButton1.Checked) { newRating = "A"; } else if (radioButton2.Checked) { newRating = "B"; } else { newRating = "C"; } string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "Update customer set rating = '" + newRating + "' where cid='" + listBox1.SelectedItem + "'"; SqlCommand objComm = new SqlCommand(strSQL, objConn); try { objConn.Open(); objComm.ExecuteNonQuery(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } objConn.Close(); }

Possible Database Errors SQL syntax errors Database connection not open Null Violate database constraints Referential integrity Field data type and length Primary key constraint Invalid data – database may have validation rules.