using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Threading;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
delegate void SendToParent(string txt);
// public delegate string SendToParent();
mysqlConnect conn = new mysqlConnect();
DataTable dt = new DataTable();
time tm = new time();
private void Form1_Load(object sender, EventArgs e)
{
conn.getConnection();
}
private void ConnServer()
{
SendToParent stc = new SendToParent(ConnServerRes);
SendToParent lb = new SendToParent(lbtext);
//线程的相关操作
this.Invoke(lb, new object[] { "获取用户名...." });
DataTable dm = conn.executeQuery("select * from bbsmember");
string[] meber=new string[dm.Rows.Count];
for (int i = 0; i < dm.Rows.Count; i++)
{
meber[i] = dm.Rows[i]["username"].ToString();
}
this.Invoke(lb, new object[] { "等待插入...." });
for (int i = 0; i < dt.Rows.Count; i++)
{
string title = dt.Rows[i]["标题"].ToString();
string text = dt.Rows[i]["内容"].ToString();
//string pic = dt.Rows[i]["pic"].ToString();
int dy = DateTime.Now.Day + 1;
string year = DateTime.Now.Year.ToString();
string m = DateTime.Now.Month.ToString().PadLeft(2, '0');
string s = DateTime.Now.Second.ToString().PadLeft(2, '0');
string date = DateTime.Now.Year.ToString() + "/"+DateTime.Now.Month.ToString().PadLeft(2,'0')+"/"+dy.ToString();
Random rd = new Random();
string h = rd.Next(0, 23).ToString().PadLeft(2, '0');
string min = rd.Next(0, 59).ToString().PadLeft(2, '0');
string tpdate = year+m+dy+h+min+s;
string view = rd.Next(20,30).ToString();
string i_id = "50_" + rd.Next(1,3).ToString() + "0";
int j = rd.Next(1,37);
string author_ip = meber[j];
string sql = "insert into bbslistdata(title,text,date,reply,views,author_ip,topdate,area_id) values ('" + title + "','" + text + "','" + date + "',0,'" + view + "','" + author_ip + "','" + tpdate + "','" + i_id + "')";
if (conn.executeUpdate(sql) == true)
{
this.Invoke(stc, new object[] { "插入成功" });
}
}
this.Invoke(lb, new object[] { "插入完毕" });
}
private void ConnServerRes(string str)
{
//操作主线程中的控件
listBox1.Items.Add(str);
}
private void lbtext(string str)
{
//操作主线程中的控件
label1.Text=str;
}
private void button2_Click(object sender, EventArgs e)
{
string sql = "select * from data_content_1 order by ID ASC limit 50";
dt = conn.executeQuery(sql);
dataGrid1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
Thread connThread = new Thread(ConnServer);
//connThread = new Thread(new ThreadStart(ConnServer));
connThread.IsBackground = true;
connThread.Start();
}
}
}