读书人

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

发布时间: 2013-03-21 10:08:17 作者: rapoo

初写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();


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

读书人网 >C#

热点推荐