2 // SqlCommandTest.cs - NUnit Test Cases for testing the
5 // Umadevi S (sumadevi@novell.com)
6 // Sureshkumar T (tsureshkumar@novell.com)
7 // Senganal T (tsenganal@novell.com)
9 // Copyright (c) 2004 Novell Inc., and the individuals listed
10 // on the ChangeLog entries.
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Data.Common;
35 using System.Data.SqlClient;
37 using NUnit.Framework;
39 namespace MonoTests.System.Data.SqlClient
42 [Category ("sqlserver")]
43 public class SqlCommandTest
46 public SqlConnection conn = null ;
47 SqlCommand cmd = null;
48 string connectionString = ConnectionManager.Singleton.ConnectionString;
56 public void TearDown ()
63 public void ConstructorTest ()
65 // Test Default Constructor
66 cmd = new SqlCommand ();
67 Assert.AreEqual (String.Empty, cmd.CommandText,
68 "#1 Command Test should be empty");
69 Assert.AreEqual (30, cmd.CommandTimeout,
70 "#2 CommandTimeout should be 30");
71 Assert.AreEqual (CommandType.Text, cmd.CommandType,
72 "#3 CommandType should be text");
73 Assert.IsNull (cmd.Connection, "#4 Connection Should be null");
74 Assert.AreEqual (0, cmd.Parameters.Count,
75 "#5 Parameter shud be empty");
77 // Test Overloaded Constructor
78 String cmdText = "select * from tbl1" ;
79 cmd = new SqlCommand (cmdText);
80 Assert.AreEqual (cmdText, cmd.CommandText,
81 "#5 CommandText should be the same as passed");
82 Assert.AreEqual (30, cmd.CommandTimeout,
83 "#6 CommandTimeout should be 30");
84 Assert.AreEqual (CommandType.Text, cmd.CommandType,
85 "#7 CommandType should be text");
86 Assert.IsNull (cmd.Connection , "#8 Connection Should be null");
88 // Test Overloaded Constructor
89 SqlConnection conn = new SqlConnection ();
90 cmd = new SqlCommand (cmdText , conn);
91 Assert.AreEqual (cmdText, cmd.CommandText,
92 "#9 CommandText should be the same as passed");
93 Assert.AreEqual (30, cmd.CommandTimeout,
94 "#10 CommandTimeout should be 30");
95 Assert.AreEqual (CommandType.Text, cmd.CommandType,
96 "#11 CommandType should be text");
97 Assert.AreSame (cmd.Connection, conn, "#12 Connection Should be same");
99 // Test Overloaded Constructor
100 SqlTransaction trans = null ;
102 conn = new SqlConnection (connectionString);
104 trans = conn.BeginTransaction ();
105 cmd = new SqlCommand (cmdText, conn, trans);
106 Assert.AreEqual (cmdText, cmd.CommandText,
107 "#9 CommandText should be the same as passed");
108 Assert.AreEqual (30, cmd.CommandTimeout,
109 "#10 CommandTimeout should be 30");
110 Assert.AreEqual (CommandType.Text, cmd.CommandType,
111 "#11 CommandType should be text");
112 Assert.AreEqual (cmd.Connection, conn,
113 "#12 Connection Should be null");
114 Assert.AreEqual (cmd.Transaction, trans,
115 "#13 Transaction Property should be set");
117 // Test if parameters are reset to Default Values
118 cmd = new SqlCommand ();
119 Assert.AreEqual (String.Empty, cmd.CommandText,
120 "#1 Command Test should be empty");
121 Assert.AreEqual (30, cmd.CommandTimeout,
122 "#2 CommandTimeout should be 30");
123 Assert.AreEqual (CommandType.Text, cmd.CommandType,
124 "#3 CommandType should be text");
125 Assert.IsNull (cmd.Connection, "#4 Connection Should be null");
132 public void ExecuteScalarTest ()
134 conn = new SqlConnection (connectionString);
135 cmd = new SqlCommand ("" , conn);
136 cmd.CommandText = "Select count(*) from numeric_family where id<=4";
138 //Check Exception is thrown when executed on a closed connection
140 cmd.ExecuteScalar ();
141 Assert.Fail ("#1 InvalidOperation Exception must be thrown");
142 }catch (AssertionException e) {
144 }catch (Exception e) {
145 Assert.AreEqual (typeof (InvalidOperationException), e.GetType (),
146 "#2 Incorrect Exception : " + e.StackTrace);
149 // Check the Return value for a Correct Query
152 result = cmd.ExecuteScalar ();
153 Assert.AreEqual (4, (int)result, "#3 Query Result returned is incorrect");
155 cmd.CommandText = "select id , type_bit from numeric_family order by id asc" ;
156 result = Convert.ToInt32 (cmd.ExecuteScalar ());
157 Assert.AreEqual (1, result,
158 "#4 ExecuteScalar Should return (1,1) the result set" );
160 cmd.CommandText = "select id from numeric_family where id=-1";
161 result = cmd.ExecuteScalar ();
162 Assert.IsNull (result, "#5 Null should be returned if result set is empty");
164 // Check SqlException is thrown for Invalid Query
165 cmd.CommandText = "select count* from numeric_family";
167 result = cmd.ExecuteScalar ();
168 Assert.Fail ("#6 InCorrect Query should cause an SqlException");
169 }catch (AssertionException e) {
171 }catch (Exception e) {
172 Assert.AreEqual (typeof(SqlException), e.GetType(),
173 "#7 Incorrect Exception : " + e.StackTrace);
177 // Parameterized stored procedure calls
180 string string_value = "output value changed";
181 string return_value = "first column of first rowset";
184 "create procedure #tmp_executescalar_outparams "+
185 " (@p1 int, @p2 int out, @p3 varchar(200) out) "+
187 "select '" + return_value + "' as 'col1', @p1 as 'col2' "+
188 "set @p2 = @p2 * 2 "+
189 "set @p3 = N'" + string_value + "' "+
190 "select 'second rowset' as 'col1', 2 as 'col2' "+
193 cmd.CommandType = CommandType.Text;
194 cmd.ExecuteNonQuery ();
196 cmd.CommandText = "#tmp_executescalar_outparams";
197 cmd.CommandType = CommandType.StoredProcedure;
199 SqlParameter p1 = new SqlParameter ();
200 p1.ParameterName = "@p1";
201 p1.Direction = ParameterDirection.Input;
202 p1.DbType = DbType.Int32;
203 p1.Value = int_value;
204 cmd.Parameters.Add (p1);
206 SqlParameter p2 = new SqlParameter ();
207 p2.ParameterName = "@p2";
208 p2.Direction = ParameterDirection.InputOutput;
209 p2.DbType = DbType.Int32;
210 p2.Value = int_value;
211 cmd.Parameters.Add (p2);
213 SqlParameter p3 = new SqlParameter ();
214 p3.ParameterName = "@p3";
215 p3.Direction = ParameterDirection.Output;
216 p3.DbType = DbType.String;
218 cmd.Parameters.Add (p3);
220 result = cmd.ExecuteScalar ();
221 Assert.AreEqual (return_value, result, "#8 ExecuteScalar Should return 'first column of first rowset'");
222 Assert.AreEqual (int_value * 2, p2.Value, "#9 ExecuteScalar should fill the parameter collection with the outputted values");
223 Assert.AreEqual (string_value, p3.Value, "#10 ExecuteScalar should fill the parameter collection with the outputted values");
228 cmd.ExecuteScalar ();
229 Assert.Fail ("#11 Query should throw System.InvalidOperationException due to size = 0 and value = null");
231 catch (AssertionException e) {
234 catch (Exception e) {
235 Assert.AreEqual (typeof (InvalidOperationException), e.GetType (),
236 "#12 Incorrect Exception : " + e.StackTrace);
244 public void ExecuteNonQuery ()
246 conn = new SqlConnection (connectionString);
247 cmd = new SqlCommand ("", conn);
250 // Test for exceptions
251 // Test exception is thrown if connection is closed
252 cmd.CommandText = "Select id from numeric_family where id=1";
254 cmd.ExecuteNonQuery ();
255 Assert.Fail ("#1 Connextion shud be open");
256 }catch (AssertionException e) {
258 }catch (Exception e) {
259 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
260 "#2 Incorrect Exception : " + e);
263 // Test Exception is thrown if Query is incorrect
265 cmd.CommandText = "Select id1 from numeric_family";
267 cmd.ExecuteNonQuery ();
268 Assert.Fail ("#1 invalid Query");
269 }catch (AssertionException e) {
271 }catch (Exception e) {
272 Assert.AreEqual (typeof(SqlException), e.GetType(),
273 "#2 Incorrect Exception : " + e);
276 // Test Select/Insert/Update/Delete Statements
277 SqlTransaction trans = conn.BeginTransaction ();
278 cmd.Transaction = trans;
281 cmd.CommandText = "Select id from numeric_family where id=1";
282 result = cmd.ExecuteNonQuery ();
283 Assert.AreEqual (-1, result, "#1");
285 cmd.CommandText = "Insert into numeric_family (id,type_int) values (100,200)";
286 result = cmd.ExecuteNonQuery ();
287 Assert.AreEqual (1, result, "#2 One row shud be inserted");
289 cmd.CommandText = "Update numeric_family set type_int=300 where id=100";
290 result = cmd.ExecuteNonQuery ();
291 Assert.AreEqual (1, result, "#3 One row shud be updated");
293 // Test Batch Commands
294 cmd.CommandText = "Select id from numeric_family where id=1;";
295 cmd.CommandText += "update numeric_family set type_int=10 where id=1000";
296 cmd.CommandText += "update numeric_family set type_int=10 where id=100";
297 result = cmd.ExecuteNonQuery ();
298 Assert.AreEqual (1, result, "#4 One row shud be updated");
300 cmd.CommandText = "Delete from numeric_family where id=100";
301 result = cmd.ExecuteNonQuery ();
302 Assert.AreEqual (1, result, "#5 One row shud be deleted");
309 // Parameterized stored procedure calls
312 string string_value = "output value changed";
315 "create procedure #tmp_executescalar_outparams " +
316 " (@p1 int, @p2 int out, @p3 varchar(200) out) " +
318 "select 'test' as 'col1', @p1 as 'col2' " +
319 "set @p2 = @p2 * 2 " +
320 "set @p3 = N'" + string_value + "' " +
321 "select 'second rowset' as 'col1', 2 as 'col2' " +
324 cmd.CommandType = CommandType.Text;
325 cmd.ExecuteNonQuery ();
327 cmd.CommandText = "#tmp_executescalar_outparams";
328 cmd.CommandType = CommandType.StoredProcedure;
330 SqlParameter p1 = new SqlParameter ();
331 p1.ParameterName = "@p1";
332 p1.Direction = ParameterDirection.Input;
333 p1.DbType = DbType.Int32;
334 p1.Value = int_value;
335 cmd.Parameters.Add (p1);
337 SqlParameter p2 = new SqlParameter ();
338 p2.ParameterName = "@p2";
339 p2.Direction = ParameterDirection.InputOutput;
340 p2.DbType = DbType.Int32;
341 p2.Value = int_value;
342 cmd.Parameters.Add (p2);
344 SqlParameter p3 = new SqlParameter ();
345 p3.ParameterName = "@p3";
346 p3.Direction = ParameterDirection.Output;
347 p3.DbType = DbType.String;
349 cmd.Parameters.Add (p3);
351 cmd.ExecuteNonQuery ();
352 Assert.AreEqual (int_value * 2, p2.Value, "#6 ExecuteNonQuery should fill the parameter collection with the outputted values");
353 Assert.AreEqual (string_value, p3.Value, "#7 ExecuteNonQuery should fill the parameter collection with the outputted values");
357 public void ExecuteReaderTest ()
359 SqlDataReader reader = null;
360 conn = new SqlConnection (connectionString);
362 // Test exception is thrown if conn is closed
363 cmd = new SqlCommand ("Select count(*) from numeric_family");
365 reader = cmd.ExecuteReader ();
366 }catch (AssertionException e) {
368 }catch (Exception e) {
369 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
370 "#1 Incorrect Exception");
374 // Test exception is thrown for Invalid Query
375 cmd = new SqlCommand ("InvalidQuery", conn);
377 reader = cmd.ExecuteReader ();
378 Assert.Fail ("#1 Exception shud be thrown");
379 }catch (AssertionException e) {
381 }catch (Exception e) {
382 Assert.AreEqual (typeof(SqlException), e.GetType (),
383 "#2 Incorrect Exception : " + e);
387 // Test SqlException is thrown if a row is locked
388 // should lock a particular row and then modify it
392 // Test Connection cannot be modified when reader is in use
393 // NOTE : msdotnet contradicts documented behavior
394 cmd.CommandText = "select * from numeric_family where id=1";
395 reader = cmd.ExecuteReader ();
397 conn.Close (); // valid operation
398 conn = new SqlConnection (connectionString);
401 // NOTE msdotnet contradcits documented behavior
402 // If the above testcase fails, then this shud be tested
403 // Test connection can be modified once reader is closed
406 conn = new SqlConnection (connectionString); // valid operation
411 public void ExecuteReaderCommandBehaviorTest ()
413 // Test for command behaviors
414 DataTable schemaTable = null;
415 SqlDataReader reader = null;
417 conn = new SqlConnection (connectionString);
419 cmd = new SqlCommand ("", conn);
420 cmd.CommandText = "Select id from numeric_family where id <=4 order by id asc;";
421 cmd.CommandText += "Select type_bit from numeric_family where id <=4 order by id asc";
423 // Test for default command behavior
424 reader = cmd.ExecuteReader ();
428 while (reader.Read ())
430 Assert.AreEqual (4, rows, "#1 Multiple rows shud be returned");
433 }while (reader.NextResult());
434 Assert.AreEqual (2, results, "#2 Multiple result sets shud be returned");
437 // Test if closing reader, closes the connection
438 reader = cmd.ExecuteReader (CommandBehavior.CloseConnection);
440 Assert.AreEqual (ConnectionState.Closed, conn.State,
441 "#3 Command Behavior is not followed");
444 // Test if row info and primary Key info is returned
445 reader = cmd.ExecuteReader (CommandBehavior.KeyInfo);
446 schemaTable = reader.GetSchemaTable ();
447 Assert.IsTrue(reader.HasRows, "#4 Data Rows shud also be returned");
448 Assert.IsTrue ((bool)schemaTable.Rows[0]["IsKey"],
449 "#5 Primary Key info shud be returned");
452 // Test only column information is returned
453 reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly);
454 schemaTable = reader.GetSchemaTable ();
455 Assert.IsFalse (reader.HasRows, "#6 row data shud not be returned");
456 Assert.AreEqual(DBNull.Value, schemaTable.Rows[0]["IsKey"],
457 "#7 Primary Key info shud not be returned");
458 Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
459 "#8 Schema Data is Incorrect");
462 // Test only one result set (first) is returned
463 reader = cmd.ExecuteReader (CommandBehavior.SingleResult);
464 schemaTable = reader.GetSchemaTable ();
465 Assert.IsFalse (reader.NextResult(),
466 "#9 Only one result set shud be returned");
467 Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
468 "#10 The result set returned shud be the first result set");
471 // Test only one row is returned for all result sets
472 // msdotnet doesnt work correctly.. returns only one result set
473 reader = cmd.ExecuteReader (CommandBehavior.SingleRow);
477 while (reader.Read ())
479 Assert.AreEqual (1, rows, "#11 Only one row shud be returned");
482 }while (reader.NextResult());
483 // NOTE msdotnet contradicts documented behavior.
484 // Multiple result sets shud be returned , and in this case : 2
485 //Assert.AreEqual (2, results, "# Multiple result sets shud be returned");
486 Assert.AreEqual (2, results, "#12 Multiple result sets shud be returned");
491 public void PrepareTest_CheckValidStatement ()
493 cmd = new SqlCommand ();
494 conn = new SqlConnection (connectionString);
497 cmd.CommandText = "Select id from numeric_family where id=@ID" ;
498 cmd.Connection = conn ;
500 // Test if Parameters are correctly populated
501 cmd.Parameters.Clear ();
502 cmd.Parameters.Add ("@ID", SqlDbType.TinyInt);
503 cmd.Parameters["@ID"].Value = 2 ;
505 Assert.AreEqual (2, cmd.ExecuteScalar (), "#3 Prepared Stmt not working");
507 cmd.Parameters[0].Value = 3;
508 Assert.AreEqual (3, cmd.ExecuteScalar (), "#4 Prep Stmt not working");
513 public void PrepareTest ()
515 cmd = new SqlCommand ();
516 conn = new SqlConnection (connectionString);
519 cmd.CommandText = "Select id from numeric_family where id=@ID" ;
520 cmd.Connection = conn ;
522 // Test InvalidOperation Exception is thrown if Parameter Type
523 // is not explicitly set
524 cmd.Parameters.Add ("@ID", 2);
527 Assert.Fail ("#1 Parameter Type shud be explicitly Set");
528 }catch (AssertionException e) {
530 }catch (Exception e) {
531 Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
532 "#2 Incorrect Exception : " + e.StackTrace);
535 // Test Exception is thrown for variable size data if precision/scale
537 cmd.CommandText = "select type_varchar from string_family where type_varchar=@p1";
538 cmd.Parameters.Clear ();
539 cmd.Parameters.Add ("@p1", SqlDbType.VarChar);
540 cmd.Parameters["@p1"].Value = "afasasadadada";
543 Assert.Fail ("#5 Exception shud be thrown");
544 }catch (AssertionException e) {
546 }catch (Exception e) {
547 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
548 "#6 Incorrect Exception " + e.StackTrace);
551 // Test Exception is not thrown for Stored Procs
553 cmd.CommandType = CommandType.StoredProcedure;
554 cmd.CommandText = "ABFSDSFSF" ;
556 }catch (Exception e) {
557 Assert.Fail ("#7 Exception shud not be thrown for Stored Procs");
559 cmd.CommandType = CommandType.Text;
562 //Test InvalidOperation Exception is thrown if connection is not set
563 cmd.Connection = null;
567 Assert.Fail ("#8 NullReferenceException should be thrown");
569 Assert.Fail ("#8 InvalidOperation Exception should be thrown");
572 catch (AssertionException e) {
574 }catch (Exception e) {
576 Assert.AreEqual (typeof (NullReferenceException), e.GetType (),
577 "#9 Incorrect Exception : " + e.StackTrace);
579 Assert.AreEqual (typeof (InvalidOperationException), e.GetType (),
580 "#9 Incorrect Exception : " + e.StackTrace);
584 //Test InvalidOperation Exception is thrown if connection is closed
585 cmd.Connection = conn ;
588 Assert.Fail ("#4 InvalidOperation Exception shud be thrown");
589 }catch (AssertionException e) {
591 }catch (Exception e) {
592 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
593 "Incorrect Exception : " + e.StackTrace);
598 public void ResetTimeOut ()
600 SqlCommand cmd = new SqlCommand ();
601 cmd.CommandTimeout = 50 ;
602 Assert.AreEqual ( cmd.CommandTimeout, 50,
603 "#1 CommandTimeout should be modfiable");
604 cmd.ResetCommandTimeout ();
605 Assert.AreEqual (cmd.CommandTimeout, 30,
606 "#2 Reset Should set the Timeout to default value");
610 [ExpectedException (typeof(ArgumentException))]
611 public void CommandTimeout ()
613 cmd = new SqlCommand ();
614 cmd.CommandTimeout = 10;
615 Assert.AreEqual (10, cmd.CommandTimeout, "#1");
616 cmd.CommandTimeout = -1;
621 [ExpectedException (typeof(ArgumentOutOfRangeException))]
623 [ExpectedException (typeof(ArgumentException))]
625 public void CommandTypeTest ()
627 cmd = new SqlCommand ();
628 Assert.AreEqual (CommandType.Text ,cmd.CommandType,
629 "Default CommandType is text");
630 cmd.CommandType = (CommandType)(-1);
634 [Ignore ("msdotnet contradicts documented behavior")]
635 [ExpectedException (typeof(InvalidOperationException))]
636 public void ConnectionTest ()
638 SqlTransaction trans = null;
640 conn = new SqlConnection (connectionString);
642 trans = conn.BeginTransaction ();
643 cmd = new SqlCommand ("", conn,trans);
644 cmd.CommandText = "Select id from numeric_family where id=1";
645 cmd.Connection = new SqlConnection ();
653 public void TransactionTest ()
655 conn = new SqlConnection (connectionString);
656 cmd = new SqlCommand ("", conn);
657 Assert.IsNull (cmd.Transaction, "#1 Default value is null");
659 SqlConnection conn1 = new SqlConnection (connectionString);
661 SqlTransaction trans1 = conn1.BeginTransaction ();
662 cmd.Transaction = trans1 ;
664 cmd.ExecuteNonQuery ();
665 Assert.Fail ("#2 Connection cannot be different");
666 }catch (Exception e) {
667 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
668 "#3 Incorrect Exception : " + e);
675 // Need to add more tests
678 [ExpectedException (typeof(ArgumentOutOfRangeException))]
680 [ExpectedException (typeof(ArgumentException))]
682 public void UpdatedRowSourceTest ()
684 cmd = new SqlCommand ();
685 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource,
686 "#1 Default value is both");
687 cmd.UpdatedRowSource = UpdateRowSource.None;
688 Assert.AreEqual (UpdateRowSource.None, cmd.UpdatedRowSource,
691 cmd.UpdatedRowSource = (UpdateRowSource) (-1);
695 public void ExecuteNonQueryTempProcedureTest () {
696 conn = (SqlConnection) ConnectionManager.Singleton.Connection;
698 ConnectionManager.Singleton.OpenConnection ();
699 // create temp sp here, should normally be created in Setup of test
700 // case, but cannot be done right now because of ug #68978
701 DBHelper.ExecuteNonQuery (conn, CREATE_TMP_SP_TEMP_INSERT_PERSON);
702 SqlCommand cmd = new SqlCommand();
703 cmd.Connection = conn;
704 cmd.CommandText = "#sp_temp_insert_employee";
705 cmd.CommandType = CommandType.StoredProcedure;
706 Object TestPar = "test";
707 cmd.Parameters.Add("@fname", SqlDbType.VarChar);
708 cmd.Parameters ["@fname"].Value = TestPar;
709 Assert.AreEqual(1,cmd.ExecuteNonQuery());
711 DBHelper.ExecuteNonQuery (conn, DROP_TMP_SP_TEMP_INSERT_PERSON);
712 DBHelper.ExecuteSimpleSP (conn, "sp_clean_person_table");
713 ConnectionManager.Singleton.CloseConnection ();
717 // Test for bug #76778
718 // Test for a case, when query size is greater than the block size
720 public void LongQueryTest ()
722 SqlConnection conn = new SqlConnection (
723 connectionString + ";Pooling=false");
726 SqlCommand cmd = conn.CreateCommand ();
727 String value = new String ('a', 10000);
728 cmd.CommandText = String.Format ("Select '{0}'", value);
729 cmd.ExecuteNonQuery ();
733 // Test for bug #76778
734 // To make sure RPC (when implemented) works ok..
736 public void LongStoredProcTest()
738 SqlConnection conn = new SqlConnection (
739 connectionString + ";Pooling=false");
742 int size = conn.PacketSize ;
743 SqlCommand cmd = conn.CreateCommand ();
744 // create a temp stored proc ..
745 cmd.CommandText = "Create Procedure #sp_tmp_long_params ";
746 cmd.CommandText += "@p1 nvarchar (4000), ";
747 cmd.CommandText += "@p2 nvarchar (4000), ";
748 cmd.CommandText += "@p3 nvarchar (4000), ";
749 cmd.CommandText += "@p4 nvarchar (4000) out ";
750 cmd.CommandText += "As ";
751 cmd.CommandText += "Begin ";
752 cmd.CommandText += "Set @p4 = N'Hello' ";
753 cmd.CommandText += "Return 2 ";
754 cmd.CommandText += "End";
755 cmd.ExecuteNonQuery ();
758 cmd.CommandType = CommandType.StoredProcedure;
759 cmd.CommandText = "#sp_tmp_long_params";
761 String value = new String ('a', 4000);
762 SqlParameter p1 = new SqlParameter ("@p1",
763 SqlDbType.NVarChar,4000);
766 SqlParameter p2 = new SqlParameter ("@p2",
767 SqlDbType.NVarChar,4000);
770 SqlParameter p3 = new SqlParameter ("@p3",
771 SqlDbType.NVarChar,4000);
774 SqlParameter p4 = new SqlParameter ("@p4",
775 SqlDbType.NVarChar,4000);
776 p4.Direction = ParameterDirection.Output;
778 // for now, name shud be @RETURN_VALUE
779 // can be changed once RPC is implemented
780 SqlParameter p5 = new SqlParameter ("@RETURN_VALUE", SqlDbType.Int);
781 p5.Direction = ParameterDirection.ReturnValue ;
783 cmd.Parameters.Add (p1);
784 cmd.Parameters.Add (p2);
785 cmd.Parameters.Add (p3);
786 cmd.Parameters.Add (p4);
787 cmd.Parameters.Add (p5);
789 cmd.ExecuteNonQuery ();
790 Assert.AreEqual ("Hello", p4.Value, "#1");
791 Assert.AreEqual (2, p5.Value, "#2");
795 // Test for bug #76880
797 public void DateTimeParameterTest ()
799 SqlConnection conn = new SqlConnection (connectionString);
802 SqlCommand cmd = conn.CreateCommand ();
803 cmd.CommandText = "select * from datetime_family where type_datetime=@p1";
804 cmd.Parameters.Add ("@p1", SqlDbType.DateTime).Value= "10-10-2005";
805 // shudnt cause and exception
806 SqlDataReader rdr = cmd.ExecuteReader ();
812 * Verifies whether an enum value is converted to a numeric value when
813 * used as value for a numeric parameter (bug #66630)
816 public void EnumParameterTest() {
817 conn = (SqlConnection) ConnectionManager.Singleton.Connection;
819 ConnectionManager.Singleton.OpenConnection ();
820 // create temp sp here, should normally be created in Setup of test
821 // case, but cannot be done right now because of ug #68978
822 DBHelper.ExecuteNonQuery (conn, "CREATE PROCEDURE #Bug66630 ("
823 + "@Status smallint = 7"
825 + "AS" + Environment.NewLine
826 + "BEGIN" + Environment.NewLine
827 + "SELECT CAST(5 AS int), @Status" + Environment.NewLine
830 SqlCommand cmd = new SqlCommand("#Bug66630", conn);
831 cmd.CommandType = CommandType.StoredProcedure;
832 cmd.Parameters.Add("@Status", SqlDbType.Int).Value = Status.Error;
834 using (SqlDataReader dr = cmd.ExecuteReader()) {
835 // one record should be returned
836 Assert.IsTrue(dr.Read(), "EnumParameterTest#1");
837 // we should get two field in the result
838 Assert.AreEqual(2, dr.FieldCount, "EnumParameterTest#2");
840 Assert.AreEqual("int", dr.GetDataTypeName(0), "EnumParameterTest#3");
841 Assert.AreEqual(5, dr.GetInt32(0), "EnumParameterTest#4");
843 Assert.AreEqual("smallint", dr.GetDataTypeName(1), "EnumParameterTest#5");
844 Assert.AreEqual((short) Status.Error, dr.GetInt16(1), "EnumParameterTest#6");
845 // only one record should be returned
846 Assert.IsFalse(dr.Read(), "EnumParameterTest#7");
849 DBHelper.ExecuteNonQuery (conn, "if exists (select name from sysobjects " +
850 " where name like '#temp_Bug66630' and type like 'P') " +
851 " drop procedure #temp_Bug66630; ");
852 ConnectionManager.Singleton.CloseConnection ();
857 * The below test does not need a connection but since the setup opens
858 * the connection i will need to close it
861 public void CloneTest() {
862 ConnectionManager.Singleton.OpenConnection ();
863 SqlCommand cmd = new SqlCommand();
864 cmd.Connection = null;
865 cmd.CommandText = "sp_insert";
866 cmd.CommandType = CommandType.StoredProcedure;
867 Object TestPar = DBNull.Value;
868 cmd.Parameters.Add("@TestPar1", SqlDbType.Int);
869 cmd.Parameters["@TestPar1"].Value = TestPar;
870 cmd.Parameters.Add("@BirthDate", DateTime.Now);
871 cmd.DesignTimeVisible = true;
872 cmd.CommandTimeout = 100;
873 Object clone1 = ((ICloneable)(cmd)).Clone();
874 SqlCommand cmd1 = (SqlCommand) clone1;
875 Assert.AreEqual(2, cmd1.Parameters.Count);
876 Assert.AreEqual(100, cmd1.CommandTimeout);
877 cmd1.Parameters.Add("@test", DateTime.Now);
878 // to check that it is deep copy and not a shallow copy of the
879 // parameter collection
880 Assert.AreEqual(3, cmd1.Parameters.Count);
881 Assert.AreEqual(2, cmd.Parameters.Count);
884 private enum Status {
889 private readonly string CREATE_TMP_SP_TEMP_INSERT_PERSON = ("create procedure #sp_temp_insert_employee ( " + Environment.NewLine +
890 "@fname varchar (20)) " + Environment.NewLine +
891 "as " + Environment.NewLine +
892 "begin" + Environment.NewLine +
893 "declare @id int;" + Environment.NewLine +
894 "select @id = max (id) from employee;" + Environment.NewLine +
895 "set @id = @id + 6000 + 1;" + Environment.NewLine +
896 "insert into employee (id, fname, dob, doj) values (@id, @fname, '1980-02-11', getdate ());" + Environment.NewLine +
897 "return @id;" + Environment.NewLine +
900 private readonly string DROP_TMP_SP_TEMP_INSERT_PERSON = ("if exists (select name from sysobjects where " + Environment.NewLine +
901 "name = '#sp_temp_insert_employee' and type = 'P') " + Environment.NewLine +
902 "drop procedure #sp_temp_insert_employee; ");