>  기사  >  데이터 베이스  >  SQL你必须知道的

SQL你必须知道的

WBOY
WBOY원래의
2016-06-07 17:43:151018검색

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

,美国服务器,虚拟主机
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.