如何使用脚本代码为我的数据库创建表 [英] How to create tables to my database using script code
本文介绍了如何使用脚本代码为我的数据库创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
海,
我在一个文件夹中有40个表的脚本.我想使用此脚本文件在我的数据库中创建表.请任何人帮助,如何一次使用此脚本创建表.
谢谢
Hai,
I have a Script for 40 tables which is in one folder. I want to create table to my database using this script file. Please anybody help, how to create tables using this script at a time.
Thank you
推荐答案
一个脚本一个地运行吗?!
好的,一次运行它们,在这里:使用SQLCMD来执行多个SQL Server脚本 [
Run the script one-by-one?!
Ok, to run them at once, here: Using SQLCMD to Execute Multiple SQL Server Scripts[^]
为此,您必须创建一个批处理文件,该文件将从某个文件夹中存在的文件中读取脚本,然后执行它.请参考以下链接:
http://social.msdn.microsoft.com/Forums/zh/sqlgetstarted/thread/7e00c796-5854-4a2c-91d3-7b325420fd14 [ http://stackoverflow.com/questions/3794897/need-help-to-write-bat-file-that-execute-sql-scripts-in-sql-server-2008-and-ano [生成osql批处理脚本 [
For this you will have to create a batch file which will read your scripts from the files present in some folder and then execute it. Please refer following links:
http://social.msdn.microsoft.com/Forums/en/sqlgetstarted/thread/7e00c796-5854-4a2c-91d3-7b325420fd14[^]
http://stackoverflow.com/questions/3794897/need-help-to-write-bat-file-that-execute-sql-scripts-in-sql-server-2008-and-ano[^]
Generating osql Batch Scripts[^]
Regards
Praveen
使用SQL ManagementStudio手动
打开或创建新数据库
在查询窗口中打开脚本文件(* .sql)
执行...
对于C#代码,示例为:
Manually with SQL ManagementStudio
Open or create new database
open scriptfile (*.sql) in query window
execute...
for C# code, example is:
//create DB
string sqlString = string.Format("USE [master] CREATE DATABASE {0}", dbName);
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(sqlString, conn);
bool err = false;
try
{
conn.Open();
int r = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
created = "\nCreate database failed: " + ex.Message;
err = true;
}
conn.Close();
if (err) return created;
参见codeproject链接:
使用Installer类轻松部署SQL Server数据库 [ ^ ]
链接摘录:
see codeproject link:
Deploy SQL Server databases easily with an Installer class[^]
excerpt from link:
//delete all GO cmds
sr = new StreamReader("CreateTables.sql");
readsql = sr.ReadToEnd();
Regex regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string[] SqlLine = regex.Split(readsql);
这篇关于如何使用脚本代码为我的数据库创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文