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


C# SqliteCommand.LastInsertRowID方法代码示例

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


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

示例1: Test

        public void Test(bool v3, string encoding)
        {
            if (!v3)
                System.Diagnostics.Debug.WriteLine("Testing Version 2" + (encoding != null ? " with " + encoding + " encoding" : ""));
            else
                System.Diagnostics.Debug.WriteLine("Testing Version 3");

            SqliteConnection dbcon = new SqliteConnection();

            // the connection string is a URL that points
            // to a file.  If the file does not exist, a 
            // file is created.

            // "URI=file:some/path"
            var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,
                                    "SqliteTest" + Environment.TickCount + ".db");
            string connectionString = "URI=file://" + path;
            if (v3)
                connectionString += ",Version=3";
            if (encoding != null)
                connectionString += ",encoding=" + encoding;
            dbcon.ConnectionString = connectionString;

            dbcon.Open();

            SqliteCommand dbcmd = new SqliteCommand();
            dbcmd.Connection = dbcon;

            dbcmd.CommandText =
                "CREATE TABLE MONO_TEST ( " +
                "NID INT, " +
                "NDESC TEXT, " +
                "NTIME DATETIME); " +
                "INSERT INTO MONO_TEST  " +
                "(NID, NDESC, NTIME) " +
                "VALUES(1,'One (unicode test: \u05D0)', '2006-01-01')";
            System.Diagnostics.Debug.WriteLine("Create & insert modified rows = 1: " + dbcmd.ExecuteNonQuery());

            dbcmd.CommandText =
                "INSERT INTO MONO_TEST  " +
                "(NID, NDESC, NTIME) " +
                "VALUES(:NID,:NDESC,:NTIME)";
            dbcmd.Parameters.Add(new SqliteParameter("NID", 2));
            dbcmd.Parameters.Add(new SqliteParameter(":NDESC", "Two (unicode test: \u05D1)"));
            dbcmd.Parameters.Add(new SqliteParameter(":NTIME", DateTime.Now));
            System.Diagnostics.Debug.WriteLine("Insert modified rows with parameters = 1, 2: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());

            dbcmd.CommandText =
                "INSERT INTO MONO_TEST  " +
                "(NID, NDESC, NTIME) " +
                "VALUES(3,'Three, quoted parameter test, and next is null; :NTIME', NULL)";
            System.Diagnostics.Debug.WriteLine("Insert with null modified rows and ID = 1, 3: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());

            dbcmd.CommandText =
                "INSERT INTO MONO_TEST  " +
                "(NID, NDESC, NTIME) " +
                "VALUES(4,'Four with ANSI char: ü', NULL)";
            System.Diagnostics.Debug.WriteLine("Insert with ANSI char ü = 1, 4: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());

            dbcmd.CommandText =
                "INSERT INTO MONO_TEST  " +
                "(NID, NDESC, NTIME) " +
                "VALUES(?,?,?)";
            dbcmd.Parameters.Clear();
            IDbDataParameter param1 = dbcmd.CreateParameter();
            param1.DbType = DbType.Int32;
            param1.Value = 5;
            dbcmd.Parameters.Add(param1);
            IDbDataParameter param2 = dbcmd.CreateParameter();
            param2.Value = "Using unnamed parameters";
            dbcmd.Parameters.Add(param2);
            IDbDataParameter param3 = dbcmd.CreateParameter();
            param3.DbType = DbType.DateTime;
            param3.Value = DateTime.Parse("2006-05-11 11:45:00");
            dbcmd.Parameters.Add(param3);
            System.Diagnostics.Debug.WriteLine("Insert with unnamed parameters = 1, 5: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());

            dbcmd.CommandText =
                "SELECT * FROM MONO_TEST";
            using (var reader = dbcmd.ExecuteReader())
            {
                System.Diagnostics.Debug.WriteLine("read and display data...");
                while (reader.Read())
                    for (int i = 0; i < reader.FieldCount; i++)
                        System.Diagnostics.Debug.WriteLine(
                            " Col {0}: {1} (type: {2}, data type: {3})",
                            i,
                            reader[i] == null ? "(null)" : reader[i].ToString(),
                            reader[i] == null ? "(null)" : reader[i].GetType().FullName,
                            reader.GetDataTypeName(i));
            }
            dbcmd.CommandText = "SELECT NDESC FROM MONO_TEST WHERE NID=2";
            System.Diagnostics.Debug.WriteLine("read and display a scalar = 'Two': " + dbcmd.ExecuteScalar());

            dbcmd.CommandText = "SELECT count(*) FROM MONO_TEST";
            System.Diagnostics.Debug.WriteLine("read and display a non-column scalar = 3: " + dbcmd.ExecuteScalar());

            try
            {
                dbcmd.CommandText = "SELECT NDESC INVALID SYNTAX FROM MONO_TEST WHERE NID=2";
//.........这里部分代码省略.........
开发者ID:tohosnet,项目名称:Mono.Data.Sqlite,代码行数:101,代码来源:SqliteTest.cs

示例2: Execute

        internal void Execute (HyenaSqliteConnection hconnection, SqliteConnection connection)
        {
            if (finished) {
                throw new Exception ("Command is already set to finished; result needs to be claimed before command can be rerun");
            }

            execution_exception = null;
            result = null;
            int execution_ms = 0;

            using (SqliteCommand sql_command = new SqliteCommand (CurrentSqlText)) {
                sql_command.Connection = connection;

                hconnection.OnExecuting (sql_command);

                try {
                    ticks = System.Environment.TickCount;

                    switch (CommandType) {
                        case HyenaCommandType.Reader:
                            using (SqliteDataReader reader = sql_command.ExecuteReader ()) {
                                result = new HyenaSqliteArrayDataReader (reader);
                            }
                            break;

                        case HyenaCommandType.Scalar:
                            result = sql_command.ExecuteScalar ();
                            break;

                        case HyenaCommandType.Execute:
                        default:
                            sql_command.ExecuteNonQuery ();
                            result = sql_command.LastInsertRowID ();
                            break;
                    }

                    execution_ms = System.Environment.TickCount - ticks;
                    if (log_all) {
                        Log.DebugFormat ("Executed in {0}ms {1}", execution_ms, sql_command.CommandText);
                    }
                } catch (Exception e) {
                    Log.DebugFormat ("Exception executing command: {0}", sql_command.CommandText);
                    execution_exception = e;
                }
            }

            // capture the text
            string raise_text = null;
            if (raise_command_executed && execution_ms >= raise_command_executed_threshold_ms) {
                raise_text = Text;
            }

            finished_event.Reset ();
            finished = true;

            if (raise_command_executed && execution_ms >= raise_command_executed_threshold_ms) {
                var handler = CommandExecuted;
                if (handler != null) {

                    // Don't raise this on this thread; this thread is dedicated for use by the db connection
                    ThreadAssist.ProxyToMain (delegate {
                        handler (this, new CommandExecutedArgs (raise_text, execution_ms));
                    });
                }
            }
        }
开发者ID:tigger,项目名称:tripod,代码行数:66,代码来源:HyenaSqliteCommand.cs

示例3: Test

		static void Test(bool v3, string encoding) {
			if (!v3)
				Console.WriteLine("Testing Version 2" + (encoding != null ? " with " + encoding + " encoding" : ""));
			else
				Console.WriteLine("Testing Version 3");
				
			System.IO.File.Delete("SqliteTest.db");
		
			SqliteConnection dbcon = new SqliteConnection();
			
			// the connection string is a URL that points
			// to a file.  If the file does not exist, a 
			// file is created.

			// "URI=file:some/path"
			string connectionString =
				"URI=file:SqliteTest.db";
			if (v3)
				connectionString += ",Version=3";
			if (encoding != null)
				connectionString += ",encoding=" + encoding;
			dbcon.ConnectionString = connectionString;
				
			dbcon.Open();

			SqliteCommand dbcmd = new SqliteCommand();
			dbcmd.Connection = dbcon;
			
			dbcmd.CommandText = 
				"CREATE TABLE MONO_TEST ( " +
				"NID INT, " +
				"NDESC TEXT, " +
				"NTIME DATETIME); " +
				"INSERT INTO MONO_TEST  " +
				"(NID, NDESC, NTIME) " +
				"VALUES(1,'One (unicode test: \u05D0)', '2006-01-01')";
			Console.WriteLine("Create & insert modified rows = 1: " + dbcmd.ExecuteNonQuery());

			dbcmd.CommandText =
				"INSERT INTO MONO_TEST  " +
				"(NID, NDESC, NTIME) " +
				"VALUES(:NID,:NDESC,:NTIME)";
			dbcmd.Parameters.Add( new SqliteParameter("NID", 2) );
			dbcmd.Parameters.Add( new SqliteParameter(":NDESC", "Two (unicode test: \u05D1)") );
			dbcmd.Parameters.Add( new SqliteParameter(":NTIME", DateTime.Now) );
			Console.WriteLine("Insert modified rows with parameters = 1, 2: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());

			dbcmd.CommandText =
				"INSERT INTO MONO_TEST  " +
				"(NID, NDESC, NTIME) " +
				"VALUES(3,'Three, quoted parameter test, and next is null; :NTIME', NULL)";
			Console.WriteLine("Insert with null modified rows and ID = 1, 3: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());

			dbcmd.CommandText =
				"INSERT INTO MONO_TEST  " +
				"(NID, NDESC, NTIME) " +
				"VALUES(4,'Four with ANSI char: ü', NULL)";
			Console.WriteLine("Insert with ANSI char ü = 1, 4: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());

			dbcmd.CommandText =
				"INSERT INTO MONO_TEST  " +
				"(NID, NDESC, NTIME) " +
				"VALUES(?,?,?)";
			dbcmd.Parameters.Clear();
			IDbDataParameter param1 = dbcmd.CreateParameter();
			param1.DbType = DbType.DateTime;
			param1.Value = 5;
			dbcmd.Parameters.Add(param1);			
			IDbDataParameter param2 = dbcmd.CreateParameter();
			param2.Value = "Using unnamed parameters";
			dbcmd.Parameters.Add(param2);
			IDbDataParameter param3 = dbcmd.CreateParameter();
			param3.DbType = DbType.DateTime;
			param3.Value = DateTime.Parse("2006-05-11 11:45:00");
			dbcmd.Parameters.Add(param3);
			Console.WriteLine("Insert with unnamed parameters = 1, 5: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());

			dbcmd.CommandText =
				"SELECT * FROM MONO_TEST";
			SqliteDataReader reader;
			reader = dbcmd.ExecuteReader();

			Console.WriteLine("read and display data...");
			while(reader.Read())
				for (int i = 0; i < reader.FieldCount; i++)
					Console.WriteLine(" Col {0}: {1} (type: {2}, data type: {3})",
						i, reader[i] == null ? "(null)" : reader[i].ToString(), reader[i] == null ? "(null)" : reader[i].GetType().FullName, reader.GetDataTypeName(i));

			dbcmd.CommandText = "SELECT NDESC FROM MONO_TEST WHERE NID=2";
			Console.WriteLine("read and display a scalar = 'Two': " + dbcmd.ExecuteScalar());

			dbcmd.CommandText = "SELECT count(*) FROM MONO_TEST";
			Console.WriteLine("read and display a non-column scalar = 3: " + dbcmd.ExecuteScalar());

			Console.WriteLine("read and display data using DataAdapter/DataSet...");
			SqliteDataAdapter adapter = new SqliteDataAdapter("SELECT * FROM MONO_TEST", connectionString);
			DataSet dataset = new DataSet();
			adapter.Fill(dataset);
			foreach(DataTable myTable in dataset.Tables){
				foreach(DataRow myRow in myTable.Rows){
//.........这里部分代码省略.........
开发者ID:REALTOBIZ,项目名称:mono,代码行数:101,代码来源:SqliteTest.cs


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