Importing data to ADO.net

Source: Internet
Author: User

After learning the three layers, I found a lot of deficiencies, so I found some materials to supplement it. I was very confused when I was reading the code for importing and exporting files during the ado video, the reason is that you are not familiar with the OpenFileDialog control method. First, we will show you a simple but tedious program:

Private void btnInput_Click (object sender, EventArgs e) {if (ofdInput. showDialog () = DialogResult. OK) // whether to import the File and click the "open" button {using (FileStream filestream = File. openRead (ofdInput. fileName) // If you import a file and press the "open" button, the file content will be written to filestream {using (StreamReader streamReader = new StreamReader (filestream )) // StreamReader is the {string line = null used to read filestream; // The name variable line. The default value is null. It is used to read data one by one while (line = streamReader. readLine () )! = Null) // one piece of read data {string [] strs = line. split ('|'); // defines the separator "|" string name = strs [0] for the Split field; // The first number of each entry is name int age = Convert. toInt32 (strs [1]); // The second number is age, because Age in the database is of the int type, and here strs [] assigns the string type to age, therefore, a conversion process, using (SqlConnection conn = new SqlConnection ("server = .; database = IOdata; uid = sa; password = 123456 ") // create a database connection {conn. open (); // Open the connection using (SqlCommand cmd = conn. createCommand () // create an SQL command {cmd. commandText = "Insert into T_Persons (Name, Age) values (@ Name, @ Age)"; // Insert data into cmd. parameters. add (new SqlParameter ("Name", name); // assign a cmd value to the name using parameterization. parameters. add (new SqlParameter ("Age", age); cmd. executeNonQuery ();}}
In the above program, we know that filestream is used to write the file to be opened by the user, and streamreader is used to read the file content, after connecting to the database, insert the content read from the file into the database to import the program. However, this is a very simple process, however, the connection to the database is used here, and this process is very troublesome, so we can simplify it and reuse sqlcommand. Below we will simplify it.

Private void btnInput_Click (object sender, EventArgs e) {if (ofdInput. ShowDialog ()! = DialogResult. OK) // if you do not press the OPEN button {return;} using (FileStream filestream = File. openRead (ofdInput. fileName) // If you import a file and press the "open" button, the file content will be written to filestream {using (StreamReader streamReader = new StreamReader (filestream )) // StreamReader is used to read filestream {Using (SqlConnection conn = new SqlConnection ("server =.; database = IOdata; uid = sa; password = 123456 "))// Create a database connection {conn. open (); // Open the connection using (SqlCommand cmd = conn. createCommand () // create an SQL command {cmd. commandText = "Insert into T_Persons (Name, Age) values (@ Name, @ Age)"; // Insert data string line = null; // Name the variable line, the default value is null, which is used to read data one by one while (line = streamReader. readLine ())! = Null) // one piece of read data {string [] strs = line. split ('|'); // defines the separator "|" string name = strs [0] for the Split field; // The first number of each entry is name int age = Convert. toInt32 (strs [1]); // The second number is age, because Age in the database is of the int type, and here strs [] assigns the string type to age, therefore, there must be a conversion process cmd. parameters. clear (); cmd. parameters. add (new SqlParameter ("Name", name); // assign a cmd value to the name using parameterization. parameters. add (new SqlParameter ("Age", age); cmd. executeNonQuery () ;}}}} MessageBox. show ("imported successfully ");
Compared with the previous Code, although the changes are not significant, it is very useful for a large program. It is time-consuming to open or close the database repeatedly, and the user has no patience, what they need is that the sooner the better, the better, so they are in line with the explanation of the previous test system's senior brother and sister, big data problems ......



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.