日期:2014-05-18  浏览次数:20863 次

C# 创建数据库表
在用C#创建数据库中表的时候遇到一个问题

用sql语句创建的

从数据库导出的表脚本为:

SQL code



CREATE TABLE [dbo].[sys_Login](
    [Fid] [nvarchar](50) NOT NULL,
    [Fname] [nvarchar](50) NULL,
    [F_Pwd] [nvarchar](250) NULL,
    [F_Sex] [char](10) NULL,
    [F_BM] [nvarchar](50) NULL,
    [F_ZhiWei] [nvarchar](50) NULL,
    [F_InDate] [nvarchar](50) NULL,
    [F_Birthday] [nvarchar](50) NULL,
    [F_Phone] [nvarchar](50) NULL,
    [F_ZJType] [nvarchar](50) NULL,
    [F_ZJNumber] [nvarchar](50) NULL,
    [F_XL] [nvarchar](50) NULL,
    [F_One] [nvarchar](4000) NULL,
    [F_Two] [nvarchar](4000) NULL,
    [F_Three] [nvarchar](4000) NULL,
    [F_State] [int] NULL,
 CONSTRAINT [PK_sys_Login] PRIMARY KEY CLUSTERED 
(
    [Fid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


SET ANSI_PADDING OFF

ALTER TABLE [dbo].[sys_Login] ADD  CONSTRAINT [DF_sys_Login_F_State]  DEFAULT ((0)) FOR [F_State]






但是执行的时候 就要弄成一句话 把这些都用空格隔开 弄成一句比如:

string sql="create........((0)) FOR [F_State]"

有没有办法保持原来的样的
SQL code
string sql="
CREATE TABLE [dbo].[sys_Login](
    [Fid] [nvarchar](50) NOT NULL,
    [Fname] [nvarchar](50) NULL,
    [F_Pwd] [nvarchar](250) NULL,
    [F_Sex] [char](10) NULL,
    [F_BM] [nvarchar](50) NULL,
    [F_ZhiWei] [nvarchar](50) NULL,
    [F_InDate] [nvarchar](50) NULL,
    [F_Birthday] [nvarchar](50) NULL,
    [F_Phone] [nvarchar](50) NULL,
    [F_ZJType] [nvarchar](50) NULL,
    [F_ZJNumber] [nvarchar](50) NULL,
    [F_XL] [nvarchar](50) NULL,
    [F_One] [nvarchar](4000) NULL,
    [F_Two] [nvarchar](4000) NULL,
    [F_Three] [nvarchar](4000) NULL,
    [F_State] [int] NULL,
 CONSTRAINT [PK_sys_Login] PRIMARY KEY CLUSTERED 
(
    [Fid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


SET ANSI_PADDING OFF

ALTER TABLE [dbo].[sys_Login] ADD  CONSTRAINT [DF_sys_Login_F_State]  DEFAULT ((0)) FOR [F_State]

"


中间不用+号连接起来

------解决方案--------------------
放到文本文件里,然后在程序里读出执行
------解决方案--------------------
一种释放到文件里读出来
另一种是加@试试
string sql=@"
CREATE TABLE [dbo].[sys_Login](
[Fid] [nvarchar](50) NOT NULL,
[Fname] [nvarchar](50) NULL,
[F_Pwd] [nvarchar](250) NULL,
[F_Sex] [char](10) NULL,
[F_BM] [nvarchar](50) NULL,
[F_ZhiWei] [nvarchar](50) NULL,
[F_InDate] [nvarchar](50) NULL,
[F_Birthday] [nvarchar](50) NULL,
[F_Phone] [nvarchar](50) NULL,
[F_ZJType] [nvarchar](50) NULL,
[F_ZJNumber] [nvarchar](50) NULL,
[F_XL] [nvarchar](50) NULL,
[F_One] [nvarchar](4000) NULL,
[F_Two] [nvarchar](4000) NULL,
[F_Three] [nvarchar](4000) NULL,
[F_State] [int] NULL,
 CONSTRAINT [PK_sys_Login] PRIMARY KEY CLUSTERED 
(
[Fid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


SET ANSI_PADDING OFF

ALTER TABLE [dbo].[sys_Login] ADD CONSTRAINT [DF_sys_Login_F_State] DEFAULT ((0)) FOR [F_State]

"