SQL你必须知道的

php中文网
发布: 2016-06-07 17:43:15
原创
1229人浏览过

using System; using System.Data.SqlClient; using System.IO; using System.Text; using System.Windows.Forms; namespace _07_数据导入 { Form1 : Form { public Form1() {InitializeComponent();} private void button1_Click( object sender, EventArgs

using System; using System.Data.SqlClient; using System.IO; using System.Text; using System.Windows.Forms; namespace _07_数据导入 { Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e) { openFileDialog1.FileName = "" ; openFileDialog1.Filter = ; //文本内容格式如 //admin,password 一行一个用户名和密码 使用",香港虚拟主机,"分隔 //sa,piziyimao DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult .OK) { string [] lines = File .ReadAllLines(openFileDialog1.FileName); ; using (SqlConnection con = new SqlConnection (strconn)) { using (SqlCommand cmd = new SqlCommand ()) { cmd.Connection = con; con.Open(); int i = 0; foreach (var item in lines) { , ); if (strs.Length == 2) { ; //这里每次使用都要清空 否则变量名 '@name' 已声明。变量名在查询批次或存储过程内部必须唯一。 cmd.Parameters.Clear(); cmd.Parameters.AddWithValue( , strs[0]); cmd.Parameters.AddWithValue( , strs[1]); cmd.CommandText = sqlstr; i++; cmd.ExecuteNonQuery(); } } MessageBox .Show(+ i + ); } } } } private void button2_Click( object sender, EventArgs e) { openFileDialog1.FileName = "" ; openFileDialog1.Filter = ; DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult .OK) { string [] lines = File .ReadAllLines(openFileDialog1.FileName); ; using (SqlConnection con = new SqlConnection (strconn)) { using (SqlCommand cmd = new SqlCommand ()) { cmd.Connection = con; StringBuilder sb = new StringBuilder(); (var item in lines) { , ); if (arr.Length == 2) { //这里使用stringbuilder效率比string要高 而且这里一次性的产生插入语句 比上面的循环插入性能要高的多 sb.Append( , arr[0], arr[1])); //还可以使用之前学过的union 批量插入数据 这里还需要处理最后一个union的问题 不如上述简单 //sb.Append(string.Format("select'{0}','{1}'union ", arr[0], arr[1])); } } con.Open(); cmd.CommandText = sb.ToString(); int sum = cmd.ExecuteNonQuery(); MessageBox .Show(sum.ToString()); } } } } } }

,美国服务器,虚拟主机
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号