首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

初写ADO.net,为啥更新不了,求指教

2013-03-21 
初写ADO.net,为什么更新不了,求指教。using Systemusing System.Datausing System.Data.SqlClientusing

初写ADO.net,为什么更新不了,求指教。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection thisConnection = new SqlConnection(
                @"Server=CHINESE-F1FA31E\MYSQLSERVER;User=CHINESE-F1FA31E;PWD=null;" + "Database=StudentsMIS");
            SqlDataAdapter thisAdapter = new SqlDataAdapter (
                "SELECT DepID,DepName FROM DepartmentName",thisConnection);
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
            DataSet thisDataSet = new DataSet();
            thisAdapter.Fill(thisDataSet, "DepartmentName");
            Console.WriteLine("# rows before change:{0}",
                thisDataSet.Tables["DepartmentName"].Rows.Count);
            DataRow thisRow = thisDataSet.Tables["DepartmentName"].NewRow();
            thisRow["DepID"] = "7";
            thisRow["DepName"] = "德语学院";
            thisDataSet.Tables["DepartmentName"].Rows.Add(thisRow);
            Console.WriteLine("# rows after change:{0}",
                thisDataSet.Tables["DepartmentName"].Rows.Count);
            thisAdapter.Update(thisDataSet, "DepartmentName");

            thisConnection.Close();
            Console.WriteLine("Program finished, press Enter/Return to continue:");
            Console.ReadLine();

        }
    }
}
database
[解决办法]

string connstring = @"server=server;database=s2;uid=uid;pwd=psw";

            using (SqlConnection thisConnection = new SqlConnection(connstring))
            {
                SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT DepID,DepName FROM DepartmentName", thisConnection);


                SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
                DataSet thisDataSet = new DataSet();
                thisAdapter.Fill(thisDataSet, "DepartmentName");
                Console.WriteLine("# rows before change:{0}",
                    thisDataSet.Tables["DepartmentName"].Rows.Count);
                DataRow thisRow = thisDataSet.Tables["DepartmentName"].NewRow();
                thisRow["DepID"] = "7";
                thisRow["DepName"] = "德语学院";
                thisDataSet.Tables["DepartmentName"].Rows.Add(thisRow);
                Console.WriteLine("# rows after change:{0}",
                    thisDataSet.Tables["DepartmentName"].Rows.Count);
                thisAdapter.Update(thisDataSet, "DepartmentName");
            }
            Console.WriteLine("Program finished, press Enter/Return to continue:");
            Console.ReadLine();


在本地测试,可以新增数据的。

热点排行