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=loca lhost;Data Base=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["pa ge_id"].To String());
}
SqlConn.Close();
}
}
}
but its giving me errors
Error 1 'WindowsApplication11.Form 1.Dispose( bool)': no suitable method found to override C:\Documents and Settings\paulwhelan.VIZOR\ My Documents\Visual Studio\Projects\WindowsApp lication11 \WindowsAp plication1 1\Form1.De signer.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
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=loca
SqlConn.Open();
SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MessageBox.Show(reader["pa
}
SqlConn.Close();
}
}
}
but its giving me errors
Error 1 'WindowsApplication11.Form
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
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.Cont ainer 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.F orm1_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=loca lhost;Data Base=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();
}
}
}
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.Cont
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.F
}
#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=loca
SqlConn.Open();
SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["
}
SqlConn.Close();
}
}
}
Correction: in the second post line
Console.WriteLine(reader[" page_id"]. ToString() );
should be replaced with:
MessageBox.Show(reader["pa ge_id"].To String());
Console.WriteLine(reader["
should be replaced with:
MessageBox.Show(reader["pa
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\WindowsApp lication12 \WindowsAp plication1 2\Form1.De signer.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
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\WindowsApp
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
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
using System.Data.SqlClient;
sun4sunday
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=loca lhost;Data Base=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["pa ge_id"].To String());
}
SqlConn.Close();
}
}
}
but i get (in reference to SqlConn.Open();)
sqlexception was unhandled
any ideas?
thanks for the help
AM
#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=loca
SqlConn.Open();
SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MessageBox.Show(reader["pa
}
SqlConn.Close();
}
}
}
but i get (in reference to SqlConn.Open();)
sqlexception was unhandled
any ideas?
thanks for the help
AM
ASKER
I think it can't find the pubs database?
Is it on the web?
Thanks
AM
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SqlConnection SqlConn = new SqlConnection("server=loca
SqlConn.Open();
SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["
}
SqlConn.Close();
}
}
}