SQL Server 2012 and C# in VS2010

Status
Not open for further replies.

carnageX

Private Joker,
Staff member
Messages
25,056
Location
Oregon
Long time no see, everybody! So, I'm working on a project for my Database management class, and we're supposed to create a database with a UI (either webpage based with something like MySQL & myPHP or Windows Forms from VS if we're using SQLServer 2012). I have all of my tables made, and have all the forms laid out how I want them. Problem is, we haven't really had any training on creating stored procedures from SQL (this class is supposed to be a "theory" class, and yet we're doing an actual DB project... so yeah), or calling the stored procedures from code (using C# for this), and interacting with the data in the tables. Anybody have any tips for me, or any good tutorials for me?

Thanks in advance, appreciate it.
 
Thanks Office. Me and one of my buddies worked on it a bit this weekend and sorta got things figured out.
In the New Query box using Management Studio, we did this for example:

Code:
create procedure ProcName (inputParameter varchar(50))
as
begin
<sql statements here>
end

Hit Execute on the toolbar, and it will create the stored procedure for you. You can then go under you Database on the Object Explorer, expand "Programmability" and then expand "Stored Procedures" and right click the new procedure that was made and choose "Execute stored procedure" and make sure it works correctly.

Calling from actual code was a bit more complex, and I don't have the code in front of me at the moment to copy and paste it in; I can do this later though if somebody wants it.
 
Ok, here's the code to connect to a database, and return a table that it gets. This example passes in a string to use as the stored procedure parameter.

Code:
        public static DataTable GenerateInvoice(string customerID)
        {
            DataTable returnTable = new DataTable();

            using (SqlConnection  db = new SqlConnection(@"Server=notebook\SQLEXPRESS;Database=DATABASENAME;User=DATABASEUSER;Password=PASSWORD"))
            {
                using (SqlCommand command = new SqlCommand("STOREDPROCEDURENAME", db)
                {
                    CommandType = CommandType.StoredProcedure

                })
                {
                    command.Parameters.Add(new SqlParameter("@INPUTPARAMS", customerID));
                    db.Open();
                    returnTable.Load(command.ExecuteReader());
                    db.Close();
                }
            }
            return returnTable;
        }
 
Status
Not open for further replies.
Back
Top Bottom