INSERT Example


	Command1.CommandText = @"INSERT INTO myTable (
						SomeValueColumn,
						SomeStringColumn
					) Values (
						@SomeValue,
						@SomeString
					)";
	//Strings should be added as parameters to avoid sanatisation risks unless you know they are safe.
	//Other values can be added as parameters too or inline in the INSERT text.
	Command1.Parameters.AddWithValue("@SomeValue", 1234);
	Command1.Parameters.AddWithValue("@SomeString", "Hello");

	Command1.ExecuteNonQuery();

INSERT and get Auto Increment value


	int NewRowIdValue;
	Command1.CommandText = @"INSERT INTO myTable (
						SomeValueColumn,
						SomeStringColumn
					) Values (
						@SomeValue,
						@SomeString
					); SELECT last_insert_rowid() AS rowid FROM myTable LIMIT 1";
	Command1.Parameters.AddWithValue("@SomeValue", 1234);
	Command1.Parameters.AddWithValue("@SomeString", "Hello");

	//Command1.ExecuteNonQuery();
	System.Data.SQLite.SQLiteDataReader Reader1 = Command1.ExecuteReader();		//Get the auto index value
	{
		if (Reader1.Read())
			NewRowIdValue = Convert.ToInt32(Reader1["rowid"]);
	}
	Reader1.Close();

Insert If Not Exists


	Command1->CommandText = "INSERT INTO tblMyTable (	\
								FieldName,					\
								FieldIsActive				\
							) SELECT 						\
								@FieldName,					\
								@FieldIsActive				\
							WHERE NOT EXISTS(SELECT 1 FROM tblMyTable WHERE FieldName = 'Part')";

Inserting Null


	Command1.Parameters.AddWithValue("@LanguageNameImage", DBNull.Value);

Get Auto Increment Row Value

After doing the INSERT command do this:

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *