0% found this document useful (0 votes)
51 views2 pages

Connstring

The document describes how to connect to SQL databases and perform CRUD (create, read, update, delete) operations using ADO.NET. It includes examples of connecting to SQL Server Compact, SQL Server Express, and remote databases. It also shows how to execute SQL statements and stored procedures to insert, update, delete records as well as retrieve data and return datasets. Stored procedures are created to encapsulate the CRUD logic and are called by passing parameter values.

Uploaded by

pepeponk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views2 pages

Connstring

The document describes how to connect to SQL databases and perform CRUD (create, read, update, delete) operations using ADO.NET. It includes examples of connecting to SQL Server Compact, SQL Server Express, and remote databases. It also shows how to execute SQL statements and stored procedures to insert, update, delete records as well as retrieve data and return datasets. Stored procedures are created to encapsulate the CRUD logic and are called by passing parameter values.

Uploaded by

pepeponk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

connstring = new SqlCeConnection(@"Data Source=c:\users\jozz\documents\DBImagenes.

sdf;Persist Security Info=False");



connstring = new SqlCeConnection (@ Data Source=\\192.168.0.213\Demo\DBImagenes.sdf;Persist Security Info=False);

connstring = @"Data source=JOZZ-PC\SQLEXPRESS;initial Catalog=agendaapp; integrated security=true";
data source = ServidorSQL; initial catalog = BaseDatos; user id = Usuario; password = Contrasea



Insertar

SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand();

cmd.CommandText = string.Format("insert into usuarios(usuario, password) values ('{0}','{1}')", this.usuario, this.password);
cmd.CommandType = CommandType.Text;
cmd.Connection = _conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

Update
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand();

cmd.CommandText = string.Format("Update usuarios set nombre = '{0}', telefono='{1}' where id= '{4}' ", this.Nombre, this.telefono, this.contactoID);
cmd.CommandType = CommandType.Text;
cmd.Connection = conn
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();


Delete
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand();

cmd.CommandText = string.Format("Delete from usuarios where id='{0}', this.usuario);
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();


int id;
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand();

cmd.CommandText = string.Format("Select id from usuarios where usuario='{0}' and password='{1}'",this.usuario,this.password);
cmd.Connection = conn;

conn.Open();
id=(int)cmd.ExecuteScalar();
conn.Close();
return id;

DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand();

cmd.CommandText = string.Format("select * from contactos where id= '{0}'", id);
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;

SqlDataAdapter da = new SqlDataAdapter(cmd);

conn.Open();
cmd.ExecuteNonQuery();
da.Fill(ds);
conn.Close();
return ds;


Insertar
CREATE PROCEDURE sp_InsertUsuarios
@Nombre nvarchar(50),
@Apellido nvarchar(max),
@AreadeTrabajo nvarchar(max),
AS
BEGIN
INSERT INTO tbUsuarios (Nombre,Apellido,AreadeTrabajo)VALUES(@Nombre,@Apellido,@AreadeTrabajo)
END
GO


Update
CREATE PROCEDURE sp_UpdateUsuario
@Nombre nvarchar(50),
@Apellido nvarchar(max),
@AreadeTrabajo nvarchar(max),
AS
BEGIN
UPDATE tbUsuarios SET Nombre=@Nombre,Apellido=@Apellido,AreadeTrabajo=@AreadeTrabajo WHERE Usuario=@Usuario
END
GO

Delete
CREATE PROCEDURE sp_BorrarUsuario
@usuario nvarchar(50)
AS
BEGIN
DELETE FROM tbUsuarios WHERE Usuario=@usuario
END
GO
con=new SqlConnection(CadenaConexion);
cmd = new SqlCommand("sp_InsertUsuarios", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@Nombre", sNom);
cmd.Parameters.AddWithValue("@Apellido", sApp);
cmd.Parameters.AddWithValue("@AreadeTrabajo", sAreaTra);

con.Open();
cmd.ExecuteNonQuery();
con.Close();

con = new SqlConnection(CadenaConexion);

cmd = new SqlCommand("sp_UpdateUsuario", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@Nombre",sNom );
cmd.Parameters.AddWithValue("@Apellido",sApp);
cmd.Parameters.AddWithValue("@AreadeTrabajo",sAreaTra );

con.Open();
cmd.ExecuteNonQuery();
con.Close();
con = new SqlConnection(CadenaConexion);

cmd = new SqlCommand("sp_BorrarUsuario", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@usuario", sUsuario);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
con = new SqlConnection(CadenaConexion);
cmd = new SqlCommand("sp_SelectUsuario", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@usuario",sUsuario);
cmd.Parameters.AddWithValue("@pass",sContrasena);


con.Open ();
SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{
IDusuario = (int)dr[0];
Nombre = dr[1].ToString();
Apellido = dr[2].ToString();
AreadeTrabajo = dr[3].ToString();
}
Con.Close();

You might also like