最近几天公司要总是传数据,原来直接用SQL 语句传来传去的,当在两个不同版本的数据库之间传数据里比较麻烦,所以就写了一个Winform的小程序,来解决这个问题,
希望对大家有用,拿出来与大家分享一下:
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.Data.SqlClient;
using System.IO;
using System.Threading;
namespace SLQ
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(textBox1.Text.Trim());
con.Open(); MessageBox.Show("已经成功连接到SQL Server服务器");
}
catch (Exception)
{
MessageBox.Show("链接到SQL Server失败!!");
}
}
private void button2_Click(object sender, EventArgs e)
{
//SaveFileDialog openfile = new SaveFileDialog();
//openfile.FileName = "SQL_Insert_Data";
//openfile.DefaultExt = ".txt"; // Default file extension
//openfile.Filter = "Text documents (.txt)|*.txt"; //
//Nullable<bool> result = openfile.ShowDialog();
SaveFileDialog dia = new SaveFileDialog();
dia.FileName = "SQL_Insert_Data";
dia.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (dia.ShowDialog()==DialogResult.OK)
{
if(dia.FileName!="")
{
textBox3.Text = dia.FileName;
}
}
//if (result == true)
//{
// textBox3.Text = openfile.FileName.ToString();
//}
}
DataTable dt;
Thread thread;
private void button3_Click(object sender, EventArgs e)
{
if (textBox3.Text=="")
{
MessageBox.Show("请选择文件要存放的路径!");
return;
}
dt = ReturnDataTable();
progressBar1.Maximum = dt.Rows.Count;
label7.Text = progressBar1.Maximum.ToString();
thread = new Thread(new ThreadStart(GoOut));
thread.Start();
// GoOut();
}
int m = 0;
int totle = 0;
private void GoOut()
{
StringBuilder sb = new StringBuilder();
string insertSQL = "";
string mus = "";
string ss = "";
foreach (DataRow item in dt.Rows)
{
totle += 1;
insertSQL = "insert " + textBox2.Text.Trim() + " values(";
for (int i = 0; i < dt.Columns.Count; i++)
{
mus = "'" + item[i].ToString() + "'";
if (radioButton2.Checked == true)
{
if (item[i].GetType() == typeof(Int32) || item[i].GetType() == typeof(Guid))
{
mus = item[i].ToString();
}
}
else
{
if (i == 0)
{
continue;
}
if (item[i].GetType() == typeof(Int32))
{
mus = item[i].ToString();
}
}
mus += ",";
insertSQL += mus;
// i++;
}
insertSQL = insertSQL.Substring(0, insertSQL.LastIndexOf(","));
insertSQL += ")";
insertSQL += "\r\n";
// File.AppendAllLines(textBlock3.Text,insertSQL.ToString(),Encoding.Default);
ss += insertSQL;
}
File.WriteAllText(textBox3.Text, ss);
MessageBox.Show("导出完成!");
}
private DataTable ReturnDataTable()
{
try
{
using (SqlConnection con = new SqlConnection(textBox1.Text.Trim()))
{
using (SqlCommand cmd = new SqlCommand("select * from " + textBox2.Text.Trim(), con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
catch (Exception)
{
MessageBox.Show("请检查数据是否正确的连接!!!");
return new DataTable();
}
}
private void button4_Click(object sender, EventArgs e)
{
thread.Abort();
Application.Exit();
}
private void button5_Click(object sender, EventArgs e)
{
progressBar1.Maximum = 1000000;
Thread thread = new Thread(new ThreadStart(CallThread));
thread.Start();
//CallThread();
}
int i = 0;
private void CallThread()
{
while (i <= 1000000)
{
Insert();
i++;
}
}
private void Insert()
{
using (SqlConnection con = new SqlConnection(textBox1.Text.Trim()))
{
using (SqlCommand cmd = new SqlCommand("insert T_user values('xiaoming','xiaoming','979170768')", con))
{
con.Open();
cmd.ExecuteNonQuery();
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Value =totle;
label5.Text = totle.ToString();
}
}
}
源码在我的CSDN资源中,大家可以下载
将数据库中的数据生成插入语句(用于数据的导出与导入)
最新推荐文章于 2023-04-11 11:25:10 发布