Home  >  Article  >  Database  >  SQL你必须知道的

SQL你必须知道的

WBOY
WBOYOriginal
2016-06-07 17:43:151020browse

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()); } } } } } }

,美国服务器,虚拟主机
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn