Rob*_*Rob 7 sql-server delphi ado
我想做以下事情.1)创建数据库.2)创建表,存储过程等时运行脚本(此脚本由SMS'生成脚本'选项创建)
我找到了以下代码:http: //www.delphipages.com/forum/showthread.php?t = 181685 并将其修改为:
尝试
Run Code Online (Sandbox Code Playgroud)ADOQuery.ConnectionString := 'Provider=SQLOLEDB.1;Password=' +
edtPassword.Text +'; Persist Security Info = True; User ID ='+ edtUser.Text +'; Initial Catalog = master; Data Source ='+ edtServerName.Text;
Run Code Online (Sandbox Code Playgroud)ADOQuery.SQL.Clear; ADOQuery.SQL.Text := 'create DataBase ' + edtWebDBName.Text; ADOQuery.ExecSQL; // should check existance of database ADOWeb.Connected := false; ADOWeb.ConnectionString := 'Provider=SQLOLEDB.1;Password=' +
edtPassword.Text +'; Persist Security Info = True; User ID ='+ edtUser.Text +'; Initial Catalog ='+ edtWebDBName.Text +'; Data Source ='+ edtServerName.Text; ADOWeb.Connected:= true;
Run Code Online (Sandbox Code Playgroud)ADOQuery.Connection := ADOWeb; ADOQuery.SQL.Clear; ADOQuery.SQL.LoadFromFile(edtScriptFileName.Text); ADOQuery.ExecSQL; except
这一直到运行脚本文件为止.然后它会生成一个异常:"GO"附近的语法不正确.如果我在新创建的数据库上运行SMS脚本,那很好.这个问题是由于同时运行多个SQL命令(脚本本质上是一长串命令/ GO语句?如何绕过它?
哦,作为奖励,在发送脚本之前快速检查新数据库是否确实存在的任何想法?(或者它是否没有必要,因为如果创建失败,它将生成异常?)
RRU*_*RUZ 10
Rob GO
语句无法被ADO识别,因此您必须在执行前从脚本中删除.
现在要检查数据库是否存在,您可以执行这样的查询
select COUNT(*) from sys.databases where name='yourdatabasename'
Run Code Online (Sandbox Code Playgroud)
检查这个非常基本的样本
假设你有这样的脚本
CREATE TABLE Dummy.[dbo].tblUsers(ID INT, UserName VARCHAR(50))
GO
INSERT INTO Dummy.[dbo].tblUsers (ID, UserName) VALUES (1, 'Jill')
GO
INSERT INTO Dummy.[dbo].tblUsers (ID, UserName) VALUES (2, 'John')
GO
INSERT INTO Dummy.[dbo].tblUsers (ID, UserName) VALUES (3, 'Jack')
GO
Run Code Online (Sandbox Code Playgroud)
现在要执行这句话,你可以做这样的事情
const
//in this case the script is inside of a const string but can be loaded from a file as well
Script=
'CREATE TABLE Dummy.[dbo].tblUsers(ID INT, UserName VARCHAR(50)) '+#13#10+
'GO '+#13#10+
'INSERT INTO Dummy.[dbo].tblUsers (ID, UserName) VALUES (1, ''Jill'') '+#13#10+
'GO '+#13#10+
'INSERT INTO Dummy.[dbo].tblUsers (ID, UserName) VALUES (2, ''John'') '+#13#10+
'GO '+#13#10+
'INSERT INTO Dummy.[dbo].tblUsers (ID, UserName) VALUES (3, ''Jack'') '+#13#10+
'GO ';
var
DatabaseExist : Boolean;
i : Integer;
begin
try
//check the connection
if not ADOConnection1.Connected then
ADOConnection1.Connected:=True;
//make the query to check if the database called Dummy exist
ADOQuery1.SQL.Add(Format('select COUNT(*) from sys.databases where name=%s',[QuotedStr('Dummy')]));
ADOQuery1.Open;
try
//get the returned value, if is greater than 0 then exist
DatabaseExist:=ADOQuery1.Fields[0].AsInteger>0;
finally
ADOQuery1.Close;
end;
if not DatabaseExist then
begin
//create the database if not exist
ADOQuery1.SQL.Text:=Format('Create Database %s',['Dummy']);
ADOQuery1.ExecSQL;
ADOQuery1.Close;
//load the script, remember can be load from a file too
ADOQuery1.SQL.Text:=Script;
//parse the script to remove the GO statements
for i := ADOQuery1.SQL.Count-1 downto 0 do
if StartsText('GO',ADOQuery1.SQL[i]) then
ADOQuery1.SQL.Delete(i);
//execute the script
ADOQuery1.ExecSQL;
ADOQuery1.Close;
end;
except
on E:Exception do
ShowMessage(E.Message);
end;
end;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12107 次 |
最近记录: |