function GetConnectionString: string;
var
SYSINI: TINIFile;
ServerName, UserName, Password, InitDB: string;
tmpstr: string;
begin
SYSINI := TIniFile.Create('DB.INI');
try
ServerName := SYSINI.ReadString('Database', 'ServerName', '');
UserName := SYSINI.ReadString('Database', 'UserName', '');
InitDB := SYSINI.ReadString('Database', 'InitDB', '');
tmpstr := SYSINI.ReadString('Database', 'Password', '');
Password := Decrypt(tmpstr, Key);
Result := '';
Result := 'Provider=SQLOLEDB.1;Password=' + Password + ';Persist Security Info=True;User ID=' + UserName + ';Initial Catalog=' + InitDB + ';Data Source=' + ServerName;
finally
SYSINI.Free;
end;
end;procedure SetConnectionString(ServerName, UserName, Password, InitDB: string);
//InitDB :=model
var
SYSINI: TINIFile;
tmpstr: string;
begin
SYSINI := TIniFile.Create('DB.INI');
try
with SYSINI do
begin
WriteString('Database', 'ServerName', ServerName);
WriteString('Database', 'UserName', UserName);
WriteString('Database', 'InitDB', InitDB);
tmpstr := Encrypt(Password, Key);
WriteString('Database', 'Password', tmpstr);
end;
finally
SYSINI.Free;
end;
end;上面的函数和过程是用生成ini文件态连接sql数据库的
小弟有了这些还是不太会写,哪位大哥能讲细一点呀,
这些代码怎么用?
var
SYSINI: TINIFile;
ServerName, UserName, Password, InitDB: string;
tmpstr: string;
begin
SYSINI := TIniFile.Create('DB.INI');
try
ServerName := SYSINI.ReadString('Database', 'ServerName', '');
UserName := SYSINI.ReadString('Database', 'UserName', '');
InitDB := SYSINI.ReadString('Database', 'InitDB', '');
tmpstr := SYSINI.ReadString('Database', 'Password', '');
Password := Decrypt(tmpstr, Key);
Result := '';
Result := 'Provider=SQLOLEDB.1;Password=' + Password + ';Persist Security Info=True;User ID=' + UserName + ';Initial Catalog=' + InitDB + ';Data Source=' + ServerName;
finally
SYSINI.Free;
end;
end;procedure SetConnectionString(ServerName, UserName, Password, InitDB: string);
//InitDB :=model
var
SYSINI: TINIFile;
tmpstr: string;
begin
SYSINI := TIniFile.Create('DB.INI');
try
with SYSINI do
begin
WriteString('Database', 'ServerName', ServerName);
WriteString('Database', 'UserName', UserName);
WriteString('Database', 'InitDB', InitDB);
tmpstr := Encrypt(Password, Key);
WriteString('Database', 'Password', tmpstr);
end;
finally
SYSINI.Free;
end;
end;上面的函数和过程是用生成ini文件态连接sql数据库的
小弟有了这些还是不太会写,哪位大哥能讲细一点呀,
这些代码怎么用?
function GetConnectionString: string;
procedure SetConnectionString(ServerName, UserName, Password, InitDB: string);GetConnectionString用于获取str
SetConnectionString用于根据ServerName, UserName, Password, InitDB保存到db.ini别忘记建立个文件'DB.INI'
一个配置服务器窗体serverform {server.pas}
这个窗体里有1个button 4个edit谁能帮忙写一下主要的代码呀?
var
ServerName, UserName, Password, InitDB: string;
begin
ServerName := Edit1.Text;
UserName := Edit2.Text;
Password := Edit3.Text;
InitDB := Edit4.Text;
SetConnectionString(ServerName, UserName, Password, InitDB);
ADOQuery1.ConnectionString := GetConnectionString;
...
end;
procedure SetConnectionString(ServerName, UserName, Password, InitDB: string);
//InitDB :=model
var
SYSINI: TINIFile;
tmpstr: string;
begin
//创建在程序运行位置下INI文件,语句可完成连接DB。INI和当没有文件的时候创建文件
SYSINI := TIniFile.Create('DB.INI');
try
with SYSINI do
begin
//WRITESTRING是Tinifile类的方法,是向INI文件写入数据用的
WriteString('Database', 'ServerName', ServerName);
WriteString('Database', 'UserName', UserName);
WriteString('Database', 'InitDB', InitDB);
tmpstr := Encrypt(Password, Key);//应该是一个加密函数Encrypt
WriteString('Database', 'Password', tmpstr);
end;
finally
SYSINI.Free;//释放创建的SYSINI
end;
end;INI格式为以一个个写入为例子
WriteString('Database', 'ServerName', ServerName);INI中是这样的
[Database]
ServerName=上个句子中最后的变量的值
SYSINI.ReadString('Database', 'ServerName', '');
同上,READSTRING就是把上面我写的读出来,如果在INI中没有值或者还没有创建出这个节点则返回一个空字符串,
[Database]
ServerName=上个句子中最后的变量的值
把密码放在ini文件中是很不安全的,虽然方便。
var
myini:tinifile;
edser,eduser,edpass,edata,path:string;
begin
form1.Top:=variant((screen.height-form1.Height)/2);
form1.Left:=variant((screen.width-form1.Width)/2);
path:=extractfiledir(application.ExeName)+'\appdbcfg.ini';
myini:=tinifile.Create(path);
edser:=myini.ReadString('data','server','');
eduser:=myini.ReadString('data','user','');
edpass:=myini.ReadString('data','password','');
edata:=myini.ReadString('data','Database','');
query1.DatabaseName := edata;
Database1.Close;
Database1.DriverName := 'MSSQL';
Database1.DatabaseName := edata;
Database1.Params.Add('server name=' + Edser);
Database1.Params.Add('Database Name=' + edata);
Database1.Params.Add('user name=' + eduser);
Database1.Params.Add('password=' + edpass);
try
Database1.Open;
except
application.MessageBox('连接sql server数据库失败!请核对appdbcfg.ini文件', '错误', 0);
application.Terminate;
close;
end;
SeSkinComboBox1.SetFocus;
这么连接也可以的。
我弄成了下面这样 还是连不上呀?procedure Tsetform.FormShow(Sender: TObject);
var
myinifile:tinifile;
servername,username,pwd,initdb:string;
begin
myinifile:=tinifile.Create('server.ini');
servername:=myinifile.ReadString('login','servername','');
username:=myinifile.ReadString('login','username','');
pwd:=myinifile.ReadString('login','pwd','');
initdb:='mingshi'; //这是我想连的数据库
try DataModule1.ms.ConnectionString:='Provider=SQLOLEDB.1;Password='+pwd+';Persist Security Info=True;User ID='+username+';Initial Catalog='+initdb+';Data Source='+servername+'';
DataModule1.ms.open;
except
application.MessageBox('连接sql server数据库失败!请核对server.ini文件', '错误', 0);
application.Terminate;
close;
end;
end;
总是出来缺省值
这是怎么回事呀
这贴子老不结也不行呀