Link to home
Start Free TrialLog in
Avatar of amoran
amoran

asked on

db connection with visual studio

hi
Im using visual studio and i want to display the contents of a db on a windows form.

I go to
file
new
project
windows application

and then for the code i put

#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

#endregion

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

    public static void Main(string[] args)
    {
        SqlConnection SqlConn = new SqlConnection("server=localhost;DataBase=pubs;uid=sa;pwd=sa");
        SqlConn.Open();
        SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            MessageBox.Show(reader["page_id"].ToString());
        }
        SqlConn.Close();
    }


    }
}

but its giving me errors

      Error      1            'WindowsApplication11.Form1.Dispose(bool)': no suitable method found to override      C:\Documents and Settings\paulwhelan.VIZOR\My Documents\Visual Studio\Projects\WindowsApplication11\WindowsApplication11\Form1.Designer.cs      13      32                  


and its also saying it has 'more than one entry point defined'

Any ideas?
I'm a total newbie (in case its not obvious!)
Thanks
AM
Avatar of AlexFM
AlexFM

Create application again using Wizard and paste Main function code to it. I suggest you to create Console application, since this code doesn't use form:

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            SqlConnection SqlConn = new SqlConnection("server=localhost;DataBase=pubs;uid=sa;pwd=sa");
            SqlConn.Open();
            SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["page_id"].ToString());
            }
            SqlConn.Close();
        }
    }
}
Your code is missing some functions required by Windows Forms application. This is the way to do the same in Windows Forms application (form itself is not used):

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication10
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            SqlConnection SqlConn = new SqlConnection("server=localhost;DataBase=pubs;uid=sa;pwd=sa");
            SqlConn.Open();
            SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["page_id"].ToString());
            }
            SqlConn.Close();
        }
    }
}
Correction: in the second post line
Console.WriteLine(reader["page_id"].ToString());
should be replaced with:
MessageBox.Show(reader["page_id"].ToString());
Avatar of amoran

ASKER

ok
in my form1.cs i put the code in your second post

now it says

      Error      2            The namespace 'WindowsApplication10' already contains a definition for 'Form1'      C:\\Visual Studio\Projects\WindowsApplication12\WindowsApplication12\Form1.Designer.cs      14      17                  
      Warning      1            Cannot open a designer for the file because the class within it does not inherit from a class that can be visually designed.            0      -1                  


thanks!
AM
Create application again and after this paste this code replacing all existing code in the file.
Check this...

http://www.connectionstrings.com/

You will get the complete list of connection strings with the code.

A must check ;)

sun4sunday
Also you have to use this for the connection

using System.Data.SqlClient;

sun4sunday
Avatar of amoran

ASKER

ok now i do this


#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

#endregion

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

        private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection SqlConn = new SqlConnection("server=localhost;DataBase=pubs;uid=sa;pwd=sa");
            SqlConn.Open();
            SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                MessageBox.Show(reader["page_id"].ToString());
            }
            SqlConn.Close();


        }
    }
}

but i get (in reference to SqlConn.Open();)
sqlexception was unhandled

any ideas?
thanks for the help
AM
Avatar of amoran

ASKER

I think it can't find the pubs database?
Is it on the web?
Thanks
AM
According to connection string, SQL server shoul be installed on your computer: server=localhost. If you are working with XP, you cannot install SQL server and need to install MSDE which is available for download from Microsoft WEB site.
Avatar of amoran

ASKER

cool
I installed msde
now when i run it i get

Named Pipes Provider: The network path was not found.\r\n

Which I guess is a step in the right direction!
Thanks for the help
AM
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial