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> Database1:TDataBase;<br> Query1:TQuery;<br><br>//var dbname,zjname,usname,pwdkl:string;<br>begin<br> if w_db.Text<>'' then<br> begin<br> filename:=ExtractFilePath(paramstr(0))+'mydb.ini';<br> DBini:=TInifile.Create(filename);<br> DBini.WriteString('数据库登录','Database',w_db.Text);<br> DBini.WriteString('数据库登录','ServerName',w_host.Text);<br> Dbini.WriteString('数据库登录','LogId',w_use.Text);<br> Dbini.WriteString('数据库登录','LogPassword',w_pwd.Text);<br> Database1.DatabaseName:=dbini.ReadString('数据库登录','Database',w_db.Text);<br> Database1.Name:=w_host.Text ;<br> Database1.DriverName:= 'MSSQL';<br> Database1.LoginPrompt:=False;<br> database1.AliasName:=w_db.Text;<br> query1.SessionName:=database1.SessionName;<br> query1.DatabaseName:=database1.DatabaseName;<br><br> {with Database1.Params do<br> begin<br> Add('DATABASE NAME=' + w_db.Text);<br> Add('SERVER NAME=' + w_host.Text); //ServerName也可以是SQL服务器的IP地址<br> Add('USER NAME=' + w_use.Text); //设置MSSQL数据的用户名称 如:sa<br> Add('OPEN MODE=READ/WRITE');<br> ADd('SCHEMA CACHE SIZE=8');<br> Add('BLOB EDIT LOGGING=');<br> Add('LANGDRIVER=');<br> Add('SQLQRYMODE=');<br> Add('SQLPASSTHRU MODE=SHARED AUTOCOMMIT');<br> Add('DATE MODE=0');<br> Add('SCHEMA CACHE TIME=-1');<br> Add('MAX QUERY TIME=300');<br> Add('MAX ROWS=-1');<br> Add('BATCH COUNT=200');<br> Add('ENABLE SCHEMA CACHE=FALSE');<br> Add('SCHEMA CACHE DIR=');<br> Add('HOST NAME=');<br> Add('APPLICATION NAME=');<br> Add('NATIONAL LANG NAME=');<br> Add('ENABLE BCD=FALSE');<br> Add('TDS PACKET SIZE=4096');<br> Add('BLOBS TO CACHE=64');<br> Add('BLOB SIZE=32');<br> Add('PASSWORD=' + w_pwd.Text); //设置MSSQL用户口令;<br> end; }<br> with query1.SQL do<br> begin<br> clear;<br> add('insert into [t_sysreg](cph,zds,mid,sqid,regid,computername');<br> add('valuescph,:zds,s_mid,:sqid,:regid,:computername');<br> end;<br> with query1 do<br> begin<br> s_mid:=ListBox1.Items.Text;<br> parambyname('cph').AsString:=w_edit1.Text;<br> parambyname('zds').AsInteger:=1;<br> parambyname('mid').AsString:=s_mid;<br> parambyname('sqid').AsString:=w_edit2.Text;<br> parambyname('regid').AsString:=w_edit3.Text;<br> parambyname('computernmae').AsString:=w_edit4.Text;<br> end;<br> database1.StartTransaction;<br><br> try<br> //Database1.Connected := true;<br> //Query1.DatabaseName := w_db.Text;<br> query1.Active:=false;<br> query1.SQL.Clear;<br> //query1.RequestLive:=true;<br> query1.Prepare;<br> //Query1.SQL.Add('insert into t_sysreg(cph,zds,mid,sqid,regid,computername');<br> // Query1.SQL.Add('select w_edit1,1,s_mid,w_edit2,w_edit3,w_edit4');<br> query1.ExecSQL;<br> query1.Open;<br> database1.Commit;<br> Except<br> //Application.MessageBox('数据库连接错误!','错误',MB_OK+MB_ICONERROR);<br> query1.Free;<br> database1.Rollback;<br> //raise;<br><br> end;<br> query1.Free;<br>end;<br>end;