예제 #1
0
파일: Runner.cs 프로젝트: haled/XSqlClient
        public ISqlResult ExecuteQuery(string connectionString, string sql)
        {
            ISqlResult result = new SqlResult();
              bool gettingColumnNames = true;

              using (SqlConnection conn = new SqlConnection(connectionString))
              {
            conn.Open();
            var cmd = new SqlCommand(sql, conn);

            var reader = cmd.ExecuteReader();
            while(reader.Read())
            {
              var newRow = new Dictionary<string,object>();
              for(int i = 0; i < reader.FieldCount; i++)
              {
            if(gettingColumnNames)
            {
              gettingColumnNames = i < reader.FieldCount;
              result.ColumnNames.Add(reader.GetName(i));
            }
            newRow.Add(reader.GetName(i), reader[i]);
              }
              result.Rows.Add(newRow);
            }
              }

              return result;
        }
예제 #2
0
        public void EmptyStringReturnedWithEmptyObject()
        {
            var _testResult = new SqlResult();

              var output = _testResult.PrintDataToText();

              Assert.Equal("",output);
        }
예제 #3
0
        public void ColumnsWithNoRowsReturnsEmptyString()
        {
            var _testResult = new SqlResult();
              _testResult.ColumnNames.Add("col1");

              var output = _testResult.PrintDataToText();

              Assert.Equal("",output);
        }
예제 #4
0
        public void ValidDataReturnsFormattedOutput()
        {
            var _testResult = new SqlResult();
              _testResult.ColumnNames.Add("col1");
              _testResult.Rows.Add(new Dictionary<string, object> {{"col1","test"}});

              var output = _testResult.PrintDataToText();

              Assert.Equal("col1    \n----    \ntest    \n",output);
        }
예제 #5
0
        public void RowsWithNoColumnNamesThrowsAnException()
        {
            var _testResult = new SqlResult();
              _testResult.Rows.Add(new Dictionary<string, object> {{"test","test"}});

              var result = Record.Exception(() => _testResult.PrintDataToText());

              Assert.NotNull(result);
              Assert.IsType<Exception>(result);
              Assert.Equal("No column names were provided, so a result can't be displayed.", result.Message);
        }