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


C# SqliteConnection.CreateCommand方法代码示例

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


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

示例1: RunSqlite

        public void RunSqlite()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandText = "CREATE TABLE Test(Value);";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "INSERT INTO Test VALUES(1);";
                    cmd.ExecuteNonQuery();
                }
                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandText =
                        "SELECT [type], [name], [tbl_name], [rootpage], [sql], [rowid] FROM [main].[sqlite_master] WHERE [type] LIKE 'table'";
                    using (var dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            System.Console.WriteLine(dr[2].ToString());
                        }
                    }
                }

                var databaseReader = new DatabaseSchemaReader.DatabaseReader(connection);
                var schema = databaseReader.ReadAll();
                var tableList = databaseReader.TableList();
                var tables = databaseReader.AllTables();
                var views = databaseReader.AllViews();
                Assert.NotEmpty(tableList);
            }
        }
开发者ID:shiningrise,项目名称:dbschemareader,代码行数:34,代码来源:TestSqLite.cs

示例2: AddHistoricMessage

        private static void AddHistoricMessage(Message message)
        {
            var sql = string.Format("INSERT INTO {0} (MessageId, MessageType, Topic, Timestamp, HeaderBag, Body) VALUES (@MessageId, @MessageType, @Topic, @Timestamp, @HeaderBag, @Body)", _sqliteTestHelper.TableName_Messages);
            var parameters = new[]
            {
                new SqliteParameter("MessageId", message.Id.ToString()),
                new SqliteParameter("MessageType", message.Header.MessageType.ToString()),
                new SqliteParameter("Topic", message.Header.Topic),
                new SqliteParameter("Timestamp", SqliteType.Text) { Value =message.Header.TimeStamp.ToString("s")},
                new SqliteParameter("HeaderBag",SqliteType.Text) { Value = JsonConvert.SerializeObject(message.Header.Bag)},
                new SqliteParameter("Body", message.Body.Value),
            };

            using (var connection = new SqliteConnection(_sqliteTestHelper.ConnectionString))
            using (var command = connection.CreateCommand())
            {
                connection.Open();

                command.CommandText = sql;
                //command.Parameters.AddRange(parameters); used to work... but can't with current Sqlite lib. Iterator issue
                for (var index = 0; index < parameters.Length; index++)
                {
                    command.Parameters.Add(parameters[index]);
                }
                command.ExecuteNonQuery();
            }
        }
开发者ID:iancooper,项目名称:Paramore,代码行数:27,代码来源:SqlMessageStoreMigrationTests.cs

示例3: ExecuteReader_throws_when_connection_closed

        public void ExecuteReader_throws_when_connection_closed()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                var ex = Assert.Throws<InvalidOperationException>(() => connection.CreateCommand().ExecuteReader());

                Assert.Equal(Strings.FormatCallRequiresOpenConnection("ExecuteReader"), ex.Message);
            }
        }
开发者ID:antiufo,项目名称:Microsoft.Data.Sqlite,代码行数:9,代码来源:SqliteCommandTest.cs

示例4: CreateAndOpenConnection

        private void CreateAndOpenConnection(bool sharedCache = false)
        {
            _connection = new SqliteConnection(CreateConnectionString(_name, sharedCache));

            _connection.Open();

            var command = _connection.CreateCommand();
            command.CommandText = "PRAGMA foreign_keys=ON;";
            command.ExecuteNonQuery();
        }
开发者ID:adwardliu,项目名称:EntityFramework,代码行数:10,代码来源:SqliteTestStore.cs

示例5: ConstructRelationalModel

        public override IModel ConstructRelationalModel([NotNull] string connectionString)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));

            var modelBuilder = new ModelBuilder(new ConventionSet());

            using (var connection = new SqliteConnection(connectionString))
            {
                connection.Open();
                var tables = new Dictionary<string, string>();
                var indexes = new List<SqliteIndexInfo>();
                var master = connection.CreateCommand();
                master.CommandText = "SELECT type, name, sql, tbl_name FROM sqlite_master";
                using (var reader = master.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var type = reader.GetString(0);
                        var name = reader.GetString(1);
                        var sql = reader.GetValue(2) as string; // can be null
                        var tableName = reader.GetString(3);

                        if (type == "table"
                            && name != "sqlite_sequence"
                            && _tableSelectionSet.Allows(TableSelection.Any, name))
                        {
                            tables.Add(name, sql);
                        }
                        else if (type == "index")
                        {
                            indexes.Add(new SqliteIndexInfo
                            {
                                Name = name,
                                TableName = tableName,
                                Sql = sql
                            });
                        }
                    }
                }

                LoadTablesAndColumns(connection, modelBuilder, tables.Keys);
                LoadIndexes(connection, modelBuilder, indexes);

                foreach (var item in tables)
                {
                    SqliteDmlParser.ParseTableDefinition(modelBuilder, item.Key, item.Value);
                }

                AddAlternateKeys(modelBuilder);
                LoadForeignKeys(connection, modelBuilder, tables.Keys);
            }

            return modelBuilder.Model;
        }
开发者ID:491134648,项目名称:EntityFramework,代码行数:54,代码来源:SqliteMetadataModelProvider.cs

示例6: CreateDatabaseWithTable

        private SqliteConnection CreateDatabaseWithTable(string dataSourceTestDb, string createTableScript)
        {
            _sqlConnection = new SqliteConnection(dataSourceTestDb);

            _sqlConnection.Open();
            using (var command = _sqlConnection.CreateCommand())
            {
                command.CommandText = createTableScript;
                command.ExecuteNonQuery();
            }

            return _sqlConnection;
        }
开发者ID:iancooper,项目名称:Paramore,代码行数:13,代码来源:SqlLiteTestHelper.cs

示例7: CharTest2

		public void CharTest2()
		{
			using (var connection = new SqliteConnection(@"Data Source=Database\TestData.sqlite"))
			{
				connection.Open();
				var command = connection.CreateCommand();

				command.CommandText = "SELECT @p";
				var p = command.CreateParameter();
				p.Value = '1';
				p.ParameterName = "@p";

				command.Parameters.Add(p);

				var r = command.ExecuteReader();
				r.Read();

				var value = r.GetValue(0);
				Assert.AreNotEqual((long)49, value, $"{value.GetType().FullName}");
			}
		}
开发者ID:ronnyek,项目名称:linq2db,代码行数:21,代码来源:SQLiteTests.cs

示例8: UInt32Test

		public void UInt32Test()
		{
			using (var connection = new SqliteConnection(@"Data Source=Database\TestData.sqlite"))
			{
				connection.Open();
				var command = connection.CreateCommand();

				command.CommandText = "SELECT @p";
				var p = command.CreateParameter();
				p.Value = UInt32.MaxValue;
				p.DbType = System.Data.DbType.UInt32;
				p.ParameterName = "@p";

				command.Parameters.Add(p);

				var r = command.ExecuteReader();
				r.Read();

				var value = r.GetValue(0);
				Assert.AreEqual(UInt32.MaxValue, value, $"{value.GetType().FullName} {value}");
			}
		}
开发者ID:ronnyek,项目名称:linq2db,代码行数:22,代码来源:SQLiteTests.cs

示例9: LoadIndexes

        private void LoadIndexes(SqliteConnection connection, ModelBuilder modelBuilder, ICollection<SqliteIndexInfo> indexes)
        {
            foreach (var index in indexes)
            {
                var indexInfo = connection.CreateCommand();
                indexInfo.CommandText = $"PRAGMA index_info(\"{index.Name.Replace("\"", "\"\"")}\");";

                var indexProps = new List<string>();
                using (var reader = indexInfo.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var name = reader.GetValue((int)IndexInfo.Name) as string;
                        if (!string.IsNullOrEmpty(name))
                        {
                            indexProps.Add(name);
                        }
                    }
                }

                if (indexProps.Count > 0)
                {
                    var indexBuilder = modelBuilder.Entity(index.TableName)
                        .Index(indexProps.ToArray())
                        .SqliteIndexName(index.Name);

                    if (!string.IsNullOrEmpty(index.Sql))
                    {
                        var uniqueKeyword = index.Sql.IndexOf("UNIQUE", StringComparison.OrdinalIgnoreCase);
                        var indexKeyword = index.Sql.IndexOf("INDEX", StringComparison.OrdinalIgnoreCase);

                        indexBuilder.Unique(uniqueKeyword > 0 && uniqueKeyword < indexKeyword);
                    }
                }
            }
        }
开发者ID:rbenhassine2,项目名称:EntityFramework,代码行数:36,代码来源:SqliteMetadataModelProvider.cs

示例10: LoadForeignKeys

        private void LoadForeignKeys(SqliteConnection connection, ModelBuilder modelBuilder, ICollection<string> tables)
        {
            foreach (var tableName in tables)
            {
                var fkList = connection.CreateCommand();
                fkList.CommandText = $"PRAGMA foreign_key_list(\"{tableName.Replace("\"", "\"\"")}\");";

                var foreignKeys = new Dictionary<int, ForeignKeyInfo>();
                using (var reader = fkList.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var id = reader.GetInt32((int)ForeignKeyList.Id);
                        var refTable = reader.GetString((int)ForeignKeyList.Table);
                        ForeignKeyInfo foreignKey;
                        if (!foreignKeys.TryGetValue(id, out foreignKey))
                        {
                            foreignKeys.Add(id, (foreignKey = new ForeignKeyInfo { Table = tableName, ReferencedTable = refTable }));
                        }
                        foreignKey.From.Add(reader.GetString((int)ForeignKeyList.From));
                        foreignKey.To.Add(reader.GetString((int)ForeignKeyList.To));
                    }
                }

                var dependentEntityType = modelBuilder.Entity(tableName).Metadata;

                foreach (var fkInfo in foreignKeys.Values)
                {
                    try
                    {
                        var principalEntityType = modelBuilder.Model.EntityTypes.First(e => e.Name.Equals(fkInfo.ReferencedTable, StringComparison.OrdinalIgnoreCase));

                        var principalProps = fkInfo.To
                            .Select(to => principalEntityType
                                .Properties
                                .First(p => p.Sqlite().ColumnName.Equals(to, StringComparison.OrdinalIgnoreCase))
                            )
                            .ToList()
                            .AsReadOnly();

                        var principalKey = principalEntityType.FindKey(principalProps);
                        if (principalKey == null)
                        {
                            var index = principalEntityType.FindIndex(principalProps);
                            if (index != null
                                && index.IsUnique == true)
                            {
                                principalKey = principalEntityType.AddKey(principalProps);
                            }
                            else
                            {
                                LogFailedForeignKey(fkInfo);
                                continue;
                            }
                        }

                        var depProps = fkInfo.From
                            .Select(
                                @from => dependentEntityType
                                    .Properties.
                                    First(p => p.Sqlite().ColumnName.Equals(@from, StringComparison.OrdinalIgnoreCase))
                            )
                            .ToList()
                            .AsReadOnly();

                        var foreignKey = dependentEntityType.GetOrAddForeignKey(depProps, principalKey, principalEntityType);

                        if (dependentEntityType.FindIndex(depProps)?.IsUnique == true
                            || dependentEntityType.GetKeys().Any(k => k.Properties.All(p => depProps.Contains(p))))
                        {
                            foreignKey.IsUnique = true;
                        }
                    }
                    catch (InvalidOperationException)
                    {
                        LogFailedForeignKey(fkInfo);
                    }
                }
            }
        }
开发者ID:rbenhassine2,项目名称:EntityFramework,代码行数:80,代码来源:SqliteMetadataModelProvider.cs

示例11: Bind_binds_string_values_without_embedded_nulls

        public void Bind_binds_string_values_without_embedded_nulls()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                var command = connection.CreateCommand();
                command.CommandText = "SELECT @Text || 'ing';";
                command.Parameters.AddWithValue("@Text", "test");
                connection.Open();

                var result = command.ExecuteScalar();

                Assert.Equal("testing", result);
            }
        }
开发者ID:RonFrick,项目名称:Microsoft.Data.Sqlite,代码行数:14,代码来源:SqliteParameterTest.cs

示例12: Bind_throws_when_unknown

        public void Bind_throws_when_unknown()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                var command = connection.CreateCommand();
                command.CommandText = "SELECT @Parameter;";
                command.Parameters.AddWithValue("@Parameter", new object());
                connection.Open();

                var ex = Assert.Throws<InvalidOperationException>(() => command.ExecuteScalar());

                Assert.Equal(Strings.FormatUnknownDataType(typeof(object)), ex.Message);
            }
        }
开发者ID:RonFrick,项目名称:Microsoft.Data.Sqlite,代码行数:14,代码来源:SqliteParameterTest.cs

示例13: ExecuteNonQuery_throws_when_no_command_text

        public void ExecuteNonQuery_throws_when_no_command_text()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                var ex = Assert.Throws<InvalidOperationException>(() => connection.CreateCommand().ExecuteNonQuery());

                Assert.Equal(Strings.FormatCallRequiresSetCommandText("ExecuteNonQuery"), ex.Message);
            }
        }
开发者ID:antiufo,项目名称:Microsoft.Data.Sqlite,代码行数:11,代码来源:SqliteCommandTest.cs

示例14: ExecuteNonQuery_works

        public void ExecuteNonQuery_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                var command = connection.CreateCommand();
                command.CommandText = "SELECT 1;";
                connection.Open();

                Assert.Equal(-1, command.ExecuteNonQuery());
            }
        }
开发者ID:antiufo,项目名称:Microsoft.Data.Sqlite,代码行数:11,代码来源:SqliteCommandTest.cs

示例15: DecimalTest

	    public void DecimalTest()
	    {
			using (var connection = new SqliteConnection(@"Data Source=Database\TestData.sqlite"))
			{
				connection.Open();
				var command = connection.CreateCommand();

				command.CommandText = "SELECT MoneyValue FROM LinqDataTypes";

				var r = command.ExecuteReader();
				while (r.Read())
				{
					var value = r.GetValue(0);
					Console.WriteLine($"{value.GetType().FullName} {value}, {r.GetFieldType(0)}");
					Assert.AreNotEqual(typeof(long), r.GetFieldType(0), $"{value.GetType().FullName} {value}");
				}
			}
		}
开发者ID:ronnyek,项目名称:linq2db,代码行数:18,代码来源:SQLiteTests.cs


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