Write a SQLite database using C # Add and delete changes to the statement (and insert byte[] in the summary of problems encountered)

Source: Internet
Author: User
Tags pack sqlite sqlite database string format

Before using a lightweight database like SQLite, only Sqlserver2008 was used to make additions and deletions to the data, and the company was using a large Oracle database that was not actually used. At that time the database is very large, but after the encounter SQLite, found that there is such a mini-database, like a girl's bag carrying a small mirror, where all can be used.

Needless to say, SQLite provides a way to connect, close and other database operations, but the real increase, delete, change, check and other statements with SQL Server, Oracle is the same.

The first is to create a SQLite database file (write a method):

public static void Createdatabasefile ()

{

If (! File.exists (DataBaseName))

{

Sqliteconnection.createfile (DataBaseName);

}

}

Note: The default SQLite database file is created in the current application root directory, if you need to change the file path, remember to add the file path.

For example: sqliteconnection. CreateFile (scenename+"\"+dataSource );

?

Open a database, but also to add the file path, if you do not need to change, it does not matter ....

Datasource= "+Environment.CurrentDirectory+ "\ \" + pack.scenename +< Span style= "Color:gainsboro" > + pack. Battleid + ;

?

Then, add and delete changes to the statement, insert the statement as an example:

1. Query statements

SELECT *from Table_battle;

?

2. UPDATE statements

UPDATE table_battle SET field 1= ' New value 1 ' ;

?

3. Delete statements

DELETE from Table

?

4. Insert statement

string connstr = @ "Datasource=deptdatabase";

using (sqliteconnection conn = new Sqliteconnection (CONNSTR))

//{

Conn. Open ();

using (var transaction = conn. BeginTransaction ())

// {

Try

// {

Sqlitecommand cmd = conn. CreateCommand ();

Cmd. Parameters.Add (cmd. CreateParameter ());

Cmd.commandtext = "INSERT into table name (Field 1, Field 2, Field 3) VALUES (@ Field 1,@ field 2,@ field 3)";

Cmd. Parameters.addwithvalue ("@ Field 1", value 1);

Cmd. Parameters.addwithvalue ("@ Field 2", value 2);

Cmd. Parameters.addwithvalue ("@ Field 3", value 3);

Cmd. ExecuteNonQuery ();

Transaction.commit ();

Conn. Close ();

// }

catch (Exception e)

// {

Console.WriteLine (E.message);

Transaction. Rollback ();

// }

// }

//}

?

It is no problem to insert binary byte data using this method.

When I use the concatenation string format SQL statement has the problem, because uses the concatenation string format, the compiler thought that this sentence is the string (string type), so even if the inside data has the binary format, also is treated as the string processing (except the number format), therefore will have the error.

The following sentence is already modified, so the normal operation. (represented by a placeholder, and then assigned when used)

varsql="INSERT into Table_battle_history (battle_id,time_stamp,object_ Name,sync_type,

Detail_data) VALUES (' "+Accdata.Battleid+"‘,‘"+Accdata.TimeStamp+"‘,‘" + accdata. Objectname + + accdatasynctype + ;

?

Binary data can be stored normally in the database:

?

Write a SQLite database using C # Add and delete changes to the statement (and insert byte[] in the summary of problems encountered)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.