| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 714 人关注过本帖, 1 人收藏
标题:教教我吧,ini写入文件
取消只看楼主 加入收藏
lvmingaivb
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-8-13
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:2 
教教我吧,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
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
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.017689 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved