Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/12/2009, 12:29
lince_0011
 
Fecha de Ingreso: septiembre-2009
Mensajes: 63
Antigüedad: 14 años, 8 meses
Puntos: 0
Conexion Sql Express con programa hecho en C#

Saludos amigos de foros del web, hoy en la universidad me encargaron de proyecto final crear un programa en C# de modo ventanas,y este mismo enlazarlo a una base de datos en sql. Ya tengo hecho el programa y funciona bien, tambien la base de datos ya la tengo creada pero al intentar correr el programa con toda la base de datos me sale un errorsito el cual no se como solucionar.

Aqui el codigo del programa con todo y codigo para enlazar a la base:

Código:
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 baseproyecto
{
    public partial class Form1 : Form
    {
        public String conStr = "Data Source=.\\Sqlexpress; Initial Catalog=video;Integrated Security=true";
        public SqlConnection myCon = new SqlConnection();
        public SqlCommand myComm = new SqlCommand();
        public SqlDataAdapter myAdapter = new SqlDataAdapter();
        public String nombre, telefono, correo,direccion,ciudad;
        public int ID, i;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ID = int.Parse(textBox1.Text);
            myCon.ConnectionString = conStr;
            myComm.CommandText = "Select nombre,telefono,correo,direccion,ciudad From cliente Where id_cliente ='" + ID + "' ";
            myComm.Connection = myCon;
            myAdapter.SelectCommand = myComm;
            DataSet myds = new DataSet();
            int numrows = myAdapter.Fill(myds, "video");
            if (numrows > 0)
            {
                textBox2.Text = myds.Tables["cliente"].Rows[0]["nombre"].ToString();
                textBox3.Text = myds.Tables["cliente"].Rows[0]["telefono"].ToString();
                textBox4.Text = myds.Tables["cliente"].Rows[0]["correo"].ToString();
                textBox5.Text = myds.Tables["cliente"].Rows[0]["direccion"].ToString();
                textBox6.Text = myds.Tables["cliente"].Rows[0]["ciudad"].ToString();
            }
            myCon.Close();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //creamos el objeto de la conexion
            SqlConnection mycon = new SqlConnection();
            //asignamos la cadena de la conexion
            mycon.ConnectionString = "Data source=.\\SqlExpress;initial catalog=video;" + "integrated security=true";
            //creamos un objeto de tipo comando
            SqlCommand mycommand = new SqlCommand();
            //asignamos consulta
            mycommand.CommandText = "select*from cliente";
            //conectamos el comando con la conexion a la base de datos
            mycommand.Connection = mycon;
            //creamos el adaptador para traer informacion de la base de datos
            SqlDataAdapter myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = mycommand;
            //creamos un dataset para crear una imagen de la base de datos
            DataSet myds = new DataSet();
            //rellenamos el adaptador con la tabla a utilizar
            myAdapter.Fill(myds,"video");
            dataGridView1.DataSource = myds;
            dataGridView1.DataMember = "video";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ID = int.Parse(textBox1.Text);
            nombre = textBox2.Text;
            telefono = textBox3.Text;
            correo = textBox4.Text;
            direccion = textBox5.Text;
            ciudad = textBox6.Text;
            myCon.ConnectionString = conStr;
            myComm.CommandText = "UPDATE cliente SET Nombre='" + nombre + "',Telefono='" + telefono + "',"
                                 + "correo='" + correo + "',direccion='" + direccion+ "',ciudad='"+ciudad+" 'WHERE Id_cliente= '" + ID + "'";
            myComm.Connection = myCon;
            myCon.Open();
            i= myComm.ExecuteNonQuery();
            myCon.Close();
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string ConStr;
            int ID;
            ID = int.Parse(textBox1.Text);
            ConStr = "Data Source =.\\Sqlexpress; Initial catalog=video; integrated security=true";
                SqlConnection mycon=new SqlConnection();
            mycon.ConnectionString=ConStr;
            SqlCommand mycommand=new SqlCommand();
            mycommand.CommandText="Delete from cliente where id_cliente='" +ID+ " '";
            mycommand.Connection=mycon;
            mycon.Open();
            int i=mycommand.ExecuteNonQuery();
            mycon.Close();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            nombre = textBox2.Text;
            telefono = textBox5.Text;
            correo = textBox4.Text;
            direccion = textBox3.Text;
            ciudad = textBox6.Text;
            myCon.ConnectionString = conStr;
            myComm.CommandText = "INSERT INTO cliente(Nombre,Telefono,correo,direccion,ciudad)"
            + " VALUES (' " + nombre + " ',' " + telefono + " ',' " + correo + " ','" + direccion + " ',' " + ciudad + " ')";
            myComm.Connection = myCon;
            myCon.Open();
            i = myComm.ExecuteNonQuery();
            myCon.Close();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void but_cargar_Click(object sender, EventArgs e)
        {
            myCon.ConnectionString = conStr;
            myComm.CommandText = "Select * From cliente";
            myComm.Connection = myCon;
            myAdapter.SelectCommand = myComm;
            DataSet myds = new DataSet();
            myAdapter.Fill(myds, "cliente");
            dataGridView1.DataSource = myds;
            dataGridView1.DataMember = "cliente";

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ID = int.Parse(textBox1.Text);
            myCon.ConnectionString = conStr;
            myComm.CommandText = "Select nombre,direccion,telefono,correo,ciudad From cliente Where id_cliente ='" + ID + "'";
            myComm.Connection = myCon;
            myAdapter.SelectCommand = myComm;
            DataSet myds = new DataSet();
            int numrows = myAdapter.Fill(myds, "cliente");
            if (numrows > 0)
            {
                textBox2.Text = myds.Tables["cliente"].Rows[0]["nombre"].ToString();
                textBox3.Text = myds.Tables["cliente"].Rows[0]["direccion"].ToString();
                textBox4.Text = myds.Tables["cliente"].Rows[0]["telefono"].ToString();
                textBox5.Text = myds.Tables["cliente"].Rows[0]["correo"].ToString();
                textBox6.Text = myds.Tables["cliente"].Rows[0]["ciudad"].ToString();
            }
            myCon.Close();

        }

        
    }
}
Cuando corro el programa me dice estos dos mensajes:
No se controlo SqlException y El nombre de objeto 'cliente' no es válido., cabe destacar que tanto en todo el codigo puse el nombre de la base de datos correctamente y claro tambien puse correctamente el de las tablas. Tambien la base de datos la ejecute en Sql y no me sali ningun error.

Cual seria la solucion a esto?

Saludos