📄 rowoperation.cs
字号:
using System;
using System.Data;
using System.Data.OleDb;
namespace RowOperationApp
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class RowOperation
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\C#Program\C#100\Chapter3\NWIND.mdb");
connection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Employees",connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
DataSet dataset = new DataSet();
// adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
adapter.Fill(dataset,"Employees");
Console.WriteLine("rows before change:{0}",dataset.Tables["Employees"].Rows.Count);
DataColumn[] keys = new DataColumn[2];
keys[0] = dataset.Tables["Employees"].Columns["FirstName"];
keys[1] = dataset.Tables["Employees"].Columns["LastName"];
dataset.Tables["Employees"].PrimaryKey = keys;
string[] name = {"Feng","Wang"};
DataRow findRow = dataset.Tables["Employees"].Rows.Find(name);
if (findRow == null)
{
Console.WriteLine("Row {0} {1} don't exist, add it to Employees table",name[0],name[1]);
DataRow newRow = dataset.Tables["Employees"].NewRow();
newRow["FirstName"] = name[0];
newRow["LastName"] = name[1];
dataset.Tables["Employees"].Rows.Add(newRow);
Console.WriteLine("Row {0} {1} successfully added it into Employees table",name[0],name[1]);
}
else
{
Console.WriteLine("Row {0} {1} already exist in Employees table",name[0],name[1]);
}
adapter.Update(dataset,"Employees");
connection.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -