new with c# and sql server 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RiskyHunter
    New Member
    • Mar 2007
    • 29

    #1

    new with c# and sql server 2005

    am new with c# so plz be patient with me..
    i have created a database and i wanna connect my application with it

    while am searching i found this code:

    using System;
    using System.Data.Sql Client;

    namespace ConsoleCSharp
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class DataReader_SQL
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    //
    // TODO: Add code to start application here
    //
    try
    {
    SqlConnection thisConnection = new SqlConnection(" Data Source=,1433;da tabase=Database 2;User id=;Password=;" );
    SqlCommand thisCommand = thisConnection. CreateCommand() ;
    thisCommand.Com mandText = "SELECT CustomerID, CompanyName FROM Customers";
    SqlDataReader thisReader = thisCommand.Exe cuteReader();
    while (thisReader.Rea d())
    {
    Console.WriteLi ne("\t{0}\t{1}" , thisReader["CustomerID "], thisReader["CompanyNam e"]);
    }
    thisReader.Clos e();
    thisConnection. Close();

    }
    catch (SqlException e)
    {
    Console.WriteLi ne(e.Message);
    }

    }
    }
    }

    so my question is how can i change the user id and password i have tried to
    add the password in the xml configuration file however i still have exception
    plz help me out i wanna knew how can i connect my application with the
    database using the connection string

    thanks alot
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by RiskyHunter
    am new with c# so plz be patient with me..
    i have created a database and i wanna connect my application with it

    while am searching i found this code:

    using System;
    using System.Data.Sql Client;

    namespace ConsoleCSharp
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class DataReader_SQL
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    //
    // TODO: Add code to start application here
    //
    try
    {
    SqlConnection thisConnection = new SqlConnection(" Data Source=,1433;da tabase=Database 2;User id=;Password=;" );
    SqlCommand thisCommand = thisConnection. CreateCommand() ;
    thisCommand.Com mandText = "SELECT CustomerID, CompanyName FROM Customers";
    SqlDataReader thisReader = thisCommand.Exe cuteReader();
    while (thisReader.Rea d())
    {
    Console.WriteLi ne("\t{0}\t{1}" , thisReader["CustomerID "], thisReader["CompanyNam e"]);
    }
    thisReader.Clos e();
    thisConnection. Close();

    }
    catch (SqlException e)
    {
    Console.WriteLi ne(e.Message);
    }

    }
    }
    }

    so my question is how can i change the user id and password i have tried to
    add the password in the xml configuration file however i still have exception
    plz help me out i wanna knew how can i connect my application with the
    database using the connection string

    thanks alot
    look here for connection string details...

    All connection strings in one place. Find the syntax for your database connection using ADO.NET, ADO, ODBC, OLEDB, C#, VB, VB.NET, ASP.NET and more.

    Comment

    • RiskyHunter
      New Member
      • Mar 2007
      • 29

      #3
      dip_developer thanks a lot 4 ur reply

      i have tried to use this one:
      "Data Source=myServer Address;Initial Catalog=myDataB ase;User Id=myUsername;P assword=myPassw ord;"

      however, i still have problem in opening the connection;

      i just wanna knew if i can set up the user id and the password using the xml configuration file?? if that wrong plz tell me how can i do it

      thanks

      Comment

      • kunal pawar
        Contributor
        • Oct 2007
        • 297

        #4
        Write simple code

        SqlConnection thisConnection = new SqlConnection(u r connection string);
        thisConnection. open()
        SqlCommand thisCommand = new SqlCommand()
        thisCommand.con nection = thisConnection
        thisCommand.Com mandText = "SELECT CustomerID, CompanyName FROM Customers";
        SqlDataReader thisReader = thisCommand.Exe cuteReader();
        while (thisReader.Rea d())
        {
        Console.WriteLi ne("\t{0}\t{1}" , thisReader["CustomerID "], thisReader["CompanyNam e"]);
        }
        thisReader.Clos e();
        thisConnection. Close();

        Comment

        • RiskyHunter
          New Member
          • Mar 2007
          • 29

          #5
          kunal pawar i still have problem T_T

          An error has occurred while establishing a connection to the server. When conne
          cting to SQL Server 2005, this failure may be caused by the fact that under the
          default settings SQL Server does not allow remote connections. (provider: Named
          Pipes Provider, error: 40 - Could not open a connection to SQL Server)

          Comment

          • gkn
            New Member
            • Feb 2008
            • 4

            #6
            It seems like the SQL server instance is not correct. Please check this first.

            Comment

            • RiskyHunter
              New Member
              • Mar 2007
              • 29

              #7
              question about the connection string in c#

              i created a database using sql sever 2005 using c# and i wanna connect to that database using this code:

              Code:
              using System;
              using System.Data.SqlClient;
              
              namespace ConsoleCSharp
              {
              /// <summary>
              /// Summary description for Class1.
              /// </summary>
              class DataReader_SQL
              {
              /// <summary>
              /// The main entry point for the application.
              /// </summary>
              [STAThread]
              static void Main(string[] args)
              {
              //
              // TODO: Add code to start application here
              //
              try
              {
              SqlConnection thisConnection = new SqlConnection("Data Source=,1433;database=Database2;User id=;Password=;");
              SqlCommand thisCommand = thisConnection.CreateCommand();
              thisCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers";
              SqlDataReader thisReader = thisCommand.ExecuteReader();
              while (thisReader.Read())
              {
              Console.WriteLine("\t{0}\t{1}", thisReader["CustomerID"], thisReader["CompanyName"]);
              }
              thisReader.Close();
              thisConnection.Close();
              
              }
              catch (SqlException e)
              {
              Console.WriteLine(e.Message);
              }
              
              }
              }
              }
              the problem is that when i create the database i get this connection string:

              "Data Source=.\SQLEXP RESS;AttachDbFi lename="D:\Docu ments and Settings\200302 521\My Documents\Visua l Studio 2008\Projects\C onsoleApplicati on1\ConsoleAppl ication1\Databa se1.mdf";Integr ated Security=True;U ser Instance=True"

              however in the code they used different connection string which is:

              "Data Source=,1433;da tabase=Database 2;User id=;Password=;"

              i wanna knew what is the different between these two string because i tried both
              of them but i still get an errors.

              thanks a lot;

              Comment

              Working...