日期:2014-05-19  浏览次数:20406 次

哥们门问个dataset 和dataread的问题
哥们门两个问题
第一个:我做dataread的时候用完了需要read.close

请问如果要用dataset的话需要断开连接什么的么?或者关闭什么的么?


第二个问题:
我用dataread的时候就string   username=read[ "username "].tostring()
我看过别人写的dt.table[ "** "][数字][数字]
这样来获取,我感觉挺麻烦的
请问dataset中能不能像dataread那样直接写字段名的方法呢   谢谢


两个问题   每个50分   一共100分呵呵谢谢大家帮忙

------解决方案--------------------
一样的啊 也要关闭连接
------解决方案--------------------
using System;
using System.Data;
using System.Data.SqlClient;

namespace Microsoft.AdoNet.DataSetDemo
{
class NorthwindDataSet
{
static void Main()
{
string connectionString = GetConnectionString();
ConnectToData(connectionString);
}

private static void ConnectToData(string connectionString)
{
//Create a SqlConnection to the Northwind database.
using (SqlConnection connection =
new SqlConnection(connectionString))
{
//Create a SqlDataAdapter for the Suppliers table.
SqlDataAdapter adapter = new SqlDataAdapter();

// A table mapping names the DataTable.
adapter.TableMappings.Add( "Table ", "Suppliers ");

// Open the connection.
connection.Open();
Console.WriteLine( "The SqlConnection is open. ");

// Create a SqlCommand to retrieve Suppliers data.
SqlCommand command = new SqlCommand(
"SELECT SupplierID, CompanyName FROM dbo.Suppliers; ",
connection);
command.CommandType = CommandType.Text;

// Set the SqlDataAdapter 's SelectCommand.
adapter.SelectCommand = command;

// Fill the DataSet.
DataSet dataSet = new DataSet( "Suppliers ");
adapter.Fill(dataSet);

// Create a second Adapter and Command to get
// the Products table, a child table of Suppliers.
SqlDataAdapter productsAdapter = new SqlDataAdapter();
productsAdapter.TableMappings.Add( "Table ", "Products ");

SqlCommand productsCommand = new SqlCommand(
"SELECT ProductID, SupplierID FROM dbo.Products; ",
connection);
productsAdapter.SelectCommand = productsCommand;

// Fill the DataSet.
productsAdapter.Fill(dataSet);

// Close the connection.
connection.Close();
Console.WriteLine( "The SqlConnection is closed. ");

// Create a DataRelation to link the two tables
// based on the SupplierID.
DataColumn parentColumn =
dataSet.Tables[ "Suppliers "].Columns[ "SupplierID "];
DataColumn childColumn =
dataSet.Tables[ "Products "].Columns[ "SupplierID "];
DataRelation relation =
new System.Data.DataRelation( "SuppliersProducts ",
parentColumn, childColumn);
dataSet.Relations.Add(relation);