#安卓開發# B4A 連接mssql 數據庫
- 2020 年 3 月 11 日
- 筆記

- 引用類庫 mssql(1.0.1) javaobject
- 連接mssql代碼
Sub Process_Globals Public ms_sql As MSSQL End Sub Public Sub cn_sql As Boolean Try gbyg ms_sql.setDatabase("數據庫地址","數據庫名","數據庫用戶名","數據庫用戶密碼") Return True Catch Return False End Try End Sub Public Sub res_sql(sql As String) As Object Try Dim tb As List tb.Initialize tb=ms_sql.Query(sql) Return tb Catch Return Null End Try End Sub Public Sub cmd(sql As String) As Boolean Try ms_sql.ExecuteNonQuery(sql,True) Return True Catch Return False End Try End Sub Sub gbyg Dim jo As JavaObject jo.InitializeStatic("android.os.Build.VERSION") If jo.GetField("SDK_INT") > 9 Then Dim policy As JavaObject policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null) policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null) Dim sm As JavaObject sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy)) End If End Sub
3.main 代碼
#Region Project Attributes #ApplicationLabel: B4A Example #VersionCode: 1 #VersionName: 'SupportedOrientations possible values: unspecified, landscape or portrait. #SupportedOrientations: unspecified #CanInstallToExternalStorage: False #End Region #Region Activity Attributes #FullScreen: False #IncludeTitle: True #End Region Sub Process_Globals End Sub Sub Globals Private Button1 As Button Private Button2 As Button Private EditText1 As EditText Private EditText2 As EditText End Sub Sub Activity_Create(FirstTime As Boolean) Activity.LoadLayout("main") If FirstTime Then If code_sql.cn_sql Then ToastMessageShow("數據庫連接成功!",False) Else ToastMessageShow("數據庫連接失敗!",True) End If End If End Sub Sub Activity_Resume End Sub Sub Activity_Pause (UserClosed As Boolean) End Sub Sub Button1_Click StartActivity(Login) End Sub Sub Button2_Click If EditText1.Text="" Or EditText2.Text="" Then ToastMessageShow("請輸入用戶名或密碼!",False) Else If tf_user Then code_sql.name=EditText1.Text StartActivity(welcome) Else ToastMessageShow("用戶名或密碼錯誤!",False) End If End If End Sub Sub tf_user As Boolean Try Dim ls As List,rs As String ls.Initialize ls.AddAll(code_sql.res_sql($"select pswd from yz where name='${EditText1.Text}'"$)) If ls.IsInitialized Then If ls.Size>1 Then ls = ls.get(1) rs= ls.get(0) If rs=EditText2.Text Then Return True Else Return False End If Else Return False End If Else Return False End If Catch Return False End Try End Sub
4.login代碼
#Region Activity Attributes #FullScreen: False #IncludeTitle: True #End Region Sub Process_Globals End Sub Sub Globals Private EditText1 As EditText Private EditText2 As EditText Private Label1 As Label Private Button1 As Button End Sub Sub Activity_Create(FirstTime As Boolean) Activity.LoadLayout("login") End Sub Sub Activity_Resume End Sub Sub Activity_Pause (UserClosed As Boolean) End Sub Sub Button1_Click If EditText1.Text="" Or EditText2.Text="" Then ToastMessageShow("請輸入用戶名或密碼!",False) Else If code_sql.cmd($"insert into yz(name,pswd) values ('${EditText1.Text}','${EditText2.Text}')"$) Then Msgbox("註冊成功!","") Else ProgressDialogHide Msgbox("註冊失敗!","") End If End If End Sub
5.源代碼下載地址
https://www.lanzous.com/i9y1rgh