MAXSCRIPT 连数据库(转)
dotnet.loadassembly "System.Data" --导入.net的system.data组件
constring = "uid = sa;pwd = sa;data source=localhost;database=test" --连接语句,我连的是sqlServer
con = dotNetObject "System.Data.SqlClient.SqlConnection" constring --连接数据库
ds = dotNetObject "System.Data.Dataset" --定义dataset用来装数据
tableadapter = dotNetObject "System.Data.SqlClient.SqlDataAdapter" "SELECT * FROM test" constring --执行SQL语句
tableadapter.fill ds "test" --把查询结果装到ds下的"test" datatable中
dt = dotNetObject "System.Data.Datatable"
dt = ds.tables.item["test] --取到dt中
tmp = dt.rows.item[1].item[2] --把dt中的1行2列的数据取出来(行号和列号从0开始)
messagebox(tmp)
---以下代码把整个数据库的内容用listBox显示出来
lst_test=dotnetObject "System.Windows.Forms.Listbox"
lst_test.DataSource = dt
lst_test.DisplayMember = "name" --显示列名为name的列
hForm = dotNetObject "System.Windows.Forms.Form"
hForm.Size = dotNetObject "System.Drawing.Size" 310 335
hForm.Text = ".Net 2.0 Form with TextBox"
hForm.Controls.Add(lst_test)
hForm.TopMost = true
FormBorderStyle = dotNetClass "System.Windows.Forms.FormBorderStyle"
hForm.FormBorderStyle = FormBorderStyle.FixedDialog
hForm.ShowInTaskbar = false
hForm.MinimizeBox = false
hForm.MaximizeBox = false
hApp = dotNetClass "System.Windows.Forms.Application"
hApp.Run hForm