How do I execute a proc from a C# console app?

  • Not sure if this is the right place. . . looking for some help from anyone with C# experience. . .

    I was wondering if someone can provide me some direction on how to execute a SQL Server 2005 proc from a simple 2005 C# console application. I dont have a lot of experience with C# or visual studio and I can't find a lot of info on doing this. Basically I am using the exe to run on one server to execute the stored proc on another server. The proc doesn't return any data, so all I need it to do is simply start the job. I know this is hard to do without knowing my set up, so just a general idea to start me off in the right direction would be great.

    I know there are other ways to do this in SQL Server (like using SQL Agent) but I have to go this route.

    Thanks

  • I'm not familiar with C#, but I bet there's something in it comparable to the VB "SQLConnection" object. If you can find something like that, that should do it for you.

    Also, Visual Studio has wizards for setting up SQL connections and that kind of thing. Couldn't you use something like that?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Here's an example of using ADO.NET to start a stored procedure; built in Visual Studio 2005.

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Data.SqlClient;

    using System.Data;

    namespace ConsoleApplication1

    {

    class Program

    {

    static void Main(string[] args)

    {

    SqlConnection myConn = new SqlConnection("data source=myServer; Integrated Security=SSPI; Initial Catalog=myDatabase");

    SqlCommand myCmd = new SqlCommand("usp_proc1",myConn);

    myCmd.CommandType = CommandType.StoredProcedure;

    myConn.Open();

    myCmd.ExecuteNonQuery();

    myConn.Close();

    }

    }

    }

  • GSquared (6/17/2008)


    I'm not familiar with C#, but I bet there's something in it comparable to the VB "SQLConnection" object. If you can find something like that, that should do it for you.

    Also, Visual Studio has wizards for setting up SQL connections and that kind of thing. Couldn't you use something like that?

    does it have to be a console app. just make it a windows forms app, its nicer


    Everything you can imagine is real.

  • I was actually able to figure it out on my own, but thanks for the help. I did it differently but will keep the above code in mind for the future.

    I wanted a console app because all it is doing is running at night. We have a scheduling process that will kick it off. No point in having a windows app that has no front end.

    .Net can be pretty intimidating at first, but once you start doing stuff in it, it isn't that tough. Actually, its more exciting than SQL Server.

  • why don't you create a windows service application, instead of a console app. that way it can run all the time. just a thought


    Everything you can imagine is real.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply