{"id":179344,"date":"2024-11-09T01:02:18","date_gmt":"2024-11-08T17:02:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/179344\/"},"modified":"2024-11-09T01:02:19","modified_gmt":"2024-11-08T17:02:19","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-vb-%e5%88%a4%e6%96%b7%e6%95%b8%e6%93%9a%e5%ba%ab%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8-vb-%e5%88%a4%e6%96%b7%e6%95%b8%e6%93%9a%e5%ba%ab%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/179344\/","title":{"rendered":"\u5982\u4f55\u7528 VB \u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5b58\u5728 (vb \u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5b58\u5728)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e7%94%a8-vb-%e5%88%a4%e6%96%b7%e6%95%b8%e6%93%9a%e5%ba%ab%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8-viNNLZQrRd\">\u5982\u4f55\u7528 VB \u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5b58\u5728<\/h1>\n<p>\u5728\u958b\u767c\u61c9\u7528\u7a0b\u5e8f\u6642\uff0c\u78ba\u4fdd\u6578\u64da\u5eab\u7684\u5b58\u5728\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f7f\u7528 Visual Basic (VB) \u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u80fd\u5920\u6709\u6548\u5730\u6aa2\u67e5\u6578\u64da\u5eab\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u5728\u9023\u63a5\u6216\u57f7\u884c\u67e5\u8a62\u6642\u51fa\u73fe\u932f\u8aa4\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 VB \u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5b58\u5728\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e5%ad%98%e5%9c%a8%e6%80%a7%e6%aa%a2%e6%9f%a5%e7%9a%84%e5%bf%85%e8%a6%81%e6%80%a7-viNNLZQrRd\">\u6578\u64da\u5eab\u5b58\u5728\u6027\u6aa2\u67e5\u7684\u5fc5\u8981\u6027<\/h2>\n<p>\u5728\u8a31\u591a\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6578\u64da\u5eab\u662f\u5b58\u5132\u548c\u7ba1\u7406\u6578\u64da\u7684\u6838\u5fc3\u7d44\u4ef6\u3002\u5982\u679c\u6578\u64da\u5eab\u4e0d\u5b58\u5728\uff0c\u61c9\u7528\u7a0b\u5e8f\u5c07\u7121\u6cd5\u6b63\u5e38\u904b\u884c\uff0c\u9019\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u4e1f\u5931\u6216\u61c9\u7528\u5d29\u6f70\u3002\u56e0\u6b64\uff0c\u5728\u9032\u884c\u4efb\u4f55\u6578\u64da\u5eab\u64cd\u4f5c\u4e4b\u524d\uff0c\u6aa2\u67e5\u6578\u64da\u5eab\u7684\u5b58\u5728\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-vb-%e5%88%a4%e6%96%b7%e6%95%b8%e6%93%9a%e5%ba%ab%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8-viNNLZQrRd\">\u4f7f\u7528 VB \u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5b58\u5728<\/h2>\n<p>\u5728 VB \u4e2d\uff0c\u53ef\u4ee5\u901a\u904e\u591a\u7a2e\u65b9\u5f0f\u4f86\u6aa2\u67e5\u6578\u64da\u5eab\u7684\u5b58\u5728\u6027\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 ADO.NET \u548c SQL Server \u7684\u4e00\u500b\u5e38\u898b\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"%e6%96%b9%e6%b3%95%e4%b8%80%ef%bc%9a%e4%bd%bf%e7%94%a8-ado-net-viNNLZQrRd\">\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528 ADO.NET<\/h3>\n<pre><code>Imports System.Data.SqlClient\n\nModule Module1\n    Sub Main()\n        Dim connectionString As String = \"Data Source=\u4f60\u7684\u4f3a\u670d\u5668;Initial Catalog=\u4f60\u7684\u6578\u64da\u5eab;Integrated Security=True\"\n        Dim dbName As String = \"\u4f60\u7684\u6578\u64da\u5eab\"\n\n        If DatabaseExists(connectionString, dbName) Then\n            Console.WriteLine(\"\u6578\u64da\u5eab\u5b58\u5728\u3002\")\n        Else\n            Console.WriteLine(\"\u6578\u64da\u5eab\u4e0d\u5b58\u5728\u3002\")\n        End If\n    End Sub\n\n    Function DatabaseExists(connectionString As String, dbName As String) As Boolean\n        Dim exists As Boolean = False\n        Using connection As New SqlConnection(\"Data Source=\u4f60\u7684\u4f3a\u670d\u5668;Integrated Security=True\")\n            connection.Open()\n            Dim command As New SqlCommand(\"SELECT database_id FROM sys.databases WHERE name = @dbName\", connection)\n            command.Parameters.AddWithValue(\"@dbName\", dbName)\n\n            Dim result = command.ExecuteScalar()\n            If result IsNot Nothing Then\n                exists = True\n            End If\n        End Using\n        Return exists\n    End Function\nEnd Module<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5efa\u7acb\u4e86\u4e00\u500b\u9023\u63a5\u5b57\u4e32\uff0c\u7136\u5f8c\u4f7f\u7528 SQL \u67e5\u8a62\u4f86\u6aa2\u67e5\u6307\u5b9a\u7684\u6578\u64da\u5eab\u662f\u5426\u5b58\u5728\u3002\u9019\u7a2e\u65b9\u6cd5\u7c21\u55ae\u4e14\u6709\u6548\uff0c\u9069\u5408\u7528\u65bc\u5927\u591a\u6578\u60c5\u6cc1\u3002<\/p>\n<h3 id=\"%e6%96%b9%e6%b3%95%e4%ba%8c%ef%bc%9a%e4%bd%bf%e7%94%a8-ole-db-viNNLZQrRd\">\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528 OLE DB<\/h3>\n<p>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u5176\u4ed6\u985e\u578b\u7684\u6578\u64da\u5eab\uff08\u4f8b\u5982 Access\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 OLE DB \u4f86\u6aa2\u67e5\u6578\u64da\u5eab\u7684\u5b58\u5728\u6027\u3002\u4ee5\u4e0b\u662f\u76f8\u61c9\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>Imports System.Data.OleDb\n\nModule Module1\n    Sub Main()\n        Dim connectionString As String = \"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\u4f60\u7684\u6578\u64da\u5eab.accdb;\"\n        \n        If DatabaseExists(connectionString) Then\n            Console.WriteLine(\"\u6578\u64da\u5eab\u5b58\u5728\u3002\")\n        Else\n            Console.WriteLine(\"\u6578\u64da\u5eab\u4e0d\u5b58\u5728\u3002\")\n        End If\n    End Sub\n\n    Function DatabaseExists(connectionString As String) As Boolean\n        Try\n            Using connection As New OleDbConnection(connectionString)\n                connection.Open()\n                Return True\n            End Using\n        Catch ex As Exception\n            Return False\n        End Try\n    End Function\nEnd Module<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5617\u8a66\u6253\u958b\u4e00\u500b OLE DB \u9023\u63a5\u3002\u5982\u679c\u9023\u63a5\u6210\u529f\uff0c\u5247\u6578\u64da\u5eab\u5b58\u5728\uff1b\u5982\u679c\u767c\u751f\u7570\u5e38\uff0c\u5247\u6578\u64da\u5eab\u4e0d\u5b58\u5728\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-viNNLZQrRd\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u4f7f\u7528 VB \u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u6aa2\u67e5\u6578\u64da\u5eab\u7684\u5b58\u5728\u6027\u662f\u78ba\u4fdd\u61c9\u7528\u7a0b\u5e8f\u7a69\u5b9a\u6027\u7684\u91cd\u8981\u6b65\u9a5f\u3002\u901a\u904e\u4f7f\u7528 ADO.NET \u6216 OLE DB\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5b58\u5728\uff0c\u5f9e\u800c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u932f\u8aa4\u548c\u6578\u64da\u4e1f\u5931\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u6258\u7ba1\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u4f01\u696d\uff0c\u60a8\u90fd\u53ef\u4ee5\u627e\u5230\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 VB \u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5b58\u5728\uff0c\u638c\u63e1\u6709\u6548\u7684\u6aa2\u67e5\u65b9\u6cd5\uff0c\u78ba\u4fdd\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u8207\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-179344","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/179344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=179344"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/179344\/revisions"}],"predecessor-version":[{"id":179345,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/179344\/revisions\/179345"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=179344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=179344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=179344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}