| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 714 人关注过本帖, 1 人收藏
标题:教教我吧,ini写入文件
只看楼主 加入收藏
lvmingaivb
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-8-13
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:7 
教教我吧,ini写入文件
我想写一个SourceDB.ini
在模块中部分代码如下:
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public conn As Connection
Public rs As Recordset
Public DBConnectString As String

Public ServerName As String
Public ServerLogin As String
Public ServerPass As String
Public ServerDbName As String

Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
   GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
 'GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
 GetPrivateProfileString AppName, In_Key, "", GetStr, 256, "E:\lvming\SourceDB.ini"
  GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
   GoTo GetIniStrErr
Else
   GetIniStr = GetStr
   GetStr = ""
End If
Exit Function
GetIniStrErr:
   Err.Clear
   GetIniStr = ""
   GetStr = ""
End Function
'写INI文件
Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
   GoTo WriteIniStrErr
Else
 'WritePrivateProfileString AppName, In_Key, In_Data, App.Path & "\SourceDB.ini"
 WritePrivateProfileString AppName, In_Key, In_Data, "E:\lvming\SourceDB.ini"
End If
Exit Function
WriteIniStrErr:
   Err.Clear
   WriteIniStr = False
End Function

Function Db_open() As Boolean
    On Error GoTo ErrorHandler
    Dim dbstr As String
    Dim ComputerName As String * 256
  

GetComputerName ComputerName, 255

  ' dbstr = "Provider=SQLOLEDB.1;Password=159753;Persist Security Info=True;User ID=sa;Initial Catalog=v_catch;Data Source=test"
   dbstr = "Provider=SQLOLEDB.1;Password=" & ServerPass & ";Persist Security Info=True;User ID=" & ServerLogin & ";Initial Catalog=" & ServerDbName & ";Data Source=" & ServerName & ""

End If

DBConnectString = dbstr
    Set conn = New Connection
    conn.Open dbstr
    If conn.State = 1 Then
        Db_open = True
        Exit Function
    End If
ErrorHandler:
    Db_open = False

End Function
Sub db_close()
    On Error Resume Next
    rs.Close
    Set rs = Nothing
    conn.Close
    Set conn = Nothing

End Sub

Public Sub EndProgram(Frm As Form)
  On Error Resume Next
  Call db_close
  Unload Frm
  End
End Sub

Private Sub Form_Load()
   ServerName = GetIniStr("SQL", "ServerName")
   ServerLogin = GetIniStr("SQL", "Login")
   ServerPass = GetIniStr("SQL", "password")
   ServerDbName = GetIniStr("SQL", "ServerDbName")
...
...

我想在数据备份的form中调用
部分代码如下:
Dim sql, workarea(12) As String
ProgressBar1.Visible = True
ProgressBar1.Min = LBound(workarea)
ProgressBar1.max = UBound(workarea)
ProgressBar1.Value = ProgressBar1.Min
 For connter = LBound(workarea) To UBound(workarea)
       workarea(connter) = "initial value" & connter
       ProgressBar1.Value = connter         
            cnn.Open "provider=sqloledb.1;persist security info = false;data source=test;initial catalog=v_catch;user id=sa;password=159753"   通过模块的使用让这语句不需要
            sql = "backup DATABASE v_catch TO disk='" & Text1.Text & "'"   把v_catch 改成"& serverdbname &"
            Debug.Print sql
            cnn.Execute (sql)     '执行SQL语句
          cnn.Close  通过模块的使用让这语句不需要
  Next connter
  ProgressBar1.Value = ProgressBar1.Min
  MsgBox "数据库备份成功!!", 64, "提示信息"

请问在我的模块中怎么写
搜索更多相关主题的帖子: 文件 ini 
2010-09-14 15:38
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:5 
   ServerName = GetIniStr("SQL", "ServerName")
   ServerLogin = GetIniStr("SQL", "Login")
   ServerPass = GetIniStr("SQL", "password")
   ServerDbName = GetIniStr("SQL", "ServerDbName")

;INI文件内容
[SQL]
ServerName=XXXXXX
Login=XXXXX
password=YYYYYY
ServerDbName=ZZZZZZ
;INI文件结束

方括号中对应的是第一个参数
等号左边对应 第二个参数

-------------------
INI文件结构是
以方括号为一节开头,为一组设置
然后下面就是接着具体的设置名=设置值



授人于鱼,不如授人于渔
早已停用QQ了
2010-09-14 18:00
lvmingaivb
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-8-13
收藏
得分:0 
这个我知道的,我的意思是 ,我在每次打开数据库,关闭数据库时不需要每次都通过:
cnn.Open "provider=sqloledb.1;persist security info = false;data source=test;initial catalog=v_catch;user id=sa;password=159753"   
我想知道 怎么通过模块的使用不写上面这条语句            
sql = "backup DATABASE v_catch TO disk='" & Text1.Text & "'"  
在数据备份过程中 把v_catch 改成"& serverdbname &",能写成:sql = "backup DATABASE "& serverdbname &" to disk='" & Text1.Text & "'"
            Debug.Print sql
            cnn.Execute (sql)     '执行SQL语句
          cnn.Close  通过模块的使用让这语句不需要

2010-09-15 08:49
iamice
Rank: 1
等 级:新手上路
帖 子:3
专家分:9
注 册:2008-3-11
收藏
得分:5 
搞不懂
2010-09-15 09:07
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
CNN在模块文件中定义为 public ,
然后 在程序执行时,首先进行连接,如果连接失败,则程序无法运行,这时应该提示,并退出.
CNN ,在程序运行期间,应该是保持连接的,
不建议每次使用时,重新连接 CNN
程序结束时,再关闭这个连接.

CNN 的连接字串,
应该是用你读出来的内容,然后拼起来的.
你用四条命令,读出了这里面四个关键内容,那么再连接起来就是了.
   ServerName = GetIniStr("SQL", "ServerName")
   ServerLogin = GetIniStr("SQL", "Login")
   ServerPass = GetIniStr("SQL", "password")
   ServerDbName = GetIniStr("SQL", "ServerDbName")

"provider=sqloledb.1;persist security info = false;data source=" & serverdbname & ";initial catalog=v_catch;user id=" & seveerlogin & ";password=" & serverpass

ServerName 没找到是放哪儿的.应该也是连接字符串里的,我对 数据库 不太熟.

----------
你备份时,需要重新连接的话, 开过另一个连接 如 CNN2 就是了.

授人于鱼,不如授人于渔
早已停用QQ了
2010-09-15 11:11
lvmingaivb
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-8-13
收藏
得分:0 
回复 5楼 风吹过b
谢谢你,已经解决了
我在模块里写了个sub main()里面调用DB_open,然后把调用数据库打开的窗体载入就可以了
select * from "& serverdbname &",在写入文件里没显示,是因为,我只是serverdbname = getinistr("","")读出来,没写进去,如果写进去,让serverdbname等于我的数据库,然后在其他窗体中调用就可以用serverdbname了
2010-09-16 08:47
快速回复:教教我吧,ini写入文件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021042 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved