Connection String
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Connection String

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Connection String

    Hi all and thanks in advanced for the help.

    I am VERY New to C#, and I have been playing around by watching videos in youtube etc.

    I come from the Delphi world. This is the problem I am getting

    In my delphy ADOconnection String (Which works very well) I have

    Data Source: 0166-PC\PDSTAFFING
    Initial Catalog: PDSTAFFING
    User ID: calosa_canis

    I am using Windows authentication to login into the Database

    In my C#2010 I have the following but I can't connect and gives me a Login Failure error.

    Hope you can help me out
    Once again thanks for all the help.


    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;
    using System.Data.Sql;

    namespace Test3
    {
    public partial class Form1 : Form
    {

    SqlConnection MainConnection = new SqlConnection("Data Source=0166-PC\\PDSTAFFING;User Id=carlosa_canis;Initial Catalog=PDSTAFFING;");


    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    try
    {
    MainConnection.Open();
    MainConnection.Close();
    }
    catch (Exception error)
    {
    MessageBox.Show(error.ToString());
    }

    }
    }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,473

    Re: Connection String

    Try
    Code:
    Server=0166-PC\PDSTAFFING;Database=PDSTAFFING;Integrated Security=TRUE
    Btw, you might want to let the database provider do it's job and manage the connection caching.

    In other words, it's tempting to want to open a connection and hold it open for a while because it seem inefficient to open/close connections all the time. The reality is that the database provider (SqlConnection) is smart, so when you call Close, it really just returns that connection to an internal connection pool instead of really closing the db connection.

    Given that, you should simply open a connection, do the work and then close the connection. To help with this, wrap the connection in a 'using' block (as it will call Close/Dispose automatically for you).

    Code:
    try
    {
      using( var cn = new SqlConnection("Server=0166-PC\PDSTAFFING;Database=PDSTAFFING;Integrated Security=TRUE") )
      {
        using( var cmd = new SqlCommand( ..., cn ) )
        {
            //
            // use the sql command
            //
        }                                      // SqlCommand object gets auto-cleaned up here
      }                                        // SqlConnection object gets auto-cleaned up here
    }
    catch( Exception ex )
    {
      ...
    }
    Last edited by Arjay; October 30th, 2011 at 10:10 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center