CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2012
    Posts
    3

    Connecting to SQL DB

    I am new to programming and have installed C#2010 Express and SQL Express 2008. Via OSQL I have created a database, a table and added a record, I can do a select * and see the record etc.
    I am trying to connect to this DB from C# with this:
    Code:
    SqlConnection con = new SqlConnection(@"Server=.\sqlexpress;Integrated Security=True;Connect Timeout=5;User Instance=True;database=JoJingles");
    This is the error I am getting:
    Name:  error.jpg
Views: 267
Size:  101.6 KB

  2. #2
    Join Date
    Jun 2012
    Posts
    3

    Re: Connecting to SQL DB

    OK, I feel I am slowly getting somewhere. I now dont get an error so can only assume it is connecting, however I dont see any output on the console (Other than the "Program Running" one)

    Code:
    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 JoJinglesDB
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                SqlConnection sqlConnection1 = new SqlConnection(@"Server=.\sqlexpress;Integrated Security=True;Connect Timeout=5;User Instance=True;database=JoJingles");
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader;
                System.Diagnostics.Debug.WriteLine("Program running");
                cmd.CommandText = "SELECT * FROM Customer";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sqlConnection1;
    
                sqlConnection1.Open();
    
                reader = cmd.ExecuteReader();
    
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        System.Diagnostics.Debug.WriteLine("{0}\t{1}", reader.GetInt32(0),
                            reader.GetString(1));
                      
                    }
                }
                else
                {
                    Console.WriteLine("No rows found.");
                }
                reader.Close();
    
                
    
            
            }
    
        }
    }

  3. #3
    Join Date
    Jun 2012
    Posts
    3

    Re: Connecting to SQL DB

    I should add that the above is a copy from another site and not my own code!

  4. #4
    Join Date
    Jul 2012
    Posts
    2

    Re: Connecting to SQL DB

    Have you installed SQL Server Express on your laptop, and created a database called Jo Jingles?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Connecting to SQL DB

    Quote Originally Posted by craigj View Post
    Code:
     SqlConnection sqlConnection1 = new SqlConnection(@"Server=.\sqlexpress;Integrated Security=True;Connect Timeout=5;User Instance=True;database=JoJingles");
    And what is the Provider? I don't see any in your connection string!
    Usually it should be "Provider=SQLNCLI10;" for SQL Server 2008...
    Or is it not needed in C#?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured