Guest User

Untitled

a guest
Aug 3rd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. Check for System DSN and create System DSN if NOT Existing (iSeries Access ODBC Driver)
  2. using System;
  3. using System.Runtime.InteropServices;
  4.  
  5. public class ODBC_Manager
  6. {
  7. [DllImport("ODBCCP32.dll")]
  8. public static extern bool SQLConfigDataSource(IntPtr parent, int request, string driver, string attributes);
  9.  
  10. [DllImport("ODBCCP32.dll")]
  11. public static extern int SQLGetPrivateProfileString(string lpszSection, string lpszEntry, string lpszDefault, string @RetBuffer, int cbRetBuffer, string lpszFilename);
  12.  
  13. private const short ODBC_ADD_DSN = 1;
  14. private const short ODBC_CONFIG_DSN = 2;
  15. private const short ODBC_REMOVE_DSN = 3;
  16. private const short ODBC_ADD_SYS_DSN = 4;
  17. private const short ODBC_CONFIG_SYS_DSN = 5;
  18. private const short ODBC_REMOVE_SYS_DSN = 6;
  19. private const int vbAPINull = 0;
  20.  
  21. public void CreateDSN(string strDSNName)
  22. {
  23. string strDriver;
  24. string strAttributes;
  25.  
  26. try
  27. {
  28. string strDSN = "";
  29.  
  30. string _server = //ip address of the server
  31. string _user = //username
  32. string _pass = //password
  33. string _description = //not required. give a description if you want to
  34.  
  35.  
  36. strDriver = "iSeries Access ODBC Driver";
  37.  
  38. strAttributes = "DSN=" + strDSNName + "";
  39. strAttributes += "SYSTEM=" + _server + "";
  40. strAttributes += "UID=" + _user + "";
  41. strAttributes += "PWD=" + _pass + "";
  42.  
  43. strDSN = strDSN + "System = " + _server + "n";
  44. strDSN = strDSN + "Description = " + _description + "n";
  45.  
  46. if (SQLConfigDataSource((IntPtr)vbAPINull, ODBC_ADD_SYS_DSN, strDriver, strAttributes))
  47. {
  48. Console.WriteLine("DSN was created successfully");
  49. }
  50. else
  51. {
  52. Console.WriteLine("DSN creation failed...");
  53. }
  54. }
  55. catch (Exception ex)
  56. {
  57. if (ex.InnerException != null)
  58. {
  59. Console.WriteLine(ex.InnerException.ToString());
  60. }
  61. else
  62. {
  63. Console.WriteLine(ex.Message.ToString());
  64. }
  65. }
  66. }
  67.  
  68. public int CheckForDSN(string strDSNName)
  69. {
  70. int iData;
  71. string strRetBuff = "";
  72. iData = SQLGetPrivateProfileString("ODBC Data Sources", strDSNName, "", strRetBuff, 200, "odbc.ini");
  73. return iData;
  74. }
  75. }
  76.  
  77. static void Main(string[] args)
  78. {
  79. ODBC_Manager odbc = new ODBC_Manager();
  80. string dsnName = //Name of the DSN connection here
  81.  
  82. if (odbc.CheckForDSN(dsnName) > 0)
  83. {
  84. Console.WriteLine("nnODBC Connection " + dsnName + " already exists on the system");
  85. }
  86. else
  87. {
  88. Console.WriteLine("nnODBC Connection " + dsnName + " does not exist on the system");
  89. Console.WriteLine("nnPress 'Y' to create the connection?");
  90.  
  91. string cont = Console.ReadLine();
  92. if (cont == "Y" || cont == "y")
  93. {
  94. odbc.CreateDSN(dsnName);
  95. Environment.Exit(1);
  96. }
  97. else
  98. {
  99. Environment.Exit(1);
  100. }
  101. }
  102. }
Add Comment
Please, Sign In to add comment