Skip to content

Commit

Permalink
Test | Code syntax improvements for TVP test (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
Javad authored Nov 8, 2021
1 parent 78e8ebc commit a5b357f
Show file tree
Hide file tree
Showing 11 changed files with 1,846 additions and 2,107 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@ private static void InvalidValueInOutParam(string connectionString)
connection.Open();

// Command simply set the outparam
using (var command = new SqlCommand("SET @decimal = 1.23", connection))
using var command = new SqlCommand("SET @decimal = 1.23", connection);

// Create valid param
var decimalParam = new SqlParameter("decimal", new decimal(2.34)) { SqlDbType = SqlDbType.Decimal, Direction = ParameterDirection.Output, Scale = 2, Precision = 5 };
command.Parameters.Add(decimalParam);
// Set value of param to invalid value
decimalParam.Value = "Not a decimal";

// Execute
command.ExecuteNonQuery();
// Validate
if (((decimal)decimalParam.Value) != new decimal(1.23))
{

// Create valid param
var decimalParam = new SqlParameter("decimal", new decimal(2.34)) { SqlDbType = SqlDbType.Decimal, Direction = ParameterDirection.Output, Scale = 2, Precision = 5 };
command.Parameters.Add(decimalParam);
// Set value of param to invalid value
decimalParam.Value = "Not a decimal";

// Execute
command.ExecuteNonQuery();
// Validate
if (((decimal)decimalParam.Value) != new decimal(1.23))
{
Console.WriteLine("FAIL: Value is incorrect: {0}", decimalParam.Value);
}
Console.WriteLine("FAIL: Value is incorrect: {0}", decimalParam.Value);
}
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ public void SqlAdapterTest()
finally
{
var dropTableQuery = "DROP TABLE IF EXISTS " + tableName;
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (var cmd = new SqlCommand(dropTableQuery, connection))
{
connection.Open();
cmd.ExecuteNonQuery();
}
using var connection = new SqlConnection(DataTestUtility.TCPConnectionString);
using var cmd = new SqlCommand(dropTableQuery, connection);
connection.Open();
cmd.ExecuteNonQuery();
}
}

Expand All @@ -65,29 +63,27 @@ public EventInfo()

private static void ExecuteNonQueries(string tableName)
{
List<EventInfo> entities = new List<EventInfo>
List<EventInfo> entities = new()
{
new EventInfo {Level = "L1", Message = "Message 1"},
new EventInfo {Level = "L2", Message = "Message 2"},
new EventInfo {Level = "L3", Message = "Message 3"},
new EventInfo {Level = "L4", Message = "Message 4"},
new EventInfo { Level = "L1", Message = "Message 1" },
new EventInfo { Level = "L2", Message = "Message 2" },
new EventInfo { Level = "L3", Message = "Message 3" },
new EventInfo { Level = "L4", Message = "Message 4" },
};

var sql = "INSERT INTO " + tableName + "(Level, Message, EventTime) VALUES(@Level, @Message, @EventTime)";
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (var adapter = new SqlDataAdapter())
using (var cmd = new SqlCommand(sql, connection))
{
cmd.Parameters.Add(new SqlParameter("@Level", System.Data.SqlDbType.NVarChar, 50, "Level"));
cmd.Parameters.Add(new SqlParameter("@Message", SqlDbType.NVarChar, 500, "Message"));
cmd.Parameters.Add(new SqlParameter("@EventTime", SqlDbType.DateTime, 0, "EventTime"));
cmd.UpdatedRowSource = UpdateRowSource.None;
using var connection = new SqlConnection(DataTestUtility.TCPConnectionString);
using var adapter = new SqlDataAdapter();
using var cmd = new SqlCommand(sql, connection);
cmd.Parameters.Add(new SqlParameter("@Level", System.Data.SqlDbType.NVarChar, 50, "Level"));
cmd.Parameters.Add(new SqlParameter("@Message", SqlDbType.NVarChar, 500, "Message"));
cmd.Parameters.Add(new SqlParameter("@EventTime", SqlDbType.DateTime, 0, "EventTime"));
cmd.UpdatedRowSource = UpdateRowSource.None;

adapter.InsertCommand = cmd;
adapter.UpdateBatchSize = 2;
adapter.InsertCommand = cmd;
adapter.UpdateBatchSize = 2;

adapter.Update(ConvertToTable(entities));
}
adapter.Update(ConvertToTable(entities));
}
private static DataTable ConvertToTable(List<EventInfo> entities)
{
Expand All @@ -97,9 +93,9 @@ private static DataTable ConvertToTable(List<EventInfo> entities)
table.Columns.Add("Message", typeof(string));
table.Columns.Add("EventTime", typeof(DateTime));

foreach (var entity in entities)
foreach (EventInfo entity in entities)
{
var row = table.NewRow();
DataRow row = table.NewRow();
row["Level"] = entity.Level;
row["Message"] = entity.Message;
row["EventTime"] = entity.EventTime;
Expand Down
Loading

0 comments on commit a5b357f

Please sign in to comment.