Recent

Author Topic: beginner Question: about ODBCConnection  (Read 1766 times)

xiyi0616

  • New Member
  • *
  • Posts: 12
beginner Question: about ODBCConnection
« on: June 26, 2024, 08:55:37 am »
I build some TSQLQuery component useing ODBCConnection database.
ODBCConnection driver: ODBC Driver 17 for SQL Server

I have no problem using multiple SQLQuery to execute select statements and return datas in a button event.

For example:
  sn := 35555;
  SQLQuery1.Close;
  SQLQuery1.SQL.Clear;
  SQLQuery1.Params.Clear;
  SQLQuery1.SQL.Add('select * from tb_UploadPackage where x_SerialNo=:p0');
  SQLQuery1.Params[0].AsInteger := sn;
  SQLQuery1.Open;

  SQLQuery2.Close;
  SQLQuery2.SQL.Clear;
  SQLQuery2.Params.Clear;
  SQLQuery2.SQL.Add('select * from tb_UploadPackage');
  SQLQuery2.Open;


  SQLQuery3.Close;
  SQLQuery3.SQL.Clear;
  SQLQuery3.Params.Clear;
  SQLQuery3.SQL.Add('select * from tb_UploadErrInfo where x_SerialNo=:p0');
  SQLQuery3.Params[0].AsInteger := sn;
  SQLQuery3.Open;


But when I click on this button and then click on the second button to perform a similar SQLQuery operation, I inevitably get an error: (See Attach pic), even if I cut a portion of the code from the first button event to the second one.

This is very strange. Does anyone know the reason? How should it be resolved?

(I try to check the sqlerror code, There is no specific indication of the problem.  I suspect it has something to do with transactions, but I'm not very familiar with the current data component mechanisms in Lazarus.)


« Last Edit: June 26, 2024, 06:55:04 pm by xiyi0616 »

Seenkao

  • Hero Member
  • *****
  • Posts: 649
    • New ZenGL.
Re: beginner Question: about ODBCConnection
« Reply #1 on: June 26, 2024, 01:19:20 pm »
Товарищи разработчики, вы ведь подрабатываете телепатами?

Мне просто интересно, как можно подсказать что-то, что не связано с картинкой, по картинке? Может всё-таки код приложите для того чтоб вам можно было помочь?


Google translate:
Comrade developers, you work part-time as telepaths, right?

I'm just wondering how you can suggest something that is not related to a picture from a picture? Maybe you can still attach the code so that we can help you?
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

xiyi0616

  • New Member
  • *
  • Posts: 12
Re: beginner Question: about ODBCConnection
« Reply #2 on: June 26, 2024, 01:57:01 pm »
Thank you for your time!

As a novice, I might assume this is a common issue.

But you are right, i attach some files.

When I click the Open button first and then the Test button, I get an error.

Zvoni

  • Hero Member
  • *****
  • Posts: 2792
Re: beginner Question: about ODBCConnection
« Reply #3 on: June 26, 2024, 04:04:56 pm »
You might be correct with your suspicion about Transactions.
Have you tried to give each SQLQuery its own Transaction?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

xiyi0616

  • New Member
  • *
  • Posts: 12
Re: beginner Question: about ODBCConnection
« Reply #4 on: June 26, 2024, 04:19:27 pm »
This is a good iea. Tks.
Perhaps I have a misconception that all components using a same library should be able to use a same transaction component. I have just started using the database components in Lazarus.
« Last Edit: June 26, 2024, 04:22:28 pm by xiyi0616 »

xiyi0616

  • New Member
  • *
  • Posts: 12
Re: beginner Question: about ODBCConnection
« Reply #5 on: June 26, 2024, 04:36:27 pm »
I have tried using another transaction component with SQLQuery2 in the Test button event, but I still encounter the same error.

Thaddy

  • Hero Member
  • *****
  • Posts: 16335
  • Censorship about opinions does not belong here.
Re: beginner Question: about ODBCConnection
« Reply #6 on: June 26, 2024, 06:12:52 pm »
Note there is only one(1) 64 bit driver for ODBC and it is never been very well tested.
(not fpc fault, but ms), so if you try to compile for 64 bit your code may fail.
Except for MS SQL, all other ODBC drivers are 32 bit code.
There is nothing wrong with being blunt. At a minimum it is also honest.

xiyi0616

  • New Member
  • *
  • Posts: 12
Re: beginner Question: about ODBCConnection
« Reply #7 on: June 26, 2024, 06:46:45 pm »
This is a bad message. ODBC 17 or 18 is the only driver that I know of for connecting to MS SqlServer on Linux.

af0815

  • Hero Member
  • *****
  • Posts: 1381
Re: beginner Question: about ODBCConnection
« Reply #8 on: June 26, 2024, 07:43:21 pm »
you can connect to ms-sql with freetds drivers on linux, win, raspbian.
regards
Andreas

xiyi0616

  • New Member
  • *
  • Posts: 12
Re: beginner Question: about ODBCConnection
« Reply #9 on: June 26, 2024, 07:45:37 pm »
Thank you for the suggestion. I will give it a try.

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: beginner Question: about ODBCConnection
« Reply #10 on: June 26, 2024, 08:12:12 pm »
Without the content of the error, it is hard to determine the cause.
You need to change the code page of the message to UTF-8. To do this you first need to find out what collation is set for the MSSQL server, then instead:
Code: Pascal  [Select][+][-]
  1.   SQLQuery2.Open;
you insert:
Code: Pascal  [Select][+][-]
  1.   try
  2.     SQLQuery1.Open;
  3.   except
  4.     on E: Exception do
  5.     begin
  6.       ShowMessage(CP950ToUTF8(E.Message)); // or: showMessage(CP936ToUTF8(E.Message));
  7.       exit;
  8.     end;
  9.   end;
and add LConvEncoding unit to uses section
Then you should see the error content on the message instead of these stamps.

I checked your project under Windows 10 (MSSQL also installed on Windows) and I don't get any errors in both 32 and 64 bit versions of the application.
Best regards / Pozdrawiam
paweld

xiyi0616

  • New Member
  • *
  • Posts: 12
Re: beginner Question: about ODBCConnection
« Reply #11 on: June 26, 2024, 10:34:42 pm »
The MS SQLServer which i used is not utf-8 code page, local language.  It's early version. Server 2008.


I think you're right. Let's take a look at the complete error message, and I'll try your code.

« Last Edit: June 27, 2024, 06:38:02 am by xiyi0616 »

xiyi0616

  • New Member
  • *
  • Posts: 12
Re: beginner Question: about ODBCConnection
« Reply #12 on: June 26, 2024, 10:51:43 pm »
I use CP936ToUTF8 function instead CP950ToUTF8.
Then i get useful message. But it is looks like limit to length, the message is not complete. Just head
Which means: Busy connection to ...

xiyi0616

  • New Member
  • *
  • Posts: 12
Re: beginner Question: about ODBCConnection
« Reply #13 on: June 26, 2024, 10:57:56 pm »
Am I getting close to the root of the problem?
Is my version of MSSQL Server too low?
« Last Edit: June 26, 2024, 11:05:14 pm by xiyi0616 »

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: beginner Question: about ODBCConnection
« Reply #14 on: June 26, 2024, 11:07:30 pm »
Replace this procedure
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Memo1.Lines.Clear;
  4.   //ODBCConnection1.Driver:='ODBC Driver 17 for SQL Server';
  5.   ODBCConnection1.Params.Clear;
  6.  
  7.   ODBCConnection1.Transaction := SQLTransaction1;
  8.  
  9.   ODBCConnection1.Driver:=Edit_Driver.Text;
  10.   ODBCConnection1.UserName:=Edit_MSSQLUser.Text;
  11.   ODBCConnection1.Params.Add('Database=' + Edit_MSSQLDB.Text);
  12.   ODBCConnection1.Password:=Edit_MSSQLPW.Text;
  13.   ODBCConnection1.Params.Add('Trusted_Connection=No;');
  14.   ODBCConnection1.Params.Add('Server=' + Edit_MSSQLSrv.Text);
  15.   ODBCConnection1.Params.Add('MARS_Connection=Yes;');
  16.   SQLQuery1.DataBase := ODBCConnection1;
  17.  
  18.   Memo1.Lines.Add('Try to Connect ...');
  19.   try
  20.      ODBCConnection1.Connected:= True;
  21.  
  22.      if ODBCConnection1.Connected then
  23.         Memo1.Lines.Add('Connected');
  24.  
  25.   except
  26.     on E: Exception do
  27.       Memo1.Lines.Add(e.Message);
  28.   end;
  29.  
  30. end;
And try again
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018