如何把数据表中的某一条记录的特定字段的值取出来放在一个变量里?(50分)

  • 主题发起人 主题发起人 lanchong
  • 开始时间 开始时间
L

lanchong

Unregistered / Unconfirmed
GUEST, unregistred user!
环境:win2000professional,vb.net,asp.net,sql server2000
初学ASP.net,请大家多指教。
 
dc1 = dataset1.Tables("Suppliers").Columns("SupplierID")
下面是mshelp


' Create the ConnectionString and create a SqlConnection.
' Change the data source value to the name of your computer.
Dim cString As string = "user id=sa;" &
_
"password=;" &
_
"initial catalog=northwind;" &
_
"data source=MyComputerName/NetSDK;" &
_
"Connect Timeout=5"
Dim cnNorthwind As SqlConnection = new SqlConnection(cString)
' Create a SqlDataAdapter for the Suppliers table.
Dim adpSuppliers As SqlDataAdapter = new SqlDataAdapter()
' A table mapping tells the adapter what to call the table.
adpSuppliers.TableMappings.Add("Table", "Suppliers")
cnNorthwind.Open()
Dim cmdSuppliers As SqlCommand = _
new SqlCommand("SELECT * FROM Suppliers", cnNorthwind)
cmdSuppliers.CommandType = CommandType.Text
adpSuppliers.SelectCommand = cmdSuppliers
Console.WriteLine("The connection is open.")
ds = New DataSet("Customers")
adpSuppliers.Fill(ds)
' Create a second SqlDataAdapter and SqlCommand to get
' the Products table, a child table of Suppliers.
Dim adpProducts As SqlDataAdapter = new SqlDataAdapter()
adpProducts.TableMappings.Add("Table", "Products")
Dim cmdProducts As SqlCommand = _
new SqlCommand("SELECT * FROM Products", cnNorthwind)
adpProducts.SelectCommand = cmdProducts
adpProducts.Fill(ds)
cnNorthwind.Close()
Console.WriteLine("The connection is closed.")
' You must create a DataRelation to link the two tables.
Dim dr As DataRelation
Dim dc1 As DataColumn
Dim dc2 As DataColumn
' Get the parent and child columns of the two tables.
dc1 = ds.Tables("Suppliers").Columns("SupplierID")
dc2 = ds.Tables("Products").Columns("SupplierID")
dr = new System.Data.DataRelation("suppliers2products", dc1, dc2)
ds.Relations.Add(dr)
 
我的想法是:根本不用Dim cnNorthwind As SqlConnection = new SqlConnection(cString)之类的命令,我用的全是VB.net的控件,希望能用sqlcommand和sqlconnection控件解决这个问题,喜喜
 
各项都配置好后,可以直接取的
str=dataSet1.Tables["表名"].Rows[行号]["列名"].ToString();[:)]
 
我在delphi中一般这样:
放一个adoconn和adoquery控件,adoquery的sql设为:select 字段2 from 某表 where 字段1=“某某”,execsql后,变量名=adoquery.fieldbyname("字段2").value就可以了。
我想用asp.net做到这一点,不知该如何做?
 
用SqlCommand1.ExecuteReader()能否可以?
 
可以的,用DataReader可以快速获取结果,但它是只读的,我的上一种方法是可以写入的
 
后退
顶部