当前位置: 首页>>代码示例>>C#>>正文


C# SqlConnection.ExecQuery方法代码示例

本文整理汇总了C#中System.Data.SqlClient.SqlConnection.ExecQuery方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.ExecQuery方法的具体用法?C# SqlConnection.ExecQuery怎么用?C# SqlConnection.ExecQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Data.SqlClient.SqlConnection的用法示例。


在下文中一共展示了SqlConnection.ExecQuery方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Connect

        public ReturnValue Connect()
        {
            if (ConnectionString == null)
                return ReturnValue.FailResult();


            return ReturnValue.Wrap(() =>
            {
                using (var conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    _tableInfo =
                        conn.ExecQuery(
                            "SELECT t.NAME AS TableName, s.Name AS SchemaName,max( p.rows) AS RowCounts, SUM(a.total_pages) * 8 AS TotalSpaceKB " +
                            "FROM  sys.tables t  " +
                            "INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id " +
                            "INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id " +
                            "INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id " +
                            "LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id " +
                            "WHERE (t.NAME NOT LIKE 'dt%') AND (t.is_ms_shipped = 0) AND (i.OBJECT_ID > 255 ) " +
                            "GROUP BY t.Name, s.Name " +
                            "ORDER BY t.Name");
                    DisplayTables();
                }
            }, "Connect() failed.");
        }
开发者ID:ninerats,项目名称:DataTools,代码行数:26,代码来源:TableListView.cs

示例2: AssertConstraintsAreEnabledAndTrusted

 public static void AssertConstraintsAreEnabledAndTrusted()
 {
     using (var conn = new SqlConnection(DataToolsConnectionString))
     {
         conn.Open();
         var dt = conn.ExecQuery("select name, is_disabled, is_not_trusted from sys.foreign_keys");
         var badones =
             dt.Rows.Cast<DataRow>().Where(r => r.Field<bool>("is_disabled") || r.Field<bool>("is_not_trusted"));
         CollectionAssert.IsEmpty(badones);
     }
 }
开发者ID:ninerats,项目名称:DataTools,代码行数:11,代码来源:DataSerTestHelper.cs

示例3: SpecialSchemaTest

        public void SpecialSchemaTest()
        {
            using (var conn = new SqlConnection(DataSerTestHelper.DataToolsConnectionString))
            {
                const string tableName = "BulkLoadTestTableSpecial";
                conn.ExecSql(string.Format("DELETE FROM {0}", tableName));
                var datSer = new DataTableSerializer(DataSerTestHelper.DataToolsConnectionString);
                var result = datSer.ImportTable(string.Format(@"resources\dbo.{0}.xml", tableName));
                DataSerTestHelper.AssertResult(result);
                var tableData = conn.ExecQuery(string.Format("SELECT * FROM {0}", tableName));
                Assert.AreEqual(3, tableData.Rows.Count);

            }
        }
开发者ID:ninerats,项目名称:DataTools,代码行数:14,代码来源:ImportingTablesTest.cs

示例4: DifferentSchemaTest

        public void DifferentSchemaTest()
        {
            using (var conn = new SqlConnection(DataSerTestHelper.DataToolsConnectionString))
            {
                conn.ExecSql(File.ReadAllText(@"SQL\reset_BulkLoadTestTable1.sql"));
                var datSer = new DataTableSerializer(DataSerTestHelper.DataToolsConnectionString);
                var result = datSer.ImportTable(@"resources\dbo.BulkLoadTestTable1.xml");
                DataSerTestHelper.AssertResult(result);
                var tableData = conn.ExecQuery("SELECT * FROM BulkLoadTestTable1");
                Assert.AreEqual(3, tableData.Rows.Count);
                var firstRowData = tableData.Rows[0].ItemArray;
                CollectionAssert.AreEqual(new object[] {1, "Alpha", 11, DBNull.Value}, firstRowData);

            }
        }
开发者ID:ninerats,项目名称:DataTools,代码行数:15,代码来源:ImportingTablesTest.cs

示例5: Search

        public List<SearchResult> Search(string textToSearchFor)
        {
            var searchResults = new List<SearchResult>();
            using (var conn = new SqlConnection(_databaseDataTableSet.ConnStr))
            {
                conn.Open();
                foreach (var table in _databaseDataTableSet.TableList)
                {
                    var fields = GetFieldsOfType(conn, table, SqlDbTypeGroup.String);
                    DataColumn[] keys;
                    using (var adapter = new SqlDataAdapter("select * from " + table, conn))
                    using (var dt = new DataTable(table))
                    {
                        adapter.FillSchema(dt, SchemaType.Mapped);
                        keys = dt.PrimaryKey;
                    }

                    foreach (DataRow fieldInfoRow in fields.Rows)
                    {
                        var fieldName = fieldInfoRow.Field<string>("COLUMN_NAME");
                        string fieldSearchSql = string.Format("select * from {0} where ([{1}] like '%{2}%')",
                            table, fieldName, textToSearchFor);
                        Log.Debug(string.Format("fieldSearchSql: {0}", fieldSearchSql));
                        var dataSearchResults = conn.ExecQuery(fieldSearchSql);
                        
                            searchResults.AddRange(
                                dataSearchResults.Rows.Cast<DataRow>().Select(dataRow => new SearchResult()
                                {
                                    TableName = table,
                                    FieldName = fieldName,
                                    FieldType = fieldInfoRow.Field<string>("DATA_TYPE"),
                                    Sql = string.Format("{0} AND {1}", fieldSearchSql, MakeKeyFilter(keys,dataRow.KeyValues())),
                                    Value = dataRow[fieldName],
                                    KeyFields = keys,
                                    KeyValue = dataRow.KeyValues()
                                }));
                       
                    }

                }

            }
            return searchResults;
        }
开发者ID:ninerats,项目名称:DataTools,代码行数:44,代码来源:DataValueSearcher.cs

示例6: GetFieldsOfType

        private DataTable GetFieldsOfType(SqlConnection conn, string table, SqlDbTypeGroup dbTypeGroup)
        {
            var matchingTypes = SqlTypeUtil.SqlDbTypeGroupMap.Where(kv => kv.Value == dbTypeGroup).Select(kv => kv.Key);
            var inClause = string.Join(",", matchingTypes.Select(t => string.Format("'{0}'", t)));

            var sqlDbTypeSql =
                string.Format(
                    "select * from INFORMATION_SCHEMA.COLUMNS where (TABLE_SCHEMA + '.' + TABLE_NAME = '{0}') " +
                    "AND (DATA_TYPE IN ({1}))", table, inClause);
            Log.Debug(sqlDbTypeSql);
            var result = conn.ExecQuery(sqlDbTypeSql);
            return result;

        }
开发者ID:ninerats,项目名称:DataTools,代码行数:14,代码来源:DataValueSearcher.cs


注:本文中的System.Data.SqlClient.SqlConnection.ExecQuery方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。