Home   Artículos   Recursos   Foros   
Artíclos recientes publicados en Latindevelopers:

Visual C++: NSDoubleEdit: Un control para el manejo de números decimales en Visual C++.
Visual C++: Implementando una Calculadora en Visual C++
Visual C++: CCommandLine: Una clase para el uso de la linea de comando
Visual C++: Una clase para el manejo del Registro


ayuda con C# y SQL

Foro sobre el nuevo lenguaje C# conocido tambien como CSharp o C Numeral

Moderador: Bestprogramado

ayuda con C# y SQL

Notapor luidiego el Dom Jun 25, 2006 6:48 pm

Hola disculpen que les moleste estoy empesando a programar con Base de datos en SQL y necesito que de favor me ayuden con esto.Tengo una base de datos y necesito presentar esos datos en un formulario eso ya lo hice (El Boton cargar y Mostrar ya estan) lo que no puedo programar es el boton siguiente(tiene que presentar los datos del siguiente usuario) , el boton atras
(tiene que presentar los datos del usuario anterior al actual) y el boton buscar(tiene que presentar los datos del usuario del cual se ingresa el numero de cedula en el textbox) yo se que ustedes pueden ayudarme y les estare muy agradecido Chao. (esto en C# 2003 ahi les envio el codigo para que lo entiendan mejor).[using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Deber
{
/// <summary>
/// Descripción breve de Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
int i=0;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Data.SqlClient.SqlCommand sqlCommand1;
private mazo.DataSet1 dataSet11;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox3;
/// <summary>
/// Variable del diseñador requerida.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Necesario para admitir el Diseñador de Windows Forms
//
InitializeComponent();

//
// TODO: agregar código de constructor después de llamar a InitializeComponent
//
}

/// <summary>
/// Limpiar los recursos que se estén utilizando.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Código generado por el Diseñador de Windows Forms
/// <summary>
/// Método necesario para admitir el Diseñador. No se puede modificar
/// el contenido del método con el editor de código.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.dataSet11 = new mazo.DataSet1();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.button3 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Cedula";
//
// label3
//
this.label3.Location = new System.Drawing.Point(40, 80);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 23);
this.label3.TabIndex = 2;
this.label3.Text = "Nombre";
//
// label4
//
this.label4.Location = new System.Drawing.Point(48, 120);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(32, 23);
this.label4.TabIndex = 3;
this.label4.Text = "Fono";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(40, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 32);
this.button1.TabIndex = 5;
this.button1.Text = "CARGAR";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 208);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 32);
this.button2.TabIndex = 6;
this.button2.Text = "MOSTRAR";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 20);
this.textBox1.TabIndex = 8;
this.textBox1.Text = "";
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(112, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(256, 20);
this.textBox2.TabIndex = 9;
this.textBox2.Text = "";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(40, 256);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(200, 20);
this.textBox4.TabIndex = 11;
this.textBox4.Text = "";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.textBox3);
this.groupBox1.Location = new System.Drawing.Point(32, 32);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(384, 168);
this.groupBox1.TabIndex = 13;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "DATOS";
//
// groupBox2
//
this.groupBox2.Location = new System.Drawing.Point(920, 16);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(344, 240);
this.groupBox2.TabIndex = 13;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "groupBox1";
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("es-ES");
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=\"CRISTIAN-X8R826\";packet size=4096;integrated security=SSPI;data s" +
"ource=\"(local)\";persist security info=False;initial catalog=Base de datos Deber";
//
// button4
//
this.button4.Location = new System.Drawing.Point(240, 208);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(72, 32);
this.button4.TabIndex = 14;
this.button4.Text = "Siguiente";
//
// button5
//
this.button5.Location = new System.Drawing.Point(328, 208);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(80, 32);
this.button5.TabIndex = 15;
this.button5.Text = "Atras";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Usuario", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("id", "id"),
new System.Data.Common.DataColumnMapping("nombre", "nombre"),
new System.Data.Common.DataColumnMapping("cedula", "cedula"),
new System.Data.Common.DataColumnMapping("fono", "fono")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT id, nombre, cedula, fono FROM Usuario";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// button3
//
this.button3.Location = new System.Drawing.Point(240, 256);
this.button3.Name = "button3";
this.button3.TabIndex = 16;
this.button3.Text = "Buscar";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(80, 120);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(136, 20);
this.textBox3.TabIndex = 17;
this.textBox3.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.IndianRed;
this.ClientSize = new System.Drawing.Size(448, 326);
this.Controls.Add(this.button3);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox4);
this.Name = "Form1";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
this.Text = "Datos ";
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// Punto de entrada principal de la aplicación.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(!Char.IsDigit(e.KeyChar))
{
e.Handled=true;
}
}

private void button1_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dataSet11);
}

private void button2_Click(object sender, System.EventArgs e )
{

textBox1.DataBindings.Add("Text",dataSet11,"Usuario.cedula");
textBox2.DataBindings.Add("Text",dataSet11,"Usuario.nombre");
textBox3.DataBindings.Add("Text",dataSet11,"Usuario.fono");

}

}
}
]
luidiego
Novato
Novato
 
Mensajes: 11
Registrado: Mié May 10, 2006 11:05 am

Notapor Vicitor2006 el Mié May 23, 2007 4:26 pm

Tu si sabes realizar una buena pregunta, con codigo y todo, para tu mala suerte yo tambien estoy empezando en esto y no te puedo ayudar.
Avatar de Usuario
Vicitor2006
Usuario Muy Activo
Usuario Muy Activo
 
Mensajes: 127
Registrado: Mié May 23, 2007 11:38 am

Notapor Bestprogramado el Mié May 23, 2007 4:35 pm

Pues te podria ayudar si pasaras la base de datos, pero no como texto, sino como archivo. Busca algun hosting o mandalo por correo a raul-338@hotmail.com

Gracias.

PD: Victor2006, chequea tus mensajes privados (Arriba del mensaje)
Imagen
Imagen
Programar es jugar a ser Dios!!
Avatar de Usuario
Bestprogramado
Usuario Muy Activo
Usuario Muy Activo
 
Mensajes: 118
Registrado: Mar May 31, 2005 1:44 pm
Ubicación: Buenos Aires

Notapor Vicitor2006 el Mié May 23, 2007 5:18 pm

Chequeado.
Avatar de Usuario
Vicitor2006
Usuario Muy Activo
Usuario Muy Activo
 
Mensajes: 127
Registrado: Mié May 23, 2007 11:38 am

Notapor JaviMarciano el Lun Ago 20, 2007 9:45 am

Si entendí bien tu pregunta, lo mejor que podes hacer es Enlazar tus TextBox a la base de Datos y los manejas con un
Código: Seleccionar todo
            Binding bind = new Binding("Text", dt, "nombre");


El código es el siguiente:
Código: Seleccionar todo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

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

        DataSet ds;
        DataTable dt;

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=(local);initial Catalog=BaseDeDatos; User Id=;Password=;Integrated Security=SSPI;");
            con.Open();
            ds = new DataSet();
            dt = ds.Tables.Add();
            SqlCommand com = new SqlCommand("Select * from TablaZ",con);
            try
            {
                com.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(com);
                da.Fill(dt) ;
                //dataGridView1.DataSource = dt;   
                textBox1.Text= Convert.ToString(dt.Rows[0][1]);
                textBox2.Text = Convert.ToString(dt.Rows[0][2]);
                textBox3.Text = Convert.ToString(dt.Rows[0][3]);   
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
         
        }
/*Para asignarle a los cuadros de texto los valores de los campos de un registro:
hacer una consulta que me devuelva los campos que necesito, para asignarla a un DataTable
y posteriormente acceder en forma individual a cada una de las columnas de este DataTable
*


*/
        BindingManagerBase admin;
        private void button2_Click(object sender, EventArgs e)
        {
            admin.Position += 1;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection conT = new SqlConnection("Data Source=(local);initial Catalog=BaseDeDatos; User Id=;Password=;Integrated Security=SSPI;");
            conT.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select * from TablaZ", conT);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
//Los parametros corresponden a el nombre de la propiedad del control a  enlazar, el origen de datos, y el miembro del origen de datos, en este caso la columna Nombre de la tabla propietarios

            Binding bind = new Binding("Text", dt, "nombre");
            textBox1.DataBindings.Add(bind);
           // Binding bind2 = new Binding("Text", dt, "edad");
            textBox2.DataBindings.Add("Text", dt, "edad");
            admin = this.BindingContext[dt];
        }

        private void button4_Click(object sender, EventArgs e)
        {
            admin.Position -= 1;
        }

        private void button3_Click(object sender, EventArgs e)
        {
           
        }
    }
}



Este código enlaza dos TextBox a una Base de Datos y utiliza los botones Siguiente y Atras para Navegar en la Base de Datos!
Un Cordial Saludo!
Ah si queres más información sobre C# y SQL puedo ayudarte, algo de idea tengo!
JaviMarciano
Novato
Novato
 
Mensajes: 11
Registrado: Mié Jul 18, 2007 4:52 pm


Volver a C# (CSharp)

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 0 invitados