基于winForm中Txt文件内容导入到数据源(导出到Txt)
作者:月神 日期:2011-12-15
private void button1_Click(object sender, EventArgs e)
{ // 以是winForm 代码
// ##################OpenFileDialog 打开数据文件开始 ###########################
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// string[] str = File.ReadAllLines(openFileDialog1.FileName);
if (string.IsNullOrEmpty(File.ReadAllText(openFileDialog1.FileName)) == false)
{
int count = 0;
using (SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog = mydb2; User ID=sa;Password=123456 "))
{
conn.Open();
string[] lines = File.ReadAllLines(openFileDialog1.FileName);
foreach (string ls in lines)
{
string[] line = ls.Split('|');
string name = line[0];
int age = Convert.ToInt32(line[1]);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Select * FROM TEST Where USERNAME=@NAME AND AGE = @AGE";
cmd.Parameters.AddWithValue("NAME", name);
cmd.Parameters.AddWithValue("AGE", age);
SqlDataReader reader = cmd.ExecuteReader();
bool b = reader.Read();
reader.Dispose();
if (b == false)
{
cmd.CommandText = "Insert INTO TEST (USERNAME,AGE) VALUES (@NAMES,@AGES)";
cmd.Parameters.AddWithValue("NAMES", name);
cmd.Parameters.AddWithValue("AGES", age);
cmd.ExecuteNonQuery();
count++;
}
}
MessageBox.Show("插入了" + count + "条数据");
}
}
else
{
MessageBox.Show("数据文件内容为空");
}
}
}
// ########## saveFileDialog 导出数据到文件 ####################
private void button3_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog = mydb2; User ID=sa;Password=123456 "))
{
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * FROM TEST", conn);
DataSet ds = new DataSet();
da.Fill(ds);//将读取的数据写入内存缓存中去
int records = ds.Tables[0].Rows.Count; //获取缓存中集合行数的总数
string[] lines = new string[records];//创建一个字符串型的数据,并指明数组中的元素个数
int counts = 0;
for (int i = 0; i < records; i++)
{
DataRow row = ds.Tables[0].Rows[i]; //获取内存中第一个表中的行,如jim,30
for (int j = 0; j < row.ItemArray.Length; j++)
{
lines[i] += row[j].ToString();//向数组添加成员
}
counts++;
}
saveFileDialog1.ShowDialog();
File.WriteAllLines(saveFileDialog1.FileName, lines);
MessageBox.Show("导出了" + counts + "条数据");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
文章来自: 本站原创
Tags: OpenFileDialog SaveFileDialog
相关日志:
上一篇
下一篇

//不使用任何控件,实现导出数据(导入类似)
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=localhost;Initial catalog=mydb2;User ID=sa;Password=123456 "))
{
string sql = "Select * FROM TEST";
string Paths = @"C:\Users\acer\Desktop\a.txt";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
//关键代码
int records = ds.Tables[0].Rows.Count; //获取缓存中第一个表集合行数的总数
string[] lines = new string[records];//创建一个字符串型的数据,并指明数组中的元素个数
int counts = 0;
for (int i = 0; i < records; i++)
{
DataRow row = ds.Tables[0].Rows[i]; //获取内存中第一个表中的行,如jim,30
for (int j = 0; j < row.ItemArray.Length; j++)
{
lines[i] += row[j].ToString();//向数组添加成员
}
counts++;
}
File.WriteAllLines(Paths, lines); //path如果没有则会创建
//写入到文本框中去
foreach (string s in lines)
{
textBox1.Text += s+Environment.NewLine;
}
MessageBox.Show("导出了" + counts + "条数据");
}
}