我使用以下代码在postgresql中创建数据库。
Dim s1 As String s1 = "Provider=MSDASQL;Driver={PostgreSQL ANSI};SERVER=localhost;DATABASE=postgres;UID=username;PWD=pwd;CONNSETTINGS=SET Datestyle TO 'DMY'%3b;BOOLSASCHAR=0;TEXTASLONGVARCHAR=1;TrueIsMinus1=1;" Dim c1 As New ADODB.Connection c1.Open s1 Dim sDB_Name As String sDB_Name = "db1" s1 = "CREATE DATABASE "" " & sDB_Name & """" s1 = s1 & " With OWNER = postgres " s1 = s1 & " TEMPLATE = template0 " s1 = s1 & " ENCODING = 'WIN1252' " s1 = s1 & " TABLESPACE = pg_default " s1 = s1 & " LC_COLLATE = 'English_United States.1252' " s1 = s1 & " LC_CTYPE = 'English_United States.1252' " s1 = s1 & " CONNECTION LIMIT = -1; " c1.Execute s1 MsgBox "done" Unload Me它运作成功,因为使用pgadmin我可以看到这个新数据库并与之交互。
但是当我尝试通过ADO从创建数据库的程序或其他程序使用此数据库时,我收到消息:
运行时错误'-2147467259(80004005)':致命:数据库“db1”不存在
如果我使用pgadmin创建具有所有相同属性的相同数据库,那么我不会收到此消息,并且可以使用外部程序使用数据库。
有人可以解释一下我做错了什么吗? 谢谢。
I used the following code to create a database in postgresql.
Dim s1 As String s1 = "Provider=MSDASQL;Driver={PostgreSQL ANSI};SERVER=localhost;DATABASE=postgres;UID=username;PWD=pwd;CONNSETTINGS=SET Datestyle TO 'DMY'%3b;BOOLSASCHAR=0;TEXTASLONGVARCHAR=1;TrueIsMinus1=1;" Dim c1 As New ADODB.Connection c1.Open s1 Dim sDB_Name As String sDB_Name = "db1" s1 = "CREATE DATABASE "" " & sDB_Name & """" s1 = s1 & " With OWNER = postgres " s1 = s1 & " TEMPLATE = template0 " s1 = s1 & " ENCODING = 'WIN1252' " s1 = s1 & " TABLESPACE = pg_default " s1 = s1 & " LC_COLLATE = 'English_United States.1252' " s1 = s1 & " LC_CTYPE = 'English_United States.1252' " s1 = s1 & " CONNECTION LIMIT = -1; " c1.Execute s1 MsgBox "done" Unload MeIt worked successfully insofar that with using pgadmin I can see and interact with this new database.
But when I try to work with this database via ADO from either the program that created the database or another program, I get the message :
Run-time error '-2147467259 (80004005)': FATAL: database "db1" does not exist
If I create the same database with all the same properties using pgadmin then I don't get this message and can work with the database using external programs.
Can somebody please explain what am I doing wrong? Thank you.
最满意答案
我做错了是因为我正在创建一个名为的数据库
“db1”
而不是
“DB1”
(名称中有一个领先的空间)。
代码中的这一行是错误的:
s1 = "CREATE DATABASE "" " & sDB_Name & """"应该是的
s1 = "CREATE DATABASE """ & sDB_Name & """"What I was doing wrong was that I was creating a database called
" db1"
rather than
"db1"
(there's a leading space in the name).
This line in the code was wrong :
s1 = "CREATE DATABASE "" " & sDB_Name & """"It should have been
s1 = "CREATE DATABASE """ & sDB_Name & """"更多推荐
发布评论