数据导入OpenFileDialog
2013-05-22 17:49
253 查看
导入文件:张三|40 捷克俊逸|27 罗布特|32 以1.txt形式保存
private void btnImport_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "文本文件|*.txt"; if (ofd.ShowDialog()!=DialogResult.OK) { return; } string filename = ofd.FileName; //ReadAllLines是把文件一次读取到string集合中 IEnumerable<string> lines=File.ReadAllLines(filename); foreach (string line in lines) { string[] str = line.Split('|'); string name = str[0]; string age = str[1]; //插入数据库操作 string strconn = "Data Source=.\\SQLEXPRESS;Initial Catalog=My_Test;Integrated Security=True"; SqlConnection conn = new SqlConnection(strconn); conn.Open(); string strSql = "insert into T_table1(name,age) values ('"+name+"','"+Convert .ToInt32(age)+"')"; SqlCommand cmd = new SqlCommand(strSql ,conn); if (cmd.ExecuteNonQuery() > 0) { MessageBox.Show("数据导入成功!"); } else { MessageBox.Show("数据导入失败!"); } } }
相关文章推荐
- 从文本文件向数据库内插入数据(openfiledialog等控件)
- openfiledialog 使用 读取txt文件 StreamReader 检验数据
- [导入]OpenFileDialog的功能有哪些?
- Excel数据,dataGridview, openFileDialog
- 使用GDBopenFileDialog打开数据并显示
- openFileInput()和 openFileOutput()方法利用java流将数据存储到文件中
- openFileOutput() openFileInput() 对数据进行读取和存储
- OpenFileDialog、SaveFileDialog常用属性、对话框用法
- C#中OpenFileDialog和PictrueBox的用法分析
- 扯扯OpenFileDialog和.NET的缺省目录
- 获取OpenFileDialog的文件名和文件路径
- 使用矩阵方式进行文件夹的选择即类似OpenFileDialog
- OpenFileDialog And SaveFileDialog
- FotoVision对OpenFileDialog的用法
- C# 对话框类(OpenFileDialog,SaveFileDialog,FolderBrowserDialog)
- mysql使用load file导出导入[大]批量数据
- 使用OpenFileDialog选择文件
- 【winform OpenFileDialog】 上传控件教程
- MySql使用全记录5 -----解决Windows下用命令导入sql文件时"Failed to open file"的问题