#安卓开发# 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