Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module test;
- import tds;
- import core.sys.posix.unistd;
- import std.exception;
- import std.stdio;
- alias writeln println;
- import core.stdc.stdio;
- import std.conv;
- alias toString = to!(string);
- enum UID = "";
- enum PWD = "";
- enum PROGNAME= "";
- enum DBSERVER= "";
- enum DBNAME= "";
- int main ()
- {
- LOGINREC* login;
- DBPROCESS* dbconn;
- char[256] hostname;
- int max_len = 256;
- DBCHAR[255] username;
- DBCHAR[255] first_name;
- DBCHAR[255] last_name;
- /* Init the DB library */
- if (dbinit() == FAIL) {
- println("Could not init db");
- return 1;
- }
- /* Allocate a login params structure */
- if ((login = dblogin()) == null) {
- println("Could not initialize dblogin() structure");
- return 2;
- }
- /* Initialize the login params in the structure */
- dbsetluser(login, UID);
- dbsetlpwd(login, PWD);
- dbsetlapp(login, PROGNAME);
- if (gethostname(hostname.ptr, hostname.length) == 0)
- dbsetlhost(login, hostname.assumeUnique);
- /* Now connect to the DB Server */
- if ((dbconn = dbopen(login, DBSERVER)) == null) {
- println("Could not connect to DB Server: ", DBSERVER.toString);
- return 3;
- }
- /* Now switch to the correct database */
- if ((dbuse(dbconn, DBNAME)) == FAIL) {
- writefln("Could not switch to database %s on DB Server %s", DBNAME.toString, DBSERVER.toString);
- return 4;
- }
- /* You can free the login structure now, as it is no longer needed after logging in */
- dbloginfree(login);
- /* Now prepare a SQL statement */
- dbcmd(dbconn, "SELECT username, first_name, last_name FROM admin_users WITH (NOLOCK)".ptr);
- /* Can use dbfcmd instead, if you want to format the SQL
- E.g.
- dbfcmd(dbconn, "SELECT id FROM Customer WHERE company_name='%s' AND avg_income > %f",
- company_name, avg_income);
- */
- /* Now execute the SQL statement */
- if (dbsqlexec(dbconn) == FAIL) {
- println("Could not execute the sql statement");
- return 5;
- }
- dbresults(dbconn); /* Very important to call this! :) */
- /* Now bind the returned columns to the variables */
- dbbind(dbconn, 1, NTBSTRINGBIND, 0, cast(BYTE*)&username);
- dbbind(dbconn, 2, NTBSTRINGBIND, 0, cast(BYTE*)&first_name);
- dbbind(dbconn, 3, NTBSTRINGBIND, 0, cast(BYTE*)&last_name);
- /* Loop thru the result set */
- while (dbnextrow(dbconn) != NO_MORE_ROWS) {
- /* print out the data */
- writefln("%s %s %s", username, first_name, last_name);
- }
- /* Clean up*/
- dbfreebuf(dbconn);
- dbclose(dbconn);
- dbexit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement