XWidgetSoft Forum
https://www.bbs.xwidget.com/

Connect Database with my Widget
https://www.bbs.xwidget.com/viewtopic.php?f=10&t=2486
Page 1 of 1

Author:  Achinthac [ January 2nd, 2013, 3:37 am ]
Post subject:  Connect Database with my Widget

How do i connect MS Access or My Sql Database with my widget ?

Author:  Tony [ January 2nd, 2013, 8:37 pm ]
Post subject:  Re: Connect Database with my Widget

you can try to uses Activex/OLE in script to connect MS Access

Author:  Achinthac [ January 6th, 2013, 6:35 am ]
Post subject:  Re: Connect Database with my Widget

Thanks i got it

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sub button2OnClick(Sender)
Dim strConnection, conn, rs, strSQL
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:\MyData\DB\waste.mdb'"
Set conn = CreateObject("ADODB.Connection")
conn.Open(strConnection)
Set rs = CreateObject("ADODB.recordset")
strSQL = "Select * from Items"
rs.Open strSQL,conn
rs.MoveFirst
while not rs.EOF
Print(rs("Item_des"))
rs.MoveNext
WEND
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Author:  alfredorhz [ March 20th, 2013, 6:55 pm ]
Post subject:  Re: Connect Database with my Widget

Hi, can u be more especific, please? or please an example
Can make a list from a db, with autoupdate?
gracias

Author:  hamid [ July 3rd, 2014, 1:08 am ]
Post subject:  Re: Connect Database with my Widget

after studying more on this, now i learned how to do this;
to clarify the example above:


this is an example of how to connect to a database
you should change some parameters in the code to use it on your own database file,
most important ones are these:
1. [dbfile path]: the database file's path that you want to connect to it
ex: in here it's: 'D:\MyData\DB\waste.mdb'
2. [TableName]: the table name in the your db file
ex: in here it's: "Items"
3. [FieldName]: the Field you want to do something on it
ex: in here it's: "Item_des"



=================
Important Note:
Xwidget uses javascript mode by default

to change between javascript/vbscript:
1. open Xwidget Designer
2. from the left Sidebar (Widget tree view), select [Widget Attributes], [widget Attribute] form will be shown
3. from this form, find [Script Lang] dropdown menu, and select one that you desire (JavaScript/VBScript)
4. now in Script Editor you can write the code in the language you have selected
================



this is a VBScript code:
(use it if you are using your widget in VBScript code, not the JavaScript code)
Code:
Sub button2OnClick(Sender)
  Dim strConnection, conn, rs, strSQL
  strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:\MyData\DB\waste.mdb'"
  Set conn = CreateObject("ADODB.Connection")
  conn.Open(strConnection)
  Set rs = CreateObject("ADODB.recordset")
  strSQL = "Select * from Items"
  rs.Open strSQL,conn
  rs.MoveFirst
  while not rs.EOF
     Print(rs("Item_des"))
     rs.MoveNext
  WEND
  rs.Close
  Set rs = Nothing
  conn.Close
  Set conn = Nothing
End Sub


JavaScript version of same code is this:
(use this if you're using your widget in JavaScript mode)
Code:
function button2OnClick(Sender)
{
  var strConnection, conn, rs, strSQL
  strConnection = "Provider= Microsoft.Jet.OLEDB.4.0 ;Data Source='D:\MyData\DB\waste.mdb'"
  conn = new ActiveXObject("ADODB.Connection");
  conn.Open(strConnection)
  rs = new ActiveXObject("ADODB.recordset")
  strSQL = "Select * from Items"
  rs.Open(strSQL,conn)
  rs.MoveFirst()
  while(!rs.EOF)
  {
     Print(rs("Item_des"))
     rs.MoveNext()
  }
  rs.Close()
  rs = null
  conn.Close()
  conn = null
}

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/