Results 1 to 8 of 8

Thread: [RESOLVED] database connection question

  1. #1

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

    Resolved [RESOLVED] database connection question

    Okay

    the db admin has created a stored procedure called

    GetContent_MapPathByName

    which can be used like so

    exec GetContent_MapPathByName 'put-the-complete-story-name-here’



    I wanted to make an app that would allow the users to connected and do this from a winform



    what i have so far is

    C# Code:
    1. dt.Clear();
    2.             string adapter = string.Format("exec GetContent_MapPathByName ‘{0}’", TB_SearchString.Text);
    3.             string conn = "Data Source = dcsql.usatoday.us.ad.gannett.com; Initial Catalog = [CMS-Site2]; User Id = xxxxx; Password = xxxxx;";
    4.             SqlConnection sqlconn = new SqlConnection(conn);
    5.             SqlDataAdapter daSQL = new SqlDataAdapter(adapter, sqlconn);
    6.             daSQL.Fill(dt);


    where xxxxx has been replaced over the name and password ( not windows authentication)


    the problem i'm running into is that when i execute code it errors out on the daSQL.fill(dt) line

    and it gives the error "Cannot open database "[CMS-Site2]" requested by the login. The login failed.
    Login failed for user 'xxxxx'."


    But i know that the username and password are correct. becuase i can use thouse same credintals to log in to that server using MS sql managment studio ( and the credintals are under security for that pirticular database too)


    Does anyone see any other errors in what im doing?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: database connection question

    a few things:
    1) check with http://www.connectionstrings.com to make sure the connection string is correct. Try the db name w/o the brackets.
    2) While not related to your problem, I suggest NOT running your sproc as string like that... create a command object, set the commandtext to the NAME of the sproc, and add a parameter to it.... then run it.
    3) I had another point, but forgot what it was by the time I got here.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

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

    Re: database connection question

    Thanks for the reply

    1)

    there were more connection strings for sql server 2005 than stars in the sky. How do i know which one to pick?


    2)

    why would you avoid doing that

    2a) do you have a generic example of how you would access the sproc?


    Thanks for your help

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: database connection question

    1) The one that fits your circumstances... VB2005/2008 ... using ADO.NET ... SQLClient... etc...

    2) because you're losing the ability to run it like an sproc... running it the way you are, it runs as a text command. PLUS, what if some one searches for O'Brien ???? Now your SQL looks like this: EXEC usp_SomePRoc 'O'Brien' ... which will cause an error.

    2a) -- check out the FAQ section in the Database Development. There's a tutorial on how to run parameterized queries (which is what you really want to do).

    But in a nut shell... something along the lines of this:
    Code:
    SqlConnection sqlconn = new SqlConnection(conn);
    SqlCommand mySqlComm = new SqlCommand;
    mySqlComm.CommandText = ('GetContent_MapPathByName'); 
    mySqlComm.Parameters.AddWithValue('@ParameterName', TB_SearchString.Text);
    mySqlComm.Connection = sqlconn;
    dt.Merge(mySqlConn.ExecuteReader);
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: database connection question

    This one: http://www.connectionstrings.com/sql-server-2005#1

    You may need to specify that it's not a Trusted Connection as it might be attempting to use mixed authentication or your windows credentials.

  6. #6

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

    Re: database connection question

    I took the [] of my db name
    and the perens on the queriy were the wrong type after that

    but it works


    as for updateing to the proper sproc execution im going to give that a wirll

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: database connection question

    Funny thing is that I remember what that third item was supposed to be now... "take the [] off of the database name" .. hehehehehe.... looks like you beat me to it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: database connection question

    Didn't even think twice about that

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