Sunday, April 14, 2013

Quickbase API On .NET (Using C#)


What is QuickBase?
QuickBase online database software lets you easily create a web database software application for your business.
QuickBase API
QuickBase provides rich set of API to access QuickBase application over the web.Using the QuickBase API, you can:
Provide secure access to QuickBase and QuickBase applications.
Manage access through the QuickBase roles feature.
Automate the application life cycle: creation, deletion, and copying (cloning) of applications.
Create, modify, and delete the application’s tables and fields
Add, update, delete, and querying table records

Following is C# code to Download data from QB table into excel file.

public void Main()
{
string username = "QB UserName";
string password = "QB Password";
string ticket = QuickbaseApiWrapper.Authenticate(username, password);
string dirpath = <>;
string filename = <>;
DataTable dt ;
dt = QuickbaseApiWrapper.DoQuery(ticket, "bfxs6dzk2", filename, "1.2.3.4.5.6.7.8.9 ", dirpath);
}  
public static class Apis
{
            public const string Authenticate = "API_Authenticate";
            public const string DoQuery = "API_DoQuery";
            public const string GetDbInfo = "API_GetDBInfo";
            public const string GenResultsTable = "API_GenResultsTable";
            public const string API_EditRecord = "API_EditRecord";
            public const string API_FindDBByName = "API_FindDBByName";
            public const string API_DoQueryCount = "API_DoQueryCount";
            public const string API_AddRecord = "API_AddRecord";
            public const string API_DeleteRecord = "API_DeleteRecord";
            public const string API_CreateTable = "API_CreateTable";
            public const string API_AddField = "API_AddField";


}
        public struct QuickbaseDbInfo
        {
            public string dbname;
            public DateTime lastRecModTime;
            public DateTime lastModifiedTime;
            public DateTime createdTime;
            public long numRecords;
            public string mgrID;
            public string mgrName;
            public string version;

        }
        public static class QuickbaseApiWrapper
        {
            const string dbId1 = "QB DB ID";
            const string queryId = "1";
            private static string CreateRootDoc(string apiName, StringDictionary parameters)
            {
                XmlDocument doc = new XmlDocument();
                XmlElement root = doc.CreateElement("qdbapi");

                if (parameters != null)
                    foreach (string field in parameters.Keys)
                    {

                        XmlElement child = doc.CreateElement(field);
                        child.InnerText = parameters[field];
                        root.AppendChild(child);
                    }
                doc.AppendChild(root);
                StringWriter sw = new StringWriter();
                XmlTextWriter xw = new XmlTextWriter(sw);
                doc.WriteTo(xw);

                return sw.ToString();
            }


            private static System.Data.DataSet GetQueryResults(string url, string action, StringDictionary parameters)
            {

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Method = "POST";
                webRequest.ContentType = "application/xml";
                webRequest.Headers.Add("QUICKBASE-ACTION", action);
                string input = CreateRootDoc(action, parameters);
                byte[] postBytes = Encoding.ASCII.GetBytes(input);
                webRequest.ContentLength = postBytes.Length;
                Stream postStream = webRequest.GetRequestStream();
                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Close();
                WebResponse response = webRequest.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());

                XmlTextReader xmlReader = new XmlTextReader(reader);
                StringDictionary toGo = new StringDictionary();
                System.Data.DataSet ds = new System.Data.DataSet();
                ds.ReadXml(xmlReader, System.Data.XmlReadMode.Auto);

                return ds;
            }

            private static void GenResultsTable(string dbIdParam, string action, StringDictionary parameters, string filename, string DirPath)
            {
                string url = "https://<>.quickbase.com/";
                if (dbIdParam != null)
                    url += "db/" + dbIdParam;
                url += "?act=" + action;
                foreach (string s in parameters.Keys)
                {
                    url += "&" + s + "=" + parameters[s];
                }

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

                webRequest.Method = "POST";
                webRequest.ContentType = "application/xml";
                webRequest.Headers.Add("QUICKBASE-ACTION", action);
                string input = CreateRootDoc(action, parameters);
                byte[] postBytes = Encoding.ASCII.GetBytes(input);
                webRequest.ContentLength = postBytes.Length;
                Stream postStream = webRequest.GetRequestStream();
                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Close();
                WebResponse response = webRequest.GetResponse();
             
                if (File.Exists(DirPath + filename + ".csv"))
                {
                    File.Delete(DirPath + filename + ".csv");
                }
                if (File.Exists(DirPath + filename + ".csv"))
                {
                    File.Delete(DirPath + filename + ".csv");
                }

            }
            public static void CopyStream(Stream input, Stream output)
            {
                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    output.Write(buffer, 0, len);
                }
            }
            public static string Authenticate(string username, string password)
            {
                StringDictionary parameters = new StringDictionary();
                parameters.Add("username", username);
                parameters.Add("password", password);
                string url = "https://<>.quickbase.com/db/main?act=" + Apis.Authenticate;

                foreach (string s in parameters.Keys)
                {
                    url += "&" + s + "=" + parameters[s];
                }

                DataSet ds = GetQueryResults(url, Apis.Authenticate, parameters);

                string errorMessage = ds.Tables[0].Rows[0]["errtext"].ToString();
                string errorCode = ds.Tables[0].Rows[0]["errcode"].ToString();
                if (errorCode != "0")
                    throw new Exception("Quickbase login failed: " + errorMessage + ", code:" + errorCode);
                else
                    return ds.Tables[0].Rows[0]["ticket"].ToString();
            }

            public static DataTable DoQuery(string ticket, string dbId, string filename, string clist, string DirPath)
            {
                StringDictionary parameters = new StringDictionary();
                parameters.Add("dbid", dbId);
                parameters.Add("apptoken", "Your DB AppToken");
                parameters.Add("ticket", ticket);
                parameters.Add("options", "csv");
                parameters.Add("clist", clist);
                GenResultsTable(dbId, Apis.GenResultsTable, parameters, filename, DirPath);
                return new DataTable();
            }
        }


Above code download file table from QB and palce on the file system at<> in excel file.

2 comments:

  1. 1xbet € 200 € Bonus & €200 Promo Codes - Riders Casino
    1xbet is 1xbet 먹튀 a new casino titanium blue that titan metal offers a wide variety of games, all titanium frames with gr5 titanium the most popular games available for free.

    ReplyDelete
  2. Programming Stuff: Quickbase Api On .Net (Using C) >>>>> Download Now

    >>>>> Download Full

    Programming Stuff: Quickbase Api On .Net (Using C) >>>>> Download LINK

    >>>>> Download Now

    Programming Stuff: Quickbase Api On .Net (Using C) >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete