Results 1 to 8 of 8

Thread: sp_executesql

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    sp_executesql

    hey

    how do you use sp_execute in C# for SQL????

    what is the structure/syntax like? is there an example you guys can give me please?

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Is that a SQL built in stored procedure?

    You can call stored procedures like:

    SqlCommand command = new SqlCommand();
    command.Connection = new SqlConnection("yourconnectionstring");
    command.CommandText = "sp_execute";
    command.CommandType = CommandType.StoredProcedure;
    command.Connection.Open();
    SqlDataReader rdr = command.ExecuteReader();
    // Use the data reader here.
    command.Connection.Close();

    My code may be wrong as I hand typed it, but the idea is there. If this doesn't make sense to you at all, then you should look up the SqlCommand, SqlConnection, SqlDataReader, SqlDataAdapter, and DataSet objects in the help.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    Thanks and yes it is a built in stored proc. I am trying to look for data insertion - since its different using this particular stored proc....and i need an example of data insertion using this stored proc

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    anyone?

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Are you sure you want to be using sp_execute (and not sp_executesql)? You see sp_execute a lot in the profiler along with sp_prepare, sp_unprepare.

    Since Books Online has no help on this at all, makes you think Microsoft never intended developers to use it.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    sp_executesql is the one i want, not sp_execute - i want to use sp_executesql


    using insert statements, in C# with sp_executesql - how?

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Quick example
    Code:
    			SqlConnection con = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;database=PatriotDispatch;server=MIKELAP");		
    			SqlCommand cmd = new SqlCommand("sp_executesql", con);
    			cmd.CommandType = CommandType.StoredProcedure;
    			string mySql = "INSERT INTO Logons (Logons_ORI, Logons_User_Name, Logons_Password) " +
    				"VALUES ('NM1234567', 'Techno', 'Edinburgh')";
    			SqlParameter param = cmd.Parameters.Add("@mySql", mySql);
    			con.Open();
    			int recordsAffected = cmd.ExecuteNonQuery();
    			con.Close();
    			MessageBox.Show(recordsAffected.ToString() + " records affected.");
    HTH,
    Mike

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    thanks ill give it a shot

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