ExecuteNonQuery in ADO.NET


The ExecuteNonQuery() is one of the frequently used method in ADO.NET. The ExecuteNonQuery is belongs to Command object and we, developers generally used to execute the statements that do not return a result set from the database. In most of the cases we use ExecuteNonQuery to insert the records into database. It is all about ExecuteNonQuery in ADO.NET

Learn about ExecuteNonQuery in ADO.NET


The ExecuteNonQuery performs Data Definition and Data manipulation taks also. The Data Definition task is nothing but creating the stored procedures, creating a table, creating data bases, etc.

The Data Manipulation task is inserting, deleting or updating the rows in a data base table.

Below is the example to connect the database using OLEDB Provider and do the Execute non query task in a database in VB.NET.


Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyConnString As String
Dim oledbConn As OleDbConnection
Dim cmd As OleDbCommand
Dim sql As String

MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Student.mdb;"
sql = "Your SQL Statement goes Here"

oledbConn = New OleDbConnection(MyConnString)
Try
oledbConn.Open()
cmd = New OleDbCommand(sql, oledbConn)
cmd.ExecuteNonQuery()
cmd.Dispose()
oledbConn.Close()
MsgBox(" ExecuteNonQuery in OleDbConnection executed !!")
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class


Below is the example to connect the database using SQLCLIENT Provider and do the Execute non query task in a database in C#.NET.

using System;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string MyConnString = null;
SqlConnection oledbConn;
SqlCommand Sqlcmd;
string sqlQuery = null;

MyConnString = "Data Source=ServerName;Initial Catalog=DBName;User ID=UserName;Password=Password";
sqlQuery = "Your SQL Statemnt Here";

oledbConn = new SqlConnection(MyConnString);
try
{
oledbConn.Open();
Sqlcmd = new SqlCommand(sqlQuery, oledbConn);
Sqlcmd.ExecuteNonQuery();
Sqlcmd.Dispose();
oledbConn.Close();
MessageBox.Show(" ExecuteNonQuery in SqlCommand executed !!");
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: