Logging to SQL Server Database

Hi everybody, I am quite new to Xojo, and I have a problem I don’t know how to face it.

I am writing an app under Windows 10 using a SQL Server Database; I use Xojo ODBC Database.
I’d like to connect to the Database using the built-in SQL Server Login box, as I did when using ADO.
Having no idea how to do that, I use a login window where users type username and password, that I use to connect to the DB.

A strange thing: if you type a wrong password (or if you don’t type it at all), an error message box appears, and then ….oops! the SQL Server Login box!!!

This is the code I use to connect to the Database using username and password typed by users.

DB_User = txtUser.Text
DB_Password = txtPwd.Text
' ODBC (Native Client 10.0)
DB = New ODBCDatabase 

Dim SqlStr As String ="Driver={SQL Server Native Client 10.0};Server=" + DB_Host 
SqlStr = SqlStr + ";Database=" + DB_Name + ";UID=" + DB_User + ";Pwd=" + DB_Password + ";MARS_Connection=yes;"
SqlStr = SqlStr + "InitializationString=SET TRANSACTION ISOLATION LEVEL SERIALIZABLE"

DB.DataSource = SqlStr

DB.Host = DB_Host
DB.DatabaseName = DB_Name
DB.UserName = DB_User
DB.Password = DB_Password

If DB.Connect Then
  Main.Show
  LoginWindow.Hide
  LoginWindow.Close
  
Else
  
  MsgBox "User or password not correct. Retry."
  
End If

And this is the error message box that is displayed when the password is wrong:

Microsoft SQL Server Login

Connection failed:
SqlState: '28000'
SQL Server Error: 18456
[Microsoft][SQL Server Native Client 10.0][SQL Server]Accesso non riuscito per l'utente 'nedi'

Clicking OK in this box the SQL Server Login box is displayed; closing this box my message box is displayed (User or password not correct. Retry)

What have I to do to let the SQL Server Login box be displayed? Or what have I to do to avoid this box be displayed when users type a wrong password?

Can anyone help me?

Thank you very much!

Nedi

Have you tried using the native Xojo SQL Server class instead of ODBC? I can’t say for sure that it behaves any differently, but it’s worth a try to see.

Hi Jay, MSSQLServerDatabase was my first choice when I started writing my app, but then I switched to ODBCDatabase because of a number of problems:
a) MSSQLServer plugin requires Windows, while ODBC is Cross Platform
b) the 64 bit compiled version crashes every now and then, while the ODBC version works perfectly
c) MSSQLServer plugin causes some troubles with database fields of type varchar(max) and text. It’s a matter of encoding: with the SQL Server plugin the encoding is Nil, with ODBC is UTF8 (and I don’t know how to solve the issue…I’m quite new to Xojo)

So I need a solution using ODBC…

Can anybody help me, please?

Ciao, prova a creare una connessione ODBC nella linguetta “DSN di sistema” dal pannello di controllo di amministrazione origini dati di ODBC nella sezione degli strumenti di amministrazione del Pannello di Controllo.
32 o 64 bit a seconda della versione dell’applicazione.
Controlla che alla fine il test della connessione funzioni.

poi puoi usare qualcosa come questo pezzo di codice

BaseDati = new ODBCDatabase BaseDati.DataSource = App.ODBCConnectionName If not BaseDati.Connect then App.AppendToDBErrorLog("Application","Connect","Session.Open","Errore:" + Basedati.ErrorMessage) Return End If

prima di eseguire il pezzo di codice metti in App.ODBCConnectionName il nome della connessione che hai assegnato nel pannello.

dopo usa BaseDati per fare il resto.

Marco

Grazie Marco! Come faccio ad assegnare al DSN user e password inseriti dall’utente con la login window?

Il nome utente e password li imposti nella connessione dal pannello di controllo ODBC.
Hai due possibilit: attraverso autenticazione windows o inserendo utente e password nelle apposite caselle della procedura guidata di creazione connessione.

Il che significa che dovrei predisporre un DSN per ogni utente, visto che devo sapere chi si collega, per questioni di autorizzazioni ad eseguire certi passi e certe operazioni sul database…

Se tu crei un DSN di sistema, il problema si supera.
Poi personalmente creerei un tabella Utenti nel database, con campi dedicati alle autorizzazioni sul resto del database.

Scusa Marco, ma c’ qualcosa che mi sfugge. Se nella definizione del DSN devo indicare un utente e una password, come faccio ad identificare chi realmente l’utente che si collega? Se ho dieci utenti che utilizzano lo stesso DSN di sistema, come li distinguo uno dall’altro?
Grazie per la tua disponibilit.

Io farei una tabella nel database chiamata Utenti con questi campi:

id, nomeecognome, nomelogin, password

nomecognome il nome e cognome completo utile per le visualizzazione;
nomelogin il nickname utile nella fase di login;
id pu essere un campo autoincrementato;

nella tabella si possono aggiungere altri campi per aiutarti a determinare i permessi per ogni utente, ad esempio:
inserimentodati di tipo boolean o meglio stringa con valori S/N
modificadati
cancelladati

poi nell’applicazione farei una videata iniziale che sfrutta questa tabella per consentire l’accesso al resto della procedura.

Non voglio reinventare la ruota: SQL Server gestisce utenti, gruppi e autorizzazioni a utenti/gruppi, impedendo ad un utente di eseguire operazioni per le quali non autorizzato.
Gi ho dovuto reinventare il lock sui record, visto che non ho trovato soluzioni (se non ricorrere ad ADO), ma non va bene che debba fare io cose che il motore del database fa gi egregiamente.