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 NameGenerator;
using System.IO;
namespace sql_Insert
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Random r = new Random();
private void button1_Click(object sender, EventArgs e)
{
int Gender;
List<string> Names;
if (radioButton1.Checked)
{
Names = NameHelper.GetNames(true, Convert.ToInt32(textBox1.Text));
Gender = 1;//男
}
else
{
Names = NameHelper.GetNames(false, Convert.ToInt32(textBox1.Text));
Gender = 0;//女
}
FileStream stream = new FileStream("c:\\a.txt", FileMode.Create);
StreamWriter writer = new StreamWriter(stream, Encoding.Default);
for(int i=0;i<Convert.ToInt32( textBox1.Text);i++)
{
int age = r.Next(1, 100);
int yw = r.Next(1, 101);
int sx = r.Next(1, 101);
string insert = "insert into " + textBox2.Text + "(" + textBox3.Text + ")" + "values";
string values = "(" +"'"+ Names[i]+"'" +","+age.ToString()+","+Gender+","+yw.ToString()+","+sx.ToString()+ ")";
writer.WriteLine(insert + values);
}
writer.Close();
stream.Close();
MessageBox.Show("已完成,文件在c盤根目錄下,a.txt");
//共5個字段名,分別爲:姓名,年齡,性別,語文,數學。
//會輸出如下效果:insert into Class(姓名,年齡,性別,語文,數學)values(溫秀羽,51,女,7,70)
}
}
}
NameHelper.cs文件如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NameGenerator
{
public class NameHelper
{
#region 隨機姓名生成器
static string[] _firstName = new string[]{
"白","畢","卞","蔡","曹","岑","常","車","陳","成" ,"程","池","鄧","丁","範","方","樊","費","馮","符"
,"傅","甘","高","葛","龔","古","關","郭","韓","何" ,"賀","洪","侯","胡","華","黃","霍","姬","簡","江"
,"姜","蔣","金","康","柯","孔","賴","郎","樂","雷" ,"黎","李","連","廉","梁","廖","林","凌","劉","柳"
,"龍","盧","魯","陸","路","呂","羅","駱","馬","梅" ,"孟","莫","母","穆","倪","寧","歐","區","潘","彭"
,"蒲","皮","齊","戚","錢","強","秦","丘","邱","饒" ,"任","沈","盛","施","石","時","史","司徒","蘇","孫"
,"譚","湯","唐","陶","田","童","塗","王","危","韋" ,"衛","魏","溫","文","翁","巫","鄔","吳","伍","武"
,"席","夏","蕭","謝","辛","邢","徐","許","薛","嚴" ,"顏","楊","葉","易","殷","尤","於","餘","俞","虞"
,"元","袁","嶽","雲","曾","詹","張","章","趙","鄭" ,"鍾","周","鄒","朱","褚","莊","卓","東方","上官"
,"令狐","申屠","歐陽" };
static string _lastNameMale = "偉剛勇毅俊峯強軍平保東文輝力明永健世廣志義興良海山仁波寧貴福生龍元全國勝學祥才發武新利清飛彬富順信子傑濤昌成康星光天達安巖中茂進林有堅和彪博誠先敬震振壯會思羣豪心邦承樂紹功松善厚慶磊民友裕河哲江超浩亮政謙亨奇固之輪翰朗伯宏言若鳴朋斌樑棟維啓克倫翔旭鵬澤晨辰士以建家致樹炎德行時泰盛雄琛鈞冠策騰楠榕風航弘";
static string _lastNameFeMa = "秀娟英華慧巧美娜靜淑惠珠翠雅芝玉萍紅娥玲芬芳燕彩春菊蘭鳳潔梅琳素雲蓮真環雪榮愛妹霞香月鶯媛豔瑞凡佳嘉瓊勤珍貞莉桂娣葉璧璐婭琦晶妍茜秋珊莎錦黛青倩婷姣婉嫺瑾穎露瑤怡嬋雁蓓紈儀荷丹蓉眉君琴蕊薇菁夢嵐苑婕馨瑗琰韻融園藝詠卿聰瀾純毓悅昭冰爽琬茗羽希寧欣飄育瀅馥筠柔竹靄凝魚曉歡霄楓芸菲寒伊亞宜可姬舒影荔枝思麗墨";
public static List<string> GetNames(bool? sex, int count)
{
int fLength = _firstName.Length;
int lLength = 0;
string nameSource = string.Empty;
if (sex.HasValue)
{
lLength = sex.Value ? _lastNameMale.Length : _lastNameFeMa.Length;
nameSource = sex.Value ? _lastNameMale : _lastNameFeMa;
}
else
{
lLength = _lastNameMale.Length + _lastNameFeMa.Length;
nameSource = string.Concat(_lastNameMale, _lastNameFeMa);
}
List<string> names = new List<string>();
System.Threading.Thread.Sleep(20);
int[] Indexfn = GenerateNonRepeatArray(count, count);
System.Threading.Thread.Sleep(20);
int[] Indexln1 = GenerateNonRepeatArray(count, count);
System.Threading.Thread.Sleep(20);
int[] Indexln2 = GenerateNonRepeatArray(count, count);
int nameLength = nameSource.Length;
for (int i = 0; i < count; i++)
{
int v1 = Indexfn[i];
int v2 = Indexln1[i];
int v3 = Indexln2[i];
if (v1 >= fLength)
{
int j = Indexfn[i] / fLength;
v1 = Indexfn[i] - fLength * j;
}
if (v2 >= nameLength)
{
int j = Indexln1[i] / nameLength;
v2 = Indexln1[i] - nameLength * j;
}
if (v3 >= nameLength)
{
int j = Indexln2[i] / nameLength;
v3 = Indexln2[i] - nameLength * j;
}
if (v1 < 0 || v2 < 0 || v3 < 0)
{
Console.WriteLine();
}
if (v1 >= _firstName.Length || v2 >= nameSource.Length || v3 >= nameSource.Length)
{
Console.WriteLine();
}
string n1 = string.Empty;
string n2 = string.Empty;
string n3 = string.Empty;
names.Add(string.Format("{0}{1}{2}", _firstName[v1], nameSource[v2], nameSource[v3]));
}
return names;
}
#endregion
/// <summary>
/// 返回指定範圍內不重複的隨機數組,如範圍小於數組長度,則返回最多能生成的隨機數組
/// </summary>
/// <param name="max"></param>
/// <param name="count"></param>
/// <returns></returns>
private static int[] GenerateNonRepeatArray(int max, int count)
{
if (max <= 0) { throw new Exception("範圍不能等於或小於0"); }
int size = count > max ? count - max : 0;
List<int> temp = new List<int>();
Random ran = new Random();
while (temp.Count < (count - size))
{
int item = ran.Next(max);
if (!temp.Contains(item))
{
temp.Add(item);
}
}
return temp.ToArray();
}
}
}