(初学者)编译时提示query1与database1可能没有初始化,但编译成功,但执行出错,请高手指点是数据库没有连接成功还是insert 语句错误!(100分

  • 主题发起人 主题发起人 nfsong
  • 开始时间 开始时间
N

nfsong

Unregistered / Unconfirmed
GUEST, unregistred user!
(初学者)编译时提示query1与database1可能没有初始化,但编译成功,但执行出错,请高手指点是数据库没有连接成功还是insert 语句错误!(100分)<br />procedure TForm1.Button1Click(Sender: TObject);<br>var DBini:Tinifile;<br>var filename:string;<br>var s_mid:string;<br>var<br> &nbsp;Database1:TDataBase;<br> &nbsp;Query1:TQuery;<br><br>//var dbname,zjname,usname,pwdkl:string;<br>begin<br> &nbsp; if w_db.Text&lt;&gt;'' then<br> &nbsp; begin<br> &nbsp; filename:=ExtractFilePath(paramstr(0))+'mydb.ini';<br> &nbsp; &nbsp;DBini:=TInifile.Create(filename);<br> &nbsp; &nbsp;DBini.WriteString('数据库登录','Database',w_db.Text);<br> &nbsp; &nbsp;DBini.WriteString('数据库登录','ServerName',w_host.Text);<br> &nbsp; &nbsp;Dbini.WriteString('数据库登录','LogId',w_use.Text);<br> &nbsp; &nbsp;Dbini.WriteString('数据库登录','LogPassword',w_pwd.Text);<br> &nbsp; &nbsp;Database1.DatabaseName:=dbini.ReadString('数据库登录','Database',w_db.Text);<br> &nbsp; &nbsp;Database1.Name:=w_host.Text ;<br> &nbsp; &nbsp;Database1.DriverName:= 'MSSQL';<br> &nbsp; &nbsp;Database1.LoginPrompt:=False;<br> &nbsp; &nbsp;database1.AliasName:=w_db.Text;<br> &nbsp; &nbsp;query1.SessionName:=database1.SessionName;<br> &nbsp; &nbsp;query1.DatabaseName:=database1.DatabaseName;<br><br> &nbsp; &nbsp;{with Database1.Params do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Add('DATABASE NAME=' + w_db.Text);<br> &nbsp; &nbsp; &nbsp;Add('SERVER NAME=' + w_host.Text); &nbsp; &nbsp; //ServerName也可以是SQL服务器的IP地址<br> &nbsp; &nbsp; &nbsp;Add('USER NAME=' + w_use.Text); &nbsp; &nbsp; &nbsp; &nbsp; //设置MSSQL数据的用户名称 如:sa<br> &nbsp; &nbsp; &nbsp;Add('OPEN MODE=READ/WRITE');<br> &nbsp; &nbsp; &nbsp;ADd('SCHEMA CACHE SIZE=8');<br> &nbsp; &nbsp; &nbsp;Add('BLOB EDIT LOGGING=');<br> &nbsp; &nbsp; &nbsp;Add('LANGDRIVER=');<br> &nbsp; &nbsp; &nbsp;Add('SQLQRYMODE=');<br> &nbsp; &nbsp; &nbsp;Add('SQLPASSTHRU MODE=SHARED AUTOCOMMIT');<br> &nbsp; &nbsp; &nbsp;Add('DATE MODE=0');<br> &nbsp; &nbsp; &nbsp;Add('SCHEMA CACHE TIME=-1');<br> &nbsp; &nbsp; &nbsp;Add('MAX QUERY TIME=300');<br> &nbsp; &nbsp; &nbsp;Add('MAX ROWS=-1');<br> &nbsp; &nbsp; &nbsp;Add('BATCH COUNT=200');<br> &nbsp; &nbsp; &nbsp;Add('ENABLE SCHEMA CACHE=FALSE');<br> &nbsp; &nbsp; &nbsp;Add('SCHEMA CACHE DIR=');<br> &nbsp; &nbsp; &nbsp;Add('HOST NAME=');<br> &nbsp; &nbsp; &nbsp;Add('APPLICATION NAME=');<br> &nbsp; &nbsp; &nbsp;Add('NATIONAL LANG NAME=');<br> &nbsp; &nbsp; &nbsp;Add('ENABLE BCD=FALSE');<br> &nbsp; &nbsp; &nbsp;Add('TDS PACKET SIZE=4096');<br> &nbsp; &nbsp; &nbsp;Add('BLOBS TO CACHE=64');<br> &nbsp; &nbsp; &nbsp;Add('BLOB SIZE=32');<br> &nbsp; &nbsp; &nbsp;Add('PASSWORD=' + w_pwd.Text); &nbsp; &nbsp; &nbsp; &nbsp;//设置MSSQL用户口令;<br> &nbsp; end; }<br> &nbsp; with query1.SQL do<br> &nbsp; begin<br> &nbsp; &nbsp; clear;<br> &nbsp; &nbsp; add('insert into [t_sysreg](cph,zds,mid,sqid,regid,computername');<br> &nbsp; &nbsp; add('values(:cph,:zds,s_mid,:sqid,:regid,:computername');<br> &nbsp; end;<br> &nbsp; with query1 do<br> &nbsp; begin<br> &nbsp; &nbsp; s_mid:=ListBox1.Items.Text;<br> &nbsp; &nbsp; parambyname('cph').AsString:=w_edit1.Text;<br> &nbsp; &nbsp; parambyname('zds').AsInteger:=1;<br> &nbsp; &nbsp; parambyname('mid').AsString:=s_mid;<br> &nbsp; &nbsp; parambyname('sqid').AsString:=w_edit2.Text;<br> &nbsp; &nbsp; parambyname('regid').AsString:=w_edit3.Text;<br> &nbsp; &nbsp; parambyname('computernmae').AsString:=w_edit4.Text;<br> &nbsp; end;<br> &nbsp; database1.StartTransaction;<br><br> &nbsp; try<br> &nbsp; &nbsp; //Database1.Connected := true;<br> &nbsp; &nbsp; //Query1.DatabaseName := w_db.Text;<br> &nbsp; &nbsp; query1.Active:=false;<br> &nbsp; &nbsp; query1.SQL.Clear;<br> &nbsp; &nbsp; //query1.RequestLive:=true;<br> &nbsp; &nbsp; query1.Prepare;<br> &nbsp; &nbsp; //Query1.SQL.Add('insert into t_sysreg(cph,zds,mid,sqid,regid,computername');<br> &nbsp; &nbsp;// Query1.SQL.Add('select w_edit1,1,s_mid,w_edit2,w_edit3,w_edit4');<br> &nbsp; &nbsp; query1.ExecSQL;<br> &nbsp; &nbsp; query1.Open;<br> &nbsp; &nbsp; database1.Commit;<br> &nbsp; Except<br> &nbsp; &nbsp; //Application.MessageBox('数据库连接错误!','错误',MB_OK+MB_ICONERROR);<br> &nbsp; &nbsp; query1.Free;<br> &nbsp; &nbsp; database1.Rollback;<br> &nbsp; &nbsp; //raise;<br><br> &nbsp; end;<br> &nbsp; query1.Free;<br>end;<br>end;
 
Database1, Query1在使用前都还没有创建啊<br>var<br> &nbsp;Database1:TDataBase;<br> &nbsp;Query1:TQuery;<br>begin<br> &nbsp;Database1:= TDatabase.Create(self);<br> &nbsp;Query1:= TQuery1.Create(self)
 
谢谢,成功了,但还存在问题,输入数据去执行时,显示当前数据库是关闭的,估计是insert时显示数据库处于关闭状态,如何解决!
 
--------------------------------<br>query1.ExecSQL;<br> &nbsp; &nbsp; query1.Open;<br>--------------------------------<br>这里重复了吧 对于insert,delete等就用ExecSQL方法,select用open方法
 
project pskreg.exe raised exception class Edatabseerror with message'qwlt(服务器即电脑名称): cannot perform this operation on a close database' process stoped,use step or run continue.<br>这就是经过djrj和300800纠正后报错的显示.编译确实成功了,请高手指点!
 
Database1:= TDatabase.Create(self);<br>Query1:= TQuery1.Create(self);<br>try<br> &nbsp;Query1.sql.add('insert into (...)');<br> &nbsp;Query1.ParamByName(..) :=...<br> &nbsp;.....<br><br> &nbsp;Database1.StartTransaction;<br> &nbsp;try<br> &nbsp; &nbsp;Query1.ExecSQL; &nbsp;// SQL 语句直接执行就行了<br> &nbsp; &nbsp;Database1.Commit;<br> &nbsp;Except<br> &nbsp; &nbsp;Database1.Rollback;<br> &nbsp;end;<br><br>finally<br> &nbsp;Query1.free;<br> &nbsp;Database1.free;<br>end;
 
在这一句database1.AliasName:=w_db.Text;后加上<br>database1.connected:=True;试试
 
多人接受答案了。
 
后退
顶部