Advertisement
Guest User

Untitled

a guest
Oct 20th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.52 KB | None | 0 0
  1. module test;
  2. import tds;
  3.  
  4. import core.sys.posix.unistd;
  5. import std.exception;
  6. import std.stdio;
  7. alias writeln println;
  8.  
  9. import core.stdc.stdio;
  10.  
  11. import std.conv;
  12.  
  13. alias toString = to!(string);
  14.  
  15. enum UID =       "";
  16. enum PWD =       "";
  17. enum PROGNAME=  "";
  18. enum DBSERVER=  "";
  19. enum DBNAME=    "";
  20.  
  21. int main ()
  22. {
  23.   LOGINREC* login;
  24.   DBPROCESS* dbconn;
  25.   char[256] hostname;
  26.   int max_len = 256;
  27.   DBCHAR[255] username;
  28.   DBCHAR[255] first_name;
  29.   DBCHAR[255] last_name;
  30.  
  31.   /* Init the DB library */
  32.   if (dbinit() == FAIL) {
  33.     println("Could not init db");
  34.     return 1;
  35.   }
  36.  
  37.   /* Allocate a login params structure */
  38.   if ((login = dblogin()) == null) {
  39.     println("Could not initialize dblogin() structure");
  40.     return 2;
  41.   }
  42.  
  43.   /* Initialize the login params in the structure */
  44.   dbsetluser(login, UID);
  45.   dbsetlpwd(login, PWD);
  46.   dbsetlapp(login, PROGNAME);
  47.   if (gethostname(hostname.ptr, hostname.length) == 0)
  48.     dbsetlhost(login, hostname.assumeUnique);
  49.  
  50.   /* Now connect to the DB Server */
  51.   if ((dbconn = dbopen(login, DBSERVER)) == null) {
  52.     println("Could not connect to DB Server: ", DBSERVER.toString);
  53.     return 3;
  54.   }
  55.  
  56.   /* Now switch to the correct database */
  57.   if ((dbuse(dbconn, DBNAME)) == FAIL) {
  58.     writefln("Could not switch to database %s on DB Server %s", DBNAME.toString, DBSERVER.toString);
  59.     return 4;
  60.   }
  61.  
  62.   /* You can free the login structure now, as it is no longer needed after logging in */
  63.   dbloginfree(login);
  64.  
  65.   /* Now prepare a SQL statement */
  66.   dbcmd(dbconn, "SELECT username, first_name, last_name FROM admin_users WITH (NOLOCK)".ptr);
  67.   /* Can use dbfcmd instead, if you want to format the SQL
  68.      E.g.
  69.      dbfcmd(dbconn, "SELECT id FROM Customer WHERE company_name='%s' AND avg_income > %f",
  70.             company_name, avg_income);
  71.   */
  72.  
  73.   /* Now execute the SQL statement */
  74.   if (dbsqlexec(dbconn) == FAIL) {
  75.     println("Could not execute the sql statement");
  76.     return 5;
  77.   }
  78.   dbresults(dbconn); /* Very important to call this! :) */
  79.  
  80.   /* Now bind the returned columns to the variables */
  81.   dbbind(dbconn, 1, NTBSTRINGBIND, 0, cast(BYTE*)&username);
  82.   dbbind(dbconn, 2, NTBSTRINGBIND, 0, cast(BYTE*)&first_name);
  83.   dbbind(dbconn, 3, NTBSTRINGBIND, 0, cast(BYTE*)&last_name);
  84.  
  85.   /* Loop thru the result set */
  86.   while (dbnextrow(dbconn) != NO_MORE_ROWS) {
  87.     /* print out the data */
  88.     writefln("%s %s %s", username, first_name, last_name);
  89.   }
  90.  
  91.   /* Clean up*/
  92.   dbfreebuf(dbconn);
  93.   dbclose(dbconn);
  94.   dbexit();
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement