Import data (ADO. Net)

Source: Internet
Author: User

Source code record

Add

string dataDir = AppDomain.CurrentDomain.BaseDirectory;            if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))            {                dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;                AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);            }  

Code in the import button

Using system; using system. windows. forms; using system. data. sqlclient; using system. io; log on to the bathroom using namespace. Hi {public partial class form1: FORM {public form1 () {initializecomponent ();} private void button#click (Object sender, eventargs e) {If (ofdimport. showdialog () = dialogresult. OK) {using (filestream = file. openread (ofdimport. filename) {using (streamreader = new streamr Eader (filestream) {string line = NULL; while (line = streamreader. Readline ())! = NULL) {string [] STRs = line. split ('|'); string name = STRs [0]; int age = convert. toint32 (STRs [1]); Using (sqlconnection conn = new sqlconnection (@ "Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | \ db1.mdf; Integrated Security = true; user instance = true ") {Conn. open (); Using (sqlcommand cmd = Conn. createcommand () {cmd. commandtext = "insert into t_persons (name, age) values (@ name, @ age)"; cmd. parameters. add (New sqlparameter ("name", name); cmd. parameters. add (New sqlparameter ("Age", age); cmd. executenonquery ();}}}}}}}}}
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.