basic CRUD code using SQL as database

Posted by

But what is the meaning of CRUD ?

C = Create
R = Read
U = Update
D = Delete

This code was used for a basic ASPX project, using SQL as the database, you just need to adjust the code to fit your needs, let’s check the CRUD example,

CREATE

SqlConnection conexion = new SqlConnection(connect);

SqlCommand command = new SqlCommand(“INSERT INTO USERS(name,last,ID,email,username,password,) VALUES(‘” + txtName.Text + “‘,'” + txtLast.Text + “‘,'” + txtID.Text + “‘,'” + txtEmail.Text + “‘,'” + txtUsername.Text + “‘,'” + txtPassword.Text + “‘)”, conexion);

conexion.Open();
command.ExecuteNonQuery() ;
Response.Write(alert(‘Correct!'”);
conexion.Close();

READ

SqlConnection conexion = new SqlConnection(connect);

string query = “SELECT * FROM USERS WHERE username='” + txtSEARCH.Text + “‘”;

SqlCommand cmd = new SqlCommand(query, conexion);
conexion.Open();
SqlDataReader data = cmd.ExecuteReader();
conexion.Close();

UPDATE

SqlConnection conexion = new SqlConnection(connect);

SqlCommand command = new SqlCommand( “update USERS set name= ‘” + txtName.Text + “‘,last= ‘” + txtLast.Text + “‘,ID= ‘” + txtID.Text + “‘,email= ‘” + txtEmail.Text + “‘,username= ‘” + txtUsername.Text + “‘,password= ‘” + txtPassword.Text + “‘ where username = ‘” + this.txtUsername.Text + “‘”, conexion);

conexion.Open();
command.ExecuteNonQuery();
Response.Write(“alert(‘Correct !.’)”);
conexion.Close();

DELETE

SqlConnection conexion = new SqlConnection(connect);

SqlCommand command = new SqlCommand(“Delete From USERS Where username = ‘” + txtUsername.Text + “‘”, conexion);

conexion.Open();
command.ExecuteNonQuery();
Response.Write(“alert(‘Deleted.’)”);
conexion.Close();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s