I was wondering if anyone could tell me why my code is not displaying anything. My connection is correct because I did copy it from a program that already works properly. *Beginner Student*

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection("user id=sa;" +
                                       "password=JFK9j0b;server=RPLSQL;" +
                                       "Trusted_Connection=yes;" +
                                       "database=TDSSW; " +
                                       "connection timeout=30000");


            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            try
            {
                System.Data.SqlClient.SqlDataReader myReader = null;
                System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand("select itemid from laitem",
                                                         myConnection);
                myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    Console.WriteLine(myReader["ItemId"].ToString());
                   // Console.WriteLine(myReader["Column2"].ToString());
                    Console.ReadLine();
                    

            
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }
    }
}

Recommended Answers

All 4 Replies

You can create a new connection from Server Explorer and copy connection strings from properties in your project. Following link contains steps to create connection from Server Explorer

http://msdn.microsoft.com/en-us/library/s4yys16a(v=VS.90).aspx

You can try following Connectionstring

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

You can also get list of connection string from http://connectionstrings.com/sql-server-2008

Hope it helps,
Mitja

Maybe "select itemid from laitem" returns an empty result set.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace SelectItems
{
    class Program
    {
        internal static string ConnectionString = "Data Source=TROJANX;Initial Catalog=ENTERPRISE4;Integrated Security=True;Pooling=False";
        
        static void Main(string[] args)
        {
            try
            {
                using (SqlConnection sco = new SqlConnection())
                {
                    sco.ConnectionString = ConnectionString;
                    sco.Open();
                    using (SqlCommand sc = new SqlCommand())
                    {
                        sc.CommandType = CommandType.Text;
                        sc.CommandText = "select * from laitem";
                        sc.Connection = sco;
                        using (SqlDataReader sr = sc.ExecuteReader())
                        {
                            while (sr.Read())
                            {
                                Console.WriteLine(sr["itemid"].ToString() + " Item: " + sr["itemname"].ToString());

                                Console.ReadLine();
                            }
                        }
                    }
                }
            }
            catch(Exception e)
            {
               Console.WriteLine(e.Message.ToString());
               Console.ReadLine();
            }
        }
    }
}

Yes, and?
Please tell us whats the matter? Is it working or not?

Otherwise I did some minor changes in your code, please let me know if its working (if not, please check the connection string).

private static string _connectionString = @"Data Source=TROJANX;Initial Catalog=ENTERPRISE4;Integrated Security=True;Pooling=False";
        private void a()
        {
            using (SqlConnection sqlConn = new SqlConnection(_connectionString))
            {               
                using (SqlCommand sc = new SqlCommand())
                {
                    sc.CommandType = CommandType.Text;
                    sc.CommandText = "select * from laitem";
                    sc.Connection = sqlConn;
                    sqlConn.Open();
                    using (SqlDataReader sr = sc.ExecuteReader())
                    {
                        while (sr.Read())
                        {
                            Console.WriteLine(sr["itemid"].ToString() + " Item: " + sr["itemname"].ToString());
                            Console.ReadLine();
                        }
                    }
                    sqlConn.Close();
                }
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.