怎么产生一个长串的字符来做ID?急,马上结贴! select newid(),* from table 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 insert into table4([id],[name]) values(newid(),'cutesun') 创建 uniqueidentifier 类型的唯一值。 我这里有一个可以随机生成小写字母字符串的方法,length是字符串的长度public string CreateRandomString(int length) { Encoding encoding = Encoding.ASCII; byte[] buf = new Byte[encoding.GetByteCount(new string(' ', length))]; for (;;) { Random r = new Random(); for(int i=0;i<length;i++) { int ASCIINum = r.Next(97,122); buf[i] = (byte)ASCIINum; } try { return new string(encoding.GetChars(buf)); } catch(Exception) { } } } 下面的示例创建具有 uniqueidentifier 数据类型的 cust 表,并使用 NEWID 将默认值填充到表中。为 NEWID() 赋默认值时,每个新行和现有行均具有 cust_id 列的唯一值。 -- Creating a table using NEWID for uniqueidentifier data type. CREATE TABLE cust( cust_id uniqueidentifier NOT NULL DEFAULT newid(), company varchar(30) NOT NULL, contact_name varchar(60) NOT NULL, address varchar(30) NOT NULL, city varchar(30) NOT NULL, state_province varchar(10) NULL, postal_code varchar(10) NOT NULL, country varchar(20) NOT NULL, telephone varchar(15) NOT NULL, fax varchar(15) NULL)GO-- Inserting data into cust table.INSERT cust(cust_id, company, contact_name, address, city, state_province, postal_code, country, telephone, fax)VALUES(newid(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL, '90110', 'Finland', '981-443655', '981-443655')INSERT cust(cust_id, company, contact_name, address, city, state_province,postal_code, country, telephone, fax)VALUES (newid(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP', '08737-363', 'Brazil', '(14) 555-8122', '')INSERT cust(cust_id, company, contact_name, address, city, state_province, postal_code, country, telephone, fax)VALUES(newid(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL, '1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')INSERT cust(cust_id, company, contact_name, address, city, state_province, postal_code, country, telephone, fax)VALUES (newid(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL, '8010', 'Austria', '7675-3425', '7675-3426')INSERT cust(cust_id, company, contact_name, address, city, state_province, postal_code, country, telephone, fax)VALUES (newid(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL, 'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68')GO 要是取数子,就改为int ASCIINum = r.Next(48,57);要是去大写字母,就改为int ASCIINum = r.Next(65,90); win7 打印问题 window2008 iis7 运行32位web问题 多线程读取数据库同一表的所有记录 Ext 和 asp.net 连用问题(新手) 关于控件选择关联的问题! system.invalidcast Exception? 如何一次访问存储过程返回多个记录集? 求救:项目忽然打不开了?????? 请求一个解决方案 连接WebService时出现"由于连接方在一段时间后没有正确答复或连接服务器失败 点击datagrid的select按扭时,如何取得该行的DataKey值? 在WEB上画曲线图,并通过鼠标点击任何位置可以取出当前点的数据?
{
Encoding encoding = Encoding.ASCII;
byte[] buf = new Byte[encoding.GetByteCount(new string(' ', length))];
for (;;)
{
Random r = new Random();
for(int i=0;i<length;i++)
{
int ASCIINum = r.Next(97,122);
buf[i] = (byte)ASCIINum;
}
try
{
return new string(encoding.GetChars(buf));
}
catch(Exception)
{
}
}
}
CREATE TABLE cust
(
cust_id uniqueidentifier NOT NULL
DEFAULT newid(),
company varchar(30) NOT NULL,
contact_name varchar(60) NOT NULL,
address varchar(30) NOT NULL,
city varchar(30) NOT NULL,
state_province varchar(10) NULL,
postal_code varchar(10) NOT NULL,
country varchar(20) NOT NULL,
telephone varchar(15) NOT NULL,
fax varchar(15) NULL
)
GO
-- Inserting data into cust table.
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,
'90110', 'Finland', '981-443655', '981-443655')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',
'08737-363', 'Brazil', '(14) 555-8122', '')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL,
'1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,
'8010', 'Austria', '7675-3425', '7675-3426')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,
'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68')
GO
int ASCIINum = r.Next(48,57);
要是去大写字母,就改为
int ASCIINum = r.Next(65,90);