ASP.NET - Insertar datos en una tabla con 200 campos

 
Vista:
Imágen de perfil de nano

Insertar datos en una tabla con 200 campos

Publicado por nano (14 intervenciones) el 18/10/2015 18:05:50
Muy buenas a todos!!!
Os comento la duda que me surge a la hora de insertar registros con Visual Studio 2015 en C# en una tabla llamada CLIENTES en SQL SERVER que tiene unos 200 campos oblgatorios pero que yo realmente quiero concrear unos 10. Como ejemplo os comento que en mi clase cls_cliente.cs tengo lo siguiente:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
 
/// <summary>
/// Descripción breve de cls_cliente
/// </summary>
public class cls_cliente: cls_conexion
{
    string tabla = "CLIENTES";
    protected string NOMBRE, NOMBRE_FISCAL, DIRECCION, POBLACION, CP, PROVINCIA, TELEFONO1;
    protected int CODIGO, IMPORTE_ASEGURADO;
 
    public cls_cliente( int CODIGO, string NOMBRE, string DIRECCION, string CP, string PROVINCIA, string POBLACION, string TELEFONO1, int IMPORTE_ASEGURADO)
    {
        this.CODIGO = CODIGO;
        this.NOMBRE = NOMBRE;
        this.DIRECCION = DIRECCION;
        this.CP = CP;
        this.POBLACION = POBLACION;
        this.PROVINCIA = PROVINCIA;
        this.TELEFONO1 = TELEFONO1;
        this.IMPORTE_ASEGURADO = IMPORTE_ASEGURADO;
    }
    public int @codigo
    {
        set { CODIGO = value; }
        get { return CODIGO; }
    }
 
    public String @nombre
    {
        set { NOMBRE = value; }
        get { return NOMBRE; }
    }
    public String @direccion
    {
        set { DIRECCION = value; }
        get { return DIRECCION; }
    }
    public String @poblacion
    {
        set { POBLACION = value; }
        get { return POBLACION; }
    }
    public String @cp
    {
        set { CP = value; }
        get { return CP; }
    }
 
    public String @provincia
    {
        set { PROVINCIA = value; }
        get { return PROVINCIA; }
    }
 
    public String @telefono1
    {
        set { TELEFONO1 = value; }
        get { return TELEFONO1; }
    }
    public int @importe_asegurado
    {
        set { IMPORTE_ASEGURADO = value; }
        get { return IMPORTE_ASEGURADO; }
    }
 
    public void agregar()
    {
        conectar(tabla);
        DataRow fila;
        fila = data.Tables[tabla].NewRow();
        fila["@codigo"]= @codigo;
        fila["@nombre"] = @nombre;
        fila["@direccion"] = @direccion;
        fila["@poblacion"] = @poblacion;
        fila["@cp"] = @cp;
        fila["@provincia"] = @provincia;
        fila["@telefono1"] = @telefono1;
        fila["@importe_asegurado"] = @importe_asegurado;
 
        Data.Tables[tabla].Rows.Add(fila);
        AdaptadorDatos.Update(Data, tabla);
 
    }

Y en mi paginacliente.aspx.cs en un botón llamado BTNMOSTRAR tengo lo siguiente:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
 
 
 
public partial class pagecliente : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void BtnMostrar_Click(object sender, EventArgs e)
    {
        cls_cliente clte = new cls_cliente(0,"","","","","","",0);
        clte.@codigo = int.Parse(TxtCodCliente.Text);
        clte.@nombre = TxtNombre.Text;
        clte.direccion = TxtDireccion.Text;
        clte.@poblacion = TxtNombre.Text;
        clte.@cp = txtCP.Text;
        clte.@provincia = txtProvincia.Text;
        clte.@telefono1 = txtTelefono.Text;
        clte.@importe_asegurado = int.Parse(txtImporteAsegurado.Text);
        clte.agregar();
 
    }
}

De que manera puedo incluir los 190 campos restantes, que suelen ser 0 ó 1, sn tener que declararlos a todos individualmente?? Hay alguna forma?
Muchas gracias de antemano y un saludo a todos,
Nano.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Insertar datos en una tabla con 200 campos

Publicado por khristian (83 intervenciones) el 23/10/2015 13:44:49
Pero esos otros campos.... sus valores 0 o 1 dependen de cada insercion o son fijos????

Por que puedes omitir los que tomen valores predeterminados, haciendo que la BD los ponga por ti.

Por ejemplo: Vigente siempre que se crea un registro nuevo es True (1), entonces, lo defines como valor predeterminado en la tabla, así al crear un nuevo registro te olvidas de ese campo.

Ahora, para lo demas, podrias pasar una lista tipo "nombre" "valor", y la recorres haciendo:

fila[lista.nombre] = lista.valor;
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar