Open windows forms app project
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
emp no
emp name
salary
text |
add |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication17
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// insert into emp244 values(101,"varun",23422)
string s = "insert into emp244 values(" + textBox1.Text + "," + textBox2.Text + "'," + textBox3.Text + ")";
MessageBox.Show(s);
OleDbConnection cn = new OleDbConnection("password=tiger;provider=msdaora.1;userid=scott");
// password userid and provider can be typed in any order
cn.Open();
OleDbCommand cmd = new OleDbCommand(s, cn);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("record is added");
}
catch (OleDbException e1)
{
MessageBox.Show(e1.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
if (c.GetType().Name == "textbox") c.Text = "";
textBox1.Focus();
}
}
}
Observation on above program:
about oledbconnection class
1)this class is the part of system.data.oledb namespace
2)this class is used to open a connection with any type of database
3)oledbconnection class is inherited from a predefined abstract class as Database connection
4)DB connection is the part of
System.Data.common namespace
About oledbcommand class
1)this class is used to execute sql statements and stored procedures
2)SQL is a collection of
DDL…..data definition language, create, alter, drop
DML….insert, update, delete
TCL….commit, rollback, savepoint
QL…..select
Methods of command class
1)Executenonquery() is used to execute DLL, DML, TCL and stored procedures
2)ExecuteReader()
a)is required while executing select statement
b)Returns multiple records in the format of datareader
3)ExecuteScalar()
a)required while execute select statements
b)searches only for first match, if match is found then only first column value of the first matched record will be returned
A program to create a table in sql server
Open sql server software, then login
->open view menu->object explorer
->right click on database->new database and give the name as DB244->ok
Right click on DB244->select new query
Open windows forms app project
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
place a button
using System.Data.sqlclient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("userid=sa;password=123;database=dB244");
cn.Open();
string s = "create table EMP(eno int,ename varchar(10),sql int)";
SqlCommand cmd = new SqlCommand(s, cn);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("table is created");
}
catch (SqlException e1)
{
MessageBox.Show(e1.Message);
}
}
}
}