日期:2014-05-18  浏览次数:20936 次

为什么这样不能将DataSet中的内容提交到数据库中 急

C# code
  很感谢各位提供的代码,现在虽然不报Update 无法找到 TableMapping['table'] 或 DataTable“table”。
这个错误了,但是依然不能将DataSet中的保存到数据库
http://topic.csdn.net/u/20081216/10/6b1e01d6-e8ff-4696-9bfc-ac776b31641e.html?seed=1494657499
以下是我的代码

 protected void Button2_Click(object sender, EventArgs e)
    {
        WebSerict.Service_order order = new WebSerict.Service_order();//这是一个webservice的调用
         DataSet ds = new DataSet();
         ds = order.getdata("zhanghao", "p1", "p2");//返回一个dataset
  
        SqlConnection conn = new SqlConnection(ConfigurationManager.
           ConnectionStrings["WebConnectionString935"].ConnectionString);
        SqlDataAdapter myAdapter = new SqlDataAdapter();
        string s = "INSERT INTO [Web].[dbo].[getdata]([bl_no],[bl_create_date],[bl_create_time]"
            + ",[ship_cor_code],[agent],[vessel],[voyage],[dis_port],[mid_port])VALUES"
            + "(<bl_no, varchar(30),>,<bl_create_date, varchar(8),>,<bl_create_time, varchar(8),>"
            + ",<ship_cor_code, varchar(10),> ,<agent, varchar(15),>,<vessel, varchar(15),>"
            + ",<voyage, varchar(10),> ,<dis_port, varchar(30),>,<mid_port, varchar(30),>)";
        SqlCommand myCommand = new SqlCommand(s, conn);
        s = "select * from dbo.getdata";
        SqlCommand myCommand2 = new SqlCommand(s, conn);
        
        myAdapter.SelectCommand = myCommand2;
        myAdapter.InsertCommand = myCommand;      
        
        SqlCommandBuilder bu = new SqlCommandBuilder(myAdapter);
        DataRow row = ds.Tables[0].Rows[0];
    
  
        TextBox1.Text = bu.GetInsertCommand().CommandText.ToString();
        TextBox2.Text = bu.GetUpdateCommand().CommandText.ToString();
     
        DataTableMapping tableMapping = myAdapter.TableMappings.Add("getdata",ds.Tables[0].TableName.ToString());
        //DataTable dt = ds.Tables[0];
        myAdapter.Update(ds, "getdata"); 
  }


SQL code

我本地表的表结构
CREATE TABLE [dbo].[getdata](
    [bl_no] [varchar](30) COLLATE Chinese_PRC_CI_AS NOT NULL,
    [bl_create_date] [varchar](8) COLLATE Chinese_PRC_CI_AS NULL,
    [bl_create_time] [varchar](8) COLLATE Chinese_PRC_CI_AS NULL,
    [ship_cor_code] [varchar](10) COLLATE Chinese_PRC_CI_AS NULL,
    [agent] [varchar](15) COLLATE Chinese_PRC_CI_AS NULL,
    [vessel] [varchar](15) COLLATE Chinese_PRC_CI_AS NULL,
    [voyage] [varchar](10) COLLATE Chinese_PRC_CI_AS NULL,
    [dis_port] [varchar](30) COLLATE Chinese_PRC_CI_AS NULL,
    [mid_port] [varchar](30) COLLATE Chinese_PRC_CI_AS NULL,
 
)





------解决方案--------------------
ds.AcceptChanges()呢
------解决方案--------------------
C# code
其实就是多加一个DataSet
localhost.WebService webs = new localhost.WebService();
        DataSet ds = new DataSet();
        ds = webs.HelloWorld1();

        SqlConnection conn = new SqlConnection(ConfigurationManager.
           ConnectionStrings["zh1_databaseConnectionString935"].ConnectionString);
        conn.Open();
        string s = "select * from getdata_new";
        
        SqlDataAdapter myAdapter = new SqlDataAdapter(s,conn);        
        SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(myAdapter);
        DataSet dsSoure = new DataSet();
        myAdapter.Fill(dsSoure);

        DataRow row = null;
        foreach (DataRow row1 in ds.Tables[0].Rows)
        {
            row = dsSoure.Tables[0].NewRow();
            row[0] = row1[0];
            dsSoure.