博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL Server 数据导入
阅读量:7103 次
发布时间:2019-06-28

本文共 2508 字,大约阅读时间需要 8 分钟。

ExcuteNonQuery() 返回影响的行数

ExcuteSacalar() 返回第一行的第一列

ExcuteReader() Reader指针,指向表的表头

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Configuration; 5 using System.Data; 6 using System.Data.SqlClient; 7 using System.Drawing; 8 using System.IO; 9 using System.Linq;10 using System.Text;11 using System.Threading.Tasks;12 using System.Windows.Forms;13 14 namespace UserInfo15 {16     public partial class MainFrm : Form17     {18         public MainFrm()19         {20             InitializeComponent();21         }22 23         private void btnOpen_Click(object sender, EventArgs e)24         {25             using (OpenFileDialog ofd = new OpenFileDialog())26             {27                 ofd.Filter = "文本文件|*.txt";28                 if (ofd.ShowDialog()==DialogResult.OK)29                 {30                     this.textPath.Text = ofd.FileName;31                     //导入数据32                     ImportData(ofd.FileName);33                 }34             }35 36         }37 38         //导入数据的方法39         private void ImportData(string fileName)40         {41             string temp = string.Empty;42             using (StreamReader reader=new StreamReader(fileName,Encoding.UTF8))43             {44                 //去掉第一行45                 reader.ReadLine();46                 //string str = "server=.;uid=sa;pwd=tracy05;database=Test";47                 string str = ConfigurationManager.ConnectionStrings["sqlConn"].ConnectionString;48                 using (SqlConnection conn=new SqlConnection(str))49                 {50                     using (SqlCommand cmd=conn.CreateCommand())51                     {52                         conn.Open();53                         while (!string.IsNullOrEmpty(temp = reader.ReadLine()))54                         {55                             var strs = temp.Split(',');56                             string sql = string.Format(@"INSERT INTO 57                                                 UserInfo(StuName,StuSex,StuBirthdate,StuPhone) 58                                                 VALUES('{0}','{1}','{2}','{3}')",59                                                         strs[1], strs[2], strs[3], strs[4]);60                             cmd.CommandText = sql;61                             cmd.ExecuteNonQuery();62                         }63                     }64 65                 }66 67             }68         }69     }70 }

 

转载于:https://www.cnblogs.com/shuichangdong/p/8318957.html

你可能感兴趣的文章
“你的深度学习框架包含15个漏洞”,360说 | 附论文
查看>>
Android应用程序组件Content Provider的启动过程源代码分析(3)
查看>>
部署及配置ISCSI Target,Livemigration系列之三
查看>>
rundeck Web页面配置node节点
查看>>
Java程序员,笔试必读
查看>>
mySQL教程 第4章 数据查询
查看>>
linux 下 eclipse 开发环境的搭建
查看>>
android中DatePicker&TimePicker的应用
查看>>
JavaScript和C#通用gb2312和utf8编码解码函数简单实现
查看>>
在创建触发器时出现不能在 'inserted' 表和 'deleted' 表中使用 text、ntext 或 image 列...
查看>>
arguments,callee&caller测试
查看>>
sql server 2012序列号
查看>>
一步一步带你实现virtual dom(一)
查看>>
android:http
查看>>
详解JAVA实现支付宝接口编程
查看>>
HTTP 请求返回代码含义
查看>>
python+soket实现UDP协议的客户/服务端中文聊天程序
查看>>
android:使用BaseExpandableListAdapter实现可折叠的列表
查看>>
什么时候用存储过程
查看>>
【MongoDB for Java】Java操作MongoDB
查看>>