Guest User

Untitled

a guest
Jul 18th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. <?xml version = "1.0" encoding = "utf-8"?>
  2. <resources>
  3. <string name = "HelloXamarin">Server</string>
  4. <string name = "ApplicationName">connect</string>
  5. <string name = "app_name">connect</string>
  6. <string name = "ButtonClick">Connect</string>
  7. <string name = "username">Username</string>
  8. <string name = "database">Database</string>
  9. <string name = "password">Password</string>
  10. </resources>
  11.  
  12. <?xml version = "1.0" encoding = "utf-8"?>
  13. <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  14. android:orientation = "vertical"
  15. android:background = "#d3d3d3"
  16. android:layout_width = "fill_parent"
  17. android:layout_height = "fill_parent">
  18. <TextView
  19. android:text = "@string/HelloXamarin"
  20. android:textAppearance = "?android:attr/textAppearanceLarge"
  21. android:layout_width = "match_parent"
  22. android:layout_height = "wrap_content"
  23. android:id = "@+id/textView2"
  24. android:textColor = "@android:color/black" />
  25. <EditText
  26. android:id="@+id/plain_text_input"
  27. android:layout_height="wrap_content"
  28. android:layout_width="match_parent"
  29. android:inputType="text"/>
  30. <TextView
  31. android:text = "@string/database"
  32. android:textAppearance = "?android:attr/textAppearanceLarge"
  33. android:layout_width = "match_parent"
  34. android:layout_height = "wrap_content"
  35. android:id = "@+id/database"
  36. android:textColor = "@android:color/black" />
  37. <EditText
  38. android:id="@+id/database_text_input"
  39. android:layout_height="wrap_content"
  40. android:layout_width="match_parent"
  41. android:inputType="text"/>
  42. <TextView
  43. android:text = "@string/username"
  44. android:textAppearance = "?android:attr/textAppearanceLarge"
  45. android:layout_width = "match_parent"
  46. android:layout_height = "wrap_content"
  47. android:id = "@+id/username"
  48. android:textColor = "@android:color/black" />
  49. <EditText
  50. android:id="@+id/username_text_input"
  51. android:layout_height="wrap_content"
  52. android:layout_width="match_parent"
  53. android:inputType="text"/>
  54. <TextView
  55. android:text = "@string/password"
  56. android:textAppearance = "?android:attr/textAppearanceLarge"
  57. android:layout_width = "match_parent"
  58. android:layout_height = "wrap_content"
  59. android:id = "@+id/password"
  60. android:textColor = "@android:color/black" />
  61. <EditText
  62. android:id="@+id/password_text_input"
  63. android:layout_height="wrap_content"
  64. android:layout_width="match_parent"
  65. android:inputType="text"/>
  66. <Button
  67. android:id = "@+id/MyButton"
  68. android:layout_width = "fill_parent"
  69. android:layout_height = "wrap_content"
  70. android:text = "@string/ButtonClick" />
  71. </LinearLayout>
  72.  
  73. using System;
  74. using Android.App;
  75. using Android.Content;
  76. using Android.Runtime;
  77. using Android.Views;
  78. using Android.Widget;
  79. using Android.OS;
  80. using System.Data.SqlClient;
  81. //using System.Data.Sql;
  82.  
  83. namespace first
  84. {
  85. [Activity(Label = "@string/app_name", MainLauncher = true)]
  86. public class MainActivity : Activity
  87. {
  88. protected override void OnCreate(Bundle bundle)
  89. {
  90. base.OnCreate(bundle);
  91. SetContentView(Resource.Layout.activity_main);
  92. Button button = FindViewById<Button>(Resource.Id.MyButton);
  93. button.Click += delegate {
  94. // button.Text = "Hello world I am your first App";
  95. string connectionString = @"Server='test';Database='connect';User Id='test';Password='test';Trusted_Connection=true";
  96. string databaseTable = "Connect";
  97. //string referenceAccountNumber = "0001134919";
  98. string selectQuery = String.Format("SELECT * FROM {0} ", databaseTable);
  99. try
  100. {
  101. using (SqlConnection connection = new SqlConnection(connectionString))
  102. {
  103. //open connection
  104. connection.Open();
  105.  
  106. button.Text = "Connection Successful";
  107. SqlCommand command = new SqlCommand(selectQuery, connection);
  108.  
  109. command.Connection = connection;
  110. command.CommandText = selectQuery;
  111. var result = command.ExecuteReader();
  112. //check if account exists
  113. var exists = result.HasRows;
  114. }
  115. }
  116. catch (Exception exception)
  117. {
  118. #region connection error
  119. AlertDialog.Builder connectionException = new AlertDialog.Builder(this);
  120. connectionException.SetTitle("Connection Error");
  121. connectionException.SetMessage(exception.ToString());
  122. connectionException.SetNegativeButton("Return", delegate { });
  123. connectionException.Create();
  124. connectionException.Show();
  125. #endregion
  126. }
  127. };
  128. }
  129. }
  130. }
Add Comment
Please, Sign In to add comment