写一个程序,能够动态生成多条插入语句如:insert into MyStudents values(“人名1”,年龄,‘男’,分数1,分数2)。将生成的插入语句输出到记事本文件 SQLStr.txt中
2012-10-25 17:43
801 查看
sql_Insert窗体应用程序后台代码
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)
{
string Gender = "";
List<string> Names;
if (radioButton1.Checked)
{
Names = NameHelper.GetNames(true, Convert.ToInt32(textBox1.Text));
Gender = "男";
}
else
{
Names = NameHelper.GetNames(false, Convert.ToInt32(textBox1.Text));
Gender = "女";
}
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");
}
}
}
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();
}
}
}
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)
{
string Gender = "";
List<string> Names;
if (radioButton1.Checked)
{
Names = NameHelper.GetNames(true, Convert.ToInt32(textBox1.Text));
Gender = "男";
}
else
{
Names = NameHelper.GetNames(false, Convert.ToInt32(textBox1.Text));
Gender = "女";
}
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");
}
}
}
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();
}
}
}
相关文章推荐
- 动态生成多条插入语句如:insert into MyStudents values(“人名1”,年龄,‘男’,分数1,分数2)。将生成的插入语句输出到记事本文件 SQLStr.txt中
- 动态生成多条插入语句如:insert into MyStudents values(“人名1”,年龄,‘男’,分数1,分数2)。将生成的插入语句输出到记事本文件 SQLStr.txt中
- 动态生成多条插入语句如:insert into MyStudents values(“人名1”,年龄,‘男’,分数1,分数2)。将生成的插入语句输出到记事本文件 SQLStr.txt中
- EXCEL表里的数据转换成insert into tablename(field)values() SQL插入语句
- 造成ORA-01843 无效的月份 的一些原因 1) 当我们在一个中文环境的客户端使用如下sql语句 INSERT INTO "temptable" ( DELIVER_DATE ) VALUES
- 在SQL Server中SQL语句的Insert支持一次插入多条记录?
- python读取excel中表结构生成sql语句,存入txt文件
- C# 用sql insert into 语句向oracle插入数据数据一直未更新
- Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句
- Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句
- 规则 2.5:对于insert into…values,insert into…select语句,在字段不多的情况下,尽量一行只写一个字段。
- Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句
- SQL高级语句-Auto increment(自增) 会在新记录插入表中时生成一个唯一的数字。
- 动态生成多条插入语句 存入文本文档中
- 生成了一个txt文件,如何用记事本和EXCEL 打开它给用户看?
- java优化Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句
- 优化用SQL语句INSERT INTO … SELECT插入数据时锁全表的问题
- Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句
- 在MFC程序中,加入一个cpp文件,如果想要打印输出信息的话,用这个语句最便捷
- 何用sql语句实现:将insert语句作为一个字段,插入到表格的一条记录之中