- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Data.SqlClient;
- namespace test
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private static string connectionString = "server=127.0.0.1;uid=sa;pwd=;database=gpsdb2";
- private void button1_Click(object sender, EventArgs e)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- string sql = " select a.OrderCode as 订单编号,a.SimNum as SIM卡号,b.CarNum as 车牌号 "+
- "from df_shipplanInfo a inner join giscar b on a.simnum=b.simnum";
- DataSet ds = new DataSet();
- try
- {
- connection.Open();
- SqlDataAdapter sda = new SqlDataAdapter(sql, connection);
- sda.Fill(ds);
- DataTable dt = ds.Tables[0];
- dataGridView1.DataSource = dt;
- using (FileStream fileStream = new FileStream("d:test.txt",FileMode.OpenOrCreate))
- {
- char[] charArry;
- string str = string.Empty;
- foreach (DataRow dr in dt.Rows)
- {
- str +="订单编号:"+dr[0].ToString()+"SIM卡号:"+dr[1].ToString()+"车牌号:"+dr[2].ToString()+'\n';
-
- }
- charArry = str.ToCharArray();
- Encoder encode = Encoding.UTF8.GetEncoder();
- byte[] arry = new byte[charArry.Length*2];
- encode.GetBytes(charArry, 0, charArry.Length, arry, 0, true);
- fileStream.Write(arry, 0, arry.Length);
- //Console.ReadKey();
- fileStream.Close();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- connection.Close();
- }
- }
- }
- }[img]D:\test.png[/img]
- }
复制代码
|