C # Connect to the database and insert data (insert)

Source: Internet
Author: User
Tags exception handling

C # Connect to a database and insert data

C # is not my main direction, in our course, although there is C #, so far, just talked about Windows Forms programming here, before that, C # only talked about some basics, so, I am very food, a C # rookie. But a few days ago a friend of mine asked me to give them some procedural questions, their programming language is just C #, so laborious to use a lot of C # knowledge, fortunately, C # syntax and Java is very similar, I refer to the programming of Java programming ideas, using C # Grammar to write programs, not the grammar online check, And finally managed to get the project done, project is about GPRS positioning of an application (because I just help, not directly involved, so it is not directly to disclose the project content), use of C # object-oriented programming, multithreading, forms, exception handling, connect the database and storage data and other knowledge, speaking of databases, My common database is Mysql,sql server,oracle, but this project is easy to use, because access and other databases are slightly different, so in the connection process, Encountered a lot of small problems (including the information on the Internet is not explicit insert problem inserts), it took me a day to freely access the Access database and insert data, the test code is published as follows:

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Data.OleDb;

Namespace ConsoleApplication1

{

Class Program

{

static void Main (string[] args)

{

String aa= "DFDSFSD";

String bb= "Dfsdfds";

String Sqlcon = "provider=microsoft.jet.oledb.4.0;" Data Source=d:\\gprs1.mdb ";

String sql = "INSERT into Tebleone ([datedtr],[datetime]) VALUES (' +aa+ ', ' +bb+ ')";

String sql = String.Format ("INSERT into Tebleone ([datedtr],[datetime]) values (' {0} ', ' {1} ')", AA,BB);

OleDbConnection c =new OleDbConnection (Sqlcon);

Try

{

OleDbCommand com = new OleDbCommand ();

Com. Connection = C;

Com.commandtext = SQL;

C.open ();

Com. ExecuteNonQuery ();

}

catch (Exception e)

{

Console.WriteLine ("Out of the wrong 䨪");

}

finally {

C.close ();

}

}

}

}

Need to pay attention to is the writing of the SQL statement, I think and other databases are different, I follow the other database of SQL statements, always error, to the above type, it is true. The second question, the data is stored in the process and other I feel slightly different, so

OleDbCommand com = new OleDbCommand ();

Com. Connection = C;

Com.commandtext = SQL;

C.open ();

Com. ExecuteNonQuery ();

The order is not reversed, as with the search, as with other databases, it can be used arbitrarily (as long as it can be used by people).

The above content is purely personal hand-written, if a warrior to see what deficiencies or the wrong place, please do not hesitate to enlighten. I thank you in advance in this 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.