博客开通第三十三天

导入扫频数据(将后缀名为.txt或者.FS*的文件里的内容导入到数据库中)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string fileName = ofd.FileName;
DataGridViewRow dgRow = this.dataGridView1.Rows[this.dataGridView1.Rows.Add()];

dgRow.Cells["Column1"].Value = fileName.Substring(fileName.LastIndexOf("\\") + 1, fileName.IndexOf('.') - fileName.LastIndexOf("\\") - 2);
dgRow.Cells["Column3"].Value = fileName.Substring(fileName.IndexOf('.') + 1);
dgRow.Cells["Column2"].Value = fileName;


}
}

private void button2_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=123456"))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;

string sqlDeleteStr = "truncate table TB_CW";

cmd.CommandText = sqlDeleteStr;
cmd.ExecuteNonQuery();

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
StreamReader sr = new StreamReader(dataGridView1.Rows[i].Cells["Column2"].FormattedValue.ToString());

while (true)
{
string str = sr.ReadLine();
if (!string.IsNullOrEmpty(str))
{

if (str.Contains("CW") && str.Contains("-"))
{

string[] list = str.Split(' ');
#region 下面写SQL语句
string sql = "Insert into TB_CW(model,coordinates,time," +
"filter_bandwidth,central_frequent,fssi) values('"
+ list[0] + "','" + list[1] + " " + list[2] + " "
+ list[3] + " " + list[4] + " " + list[5] + " "
+ list[6] + " " + list[7] + "','"+ list[8] + "','"
+ list[9] + "','" + list[10] + "','"+list[11]+"')";//+ list[3] + "','" + list[4] + "','" + list[5] + "','" + list[6] + "','" + list[7] + "','"

cmd.CommandText = sql;
cmd.ExecuteNonQuery();
#endregion
}
}
else
break;
}
sr.Close();
}

conn.Close();
}

}
}
}

原文地址:https://www.cnblogs.com/licc09/p/3039468.html