Ado.Net小练习01(数据库文件导出,导入)
来源:互联网 发布:上海公务员备考知乎 编辑:程序博客网 时间:2024/06/07 01:20
数据库文件导出主要程序:
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;">namespace _02数据库文件导出</span></span>
{
class Program
{
static void Main(string[] args)
{
string str = "Data Source=.\\SQLExpress;Initial Catalog=Test;Integrated Security=True";
using (SqlConnection con=new SqlConnection(str))
{
string sql = "select UserId, UserName, UserPwd from UserLogin";
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
using (SqlDataReader reader=cmd.ExecuteReader())
{
if (reader.HasRows)
{
using (StreamWriter sw=new StreamWriter("1.txt"))
{
sw.WriteLine("{0},{1},{2}",reader.GetName(0),reader.GetName(1),reader.GetName(2)); //获取列的名称
while (reader.Read())
{
sw.WriteLine("{0},{1},{2}",reader[0],reader[1],reader[2]);
}
}
}
}
}//endusing
}//endusing
Console.WriteLine("导出数据成功!");
Console.ReadKey();
}
}
}
导入数据到数据库:
namespace _03导入数据库
{
class Program
{
static void Main(string[] args)
{
string str = "Data Source=.\\SQLExpress;Initial Catalog=Test;Integrated Security=True";
using (StreamReader reader=new StreamReader("13.txt"))
{
string line = reader.ReadLine(); //第一行列名读完了,不要了
using (SqlConnection con=new SqlConnection(str))
{
con.Open();
string sql = "insert into UserLogin values(@UserName, @UserPwd)";
SqlParameter[] ps =
{
//告诉数据库,我的参数中存的值要以varchar类型存到表中
new SqlParameter("@UserName",SqlDbType.VarChar),
new SqlParameter("@UserPwd",SqlDbType.VarChar),
};
using (SqlCommand cmd=new SqlCommand(sql,con))
{
cmd.Parameters.AddRange(ps); //AddRange(ps)要放在外边,放在while循环里面会又添加一次ps,会报错
while ((line = reader.ReadLine()) !=null)
{
string[] txts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
//把参数用什么值替换
ps[0].Value = txts[1];//名字
ps[1].Value = txts[2];//密码
cmd.ExecuteNonQuery();
}
}
}
}
Console.WriteLine("导入数据成功");
Console.ReadKey();
}
}
}
- Ado.Net小练习01(数据库文件导出,导入)
- MySQL导出导入数据库文件
- mysql导出导入数据库文件
- MySQL导入导出数据库文件
- 导入导出Mysql数据库文件
- MySQL导入导出数据库文件
- sqoop导入导出数据库文件
- mysql导入导出数据库文件
- 命令行导入导出数据库文件
- 黑马程序员--ADO.NET数据导入导出
- Ado.Net小练习03(省市联动)
- oracle导入导出dmp数据库文件
- 数据库文件的导入和导出
- Oracle 数据库文件的导入导出
- oracle数据库文件中的导入/导出(imp/exp命令)
- oracle数据库文件中的导入/导出(imp/exp命令)
- oracle数据库文件中的导入/导出(imp/exp命令)详解
- oracle数据库文件中的导入/导出(imp/exp命令)
- 【python】获取指定日期的后(前)一(n)天
- 各种排序的比较
- Java SE 学习 第二讲
- Android开发之如何监听让服务不被杀死(service+broadcast)
- caxa cad电子图版2016 r1官方版
- Ado.Net小练习01(数据库文件导出,导入)
- iOS开发真机测试could not find developer disk image的解决
- Nginx:域名转发(CentOS7)
- 各种系统Dos下编译和运行java
- URIEncoding和useBodyEncodingForURI详解
- Android手机 Fildder真机抓包
- CentOS 安装vsftp 建新用户的方法
- python字符串函数
- 对比Pig、Hive和SQL,浅看大数据工具之间的差异 -- 转发