如果将数据库的数据导出到文本文档中去
private void button1_Click(object sender, EventArgs e){
if (odfImport.ShowDialog() == DialogResult.OK)
{
FileStream fileStream = File.OpenRead(odfImport.FileName);
StreamReader streamReader = new StreamReader(fileStream);
SqlConnection conn = new SqlConnection(@"server=.;database=Test;Integrated Security=true;");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Insert into Users(username,password) values(@username,@password)";
string line = null;
while ((line = streamReader.ReadLine())!=null)
{
string[] strs = line.Split('|');
string name = strs[0];
string password = strs[1];
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("username", name));
cmd.Parameters.Add(new SqlParameter("password",password));
cmd.ExecuteNonQuery();
}
MessageBox.Show("导入成功");
}
}
上面是将文本文档的内容导入到数据库中 现在问题是如何将数据库中的内容导出到文本文档中去 --------------------编程问答-------------------- 你可以先读取数据库数据,然后一行一行的传值给下面的方法
public void WriteText(string strResult)
{
try
{
string FilePath = System.AppDomain.CurrentDomain.BaseDirectory + @"\test\";
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
FilePath += string.Format("test_{0}.txt", DateTime.Now.ToString("yyyy-MM-dd"));
if (System.IO.File.Exists(FilePath))
{
System.IO.FileStream stream = new System.IO.FileStream(FilePath, System.IO.FileMode.Append);
byte[] data = new UTF8Encoding().GetBytes(strResult + "\r\n");
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
}
else
{
System.IO.FileStream stream = new System.IO.FileStream(FilePath, System.IO.FileMode.OpenOrCreate);
byte[] data = new UTF8Encoding().GetBytes(strResult + "\r\n");
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
}
}
catch { }
} --------------------编程问答-------------------- 1、读取数据库中的数据
2、写文本文件,一行一行的append。
补充:.NET技术 , C#