none
对DataTable的困惑? RRS feed

  • 问题

  • DataTable可否更新从数据源中读取的部分数据,需要哪些必要条件?还有,如果我创建一个跟数据源中的表结构一样的DataTable,这样是否也可以更新数据源中的数据,谢谢
    2009年4月22日 1:02

答案

  • // 建表 SQL
                //CREATE TABLE [dbo].[Employee](
                //    [fldGuid] [uniqueidentifier] NOT NULL,
                //    [fldName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
                //    [fldDescription] [nvarchar](500) COLLATE Chinese_PRC_CI_AS NOT NULL,
                // CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 
                //(
                //    [fldGuid] ASC
                //)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
                //) ON [PRIMARY]
    
                string fConnectionString = @"server=XXY\SQLEXPRESS;database=XSample;Integrated Security=SSPI";
                string fCommandText = "SELECT fldGuid, fldName, fldDescription FROM Employee";
    
                using (SqlConnection connection = new SqlConnection(fConnectionString))
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand(fCommandText, connection);
                    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
    
                    connection.Open();
    
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "Employee");
    
                    dataSet.Tables["Employee"].Rows.Add(new object[] { Guid.NewGuid(), "Name 01", "Description 01" });
                    dataSet.Tables["Employee"].Rows.Add(new object[] { Guid.NewGuid(), "Name 02", "Description 02" });
                    dataSet.Tables["Employee"].Rows.Add(new object[] { Guid.NewGuid(), "Name 03", "Description 03" });
                    dataSet.Tables["Employee"].Rows.Add(new object[] { Guid.NewGuid(), "Name 04", "Description 04" });
    
                    builder.GetUpdateCommand();
                    adapter.Update(dataSet, "Employee");
                }
    如果要更新的话要求 SelectCommand 还必须至少返回一个主键列或唯一的列, 建议您了解一个强类型 DataSet

    知识改变命运,奋斗成就人生!
    2009年4月22日 1:11
    版主
  • 你好!
         更新主要使用的是DbDataAdapter的Update方法,类似这样:
    public
     DataSet CreateCmdsAndUpdate(DataSet dataSet, string
     connectionString,
    string queryString)
    {
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    adapter.SelectCommand = new OleDbCommand(queryString, connection);
    OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

    connection.Open();

    DataSet customers = new DataSet();
    adapter.Fill(customers);

    //code to modify data in dataset here

    adapter.Update(customers);

    return customers;
    }
    }

    周雪峰
    2009年4月22日 1:59
    版主

全部回复

  • // 建表 SQL
                //CREATE TABLE [dbo].[Employee](
                //    [fldGuid] [uniqueidentifier] NOT NULL,
                //    [fldName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
                //    [fldDescription] [nvarchar](500) COLLATE Chinese_PRC_CI_AS NOT NULL,
                // CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 
                //(
                //    [fldGuid] ASC
                //)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
                //) ON [PRIMARY]
    
                string fConnectionString = @"server=XXY\SQLEXPRESS;database=XSample;Integrated Security=SSPI";
                string fCommandText = "SELECT fldGuid, fldName, fldDescription FROM Employee";
    
                using (SqlConnection connection = new SqlConnection(fConnectionString))
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand(fCommandText, connection);
                    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
    
                    connection.Open();
    
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "Employee");
    
                    dataSet.Tables["Employee"].Rows.Add(new object[] { Guid.NewGuid(), "Name 01", "Description 01" });
                    dataSet.Tables["Employee"].Rows.Add(new object[] { Guid.NewGuid(), "Name 02", "Description 02" });
                    dataSet.Tables["Employee"].Rows.Add(new object[] { Guid.NewGuid(), "Name 03", "Description 03" });
                    dataSet.Tables["Employee"].Rows.Add(new object[] { Guid.NewGuid(), "Name 04", "Description 04" });
    
                    builder.GetUpdateCommand();
                    adapter.Update(dataSet, "Employee");
                }
    如果要更新的话要求 SelectCommand 还必须至少返回一个主键列或唯一的列, 建议您了解一个强类型 DataSet

    知识改变命运,奋斗成就人生!
    2009年4月22日 1:11
    版主
  • 你好!
         更新主要使用的是DbDataAdapter的Update方法,类似这样:
    public
     DataSet CreateCmdsAndUpdate(DataSet dataSet, string
     connectionString,
    string queryString)
    {
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    adapter.SelectCommand = new OleDbCommand(queryString, connection);
    OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

    connection.Open();

    DataSet customers = new DataSet();
    adapter.Fill(customers);

    //code to modify data in dataset here

    adapter.Update(customers);

    return customers;
    }
    }

    周雪峰
    2009年4月22日 1:59
    版主