Results 1 to 6 of 6

Thread: Connecting to DB (need a hint)

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Connecting to DB (need a hint)

    Im trying to connect to a database and then feed the data into a binding source that then is displayed in a datagridview that i can filter information out of.


    with that said. when i run trial querys in excel I get a sql statment like this


    SELECT gci_requests.ref_num, gci_requests.summary, gci_requests.open_date, gci_requests.resolve_date, gci_requests.category_name, gci_requests.group_name, gci_requests.status_name, gci_requests.customer_adaccount, gci_requests.customer_lastname, gci_requests.customer_phone, gci_requests.reportedby_adaccount, gci_requests.assignee_adaccount
    FROM mdb_rpt.dbo.gci_requests gci_requests
    WHERE (gci_requests.category_name Like '%Publishing USAT%')



    My question do i use the FROM part in my connection string or is that something else all together?




    Code:
                System.Data.Odbc.OdbcConnection cn;
                System.Data.Odbc.OdbcCommand cmd;
    
                string MyString,
                    CNstring;
                
                MyString = "Select * from gci_Requests";
                CNstring = "Driver={SQL Server};Server=mdb_rpt.dbo.gci_requests;UID=caiahd_rpt;PWD=dashboard;";
                
                
    
    
                //cn = new System.Data.Odbc.OdbcConnection("Driver={SQL Server};Server=" + myServerAddress  + ";Database=" + myDataBase  + ";Uid=" + myUsername + ";Pwd=" + myPassword + ";");
                cn = new System.Data.Odbc.OdbcConnection(CNstring);//Database=CA_ServiceDesk_RPT;
                cmd = new System.Data.Odbc.OdbcCommand(MyString, cn);
    //
                try
                {
                    cn.Open();
    
    
                    CA_bindingsource.DataSource = cmd;
                    dataGridView1.DataSource = CA_bindingsource;
                    MessageBox.Show("Connected");
                }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Connecting to DB (need a hint)

    If you're connecting to SQL Server then I strongly suggest that you use SqlClient rather than Odbc.

    That connection string is incorrect. The "Server" part of connection string is the name of the SQL Server instance you want to connect to. If you want to specify a database name too then that goes in a separate property of the connection string. For SqlClient its "Data Source" but I'm not sure for Odbc. The FROM part of your query is specifying the table to retrieve data from. The connection string cares nothing for tables.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: Connecting to DB (need a hint)

    If you are pulling from one table then you do not need to use the table name with the field name as well. This is not an issue but makes it more easily readable.
    Currently Using: VS 2005 Professional

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Connecting to DB (need a hint)


  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Connecting to DB (need a hint)

    so if the server name was gci-mocsqdb01


    would i want to use the

    SqlConnection (.NET)
    Standard Security

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

    And if so what would the catalog be?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Connecting to DB (need a hint)

    The Data Source is the server and instance name. The Initial Catalog is the name of the database.

    You're probably getting confused because of the loose terminology people generally use. People will call SQL Server a "database" but it's actually an RDBMS, i.e. a system that manages relational databases. Each installation of SQL Server manages multiple databases.

    In the attached image my Data Source would be "Serenity\SQL2000" and my Initial Catalog would be "RMSSample". Serenity is the name of the physical server on which SQL Server is installed and its a named instance called SQL2000. I have two other instances on the same server named SQL2005 and SQLExpress. This instance manages all the databases listed. The user name and password you provide are for the SQL Server instance, then you can connect to any database within that instance.
    Attached Images Attached Images  
    Last edited by jmcilhinney; Mar 14th, 2007 at 06:16 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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