1. 创建存储过程
CREATE procedure LP_CreateTab
@Tab_NameInfo varchar(20),
@Tab_NameAchv varchar(20)
AS
BEGIN TRANSACTION
exec('Create Table '+@Tab_NameInfo+'
( Fd_No nvarchar(15),
Fd_XL nvarchar(10) ,
Fd_Master nvarchar(10),
Fd_StdNo nvarchar(3) primary key,
Fd_StdName nvarchar(20),
Fd_Age nvarchar(4) ,
Fd_Zoom nvarchar(15),
FOREIGN KEY (Fd_No) REFERENCES Tab_Class
)')
exec('Create Table '+@Tab_NameAchv+'
( Fd_No nvarchar(15),
Fd_StdNo nvarchar(3),
Fd_StdName nvarchar(20),
Fd_SubNo nvarchar(4) ,
Fd_SubName nvarchar(15),
Fd_Credit int,
Fd_Grd float,
Fd_KNo int,
Fd_KFlag nvarchar(4)
FOREIGN KEY (Fd_No) REFERENCES Tab_Class
)')
IF @@ERROR=0
BEGIN
COMMIT TRANSACTION
RETURN 0
END Else
BEGIN
ROLLBACK TRANSACTION
RETURN -1
END
GO
2. C#中调用public void createTable(string tableNameInfo,string tableNameAchv){ try { sqlConnection1 = new sqlConnection(@"Data Source=(local);" + "Integrated Security=sspI;" + "Initial Catalog=pubs"); sqlConnection1.open(); sqlCommand1 = new sqlCommand("LP_CreateTab",sqlConnection1); sqlCommand1.CommandType = CommandType.StoredProcedure; sqlCommand1.Parameters.Add("@Tab_NameInfo",System.Data.sqlDbType.Char,20); sqlCommand1.Parameters["@Tab_NameInfo"].Value = tableNameInfo; sqlCommand1.Parameters.Add("@Tab_NameAchv",20); sqlCommand1.Parameters["@Tab_NameAchv"].Value = tableNameAchv; sqlCommand1.ExecuteNonQuery(); sqlConnection1.Close(); } catch(Exception error) { System.Diagnostics.Debug.Write(error.Message); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。